Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file template.jou that has the following format:

/codeline1
/codeline2
/var = var
/codeline3

Now I want to replace the var part with different values like A, B and C and then write the contents of template.jou with the replaced value to a new file.

I tried the following:

sed -n 's/var/A/gpw A.jou' template.jou

But this only printed the pattern matched line (i.e. /var = A) to the new file A.jou where I want the full file like this:

/codeline1
/codeline2
/var = A
/codeline3

I've also tried:

sed -i '.bak' 's/var/A/g' template.jou

This replaced the template.jou file with new contents, but I want it to change filename as well such that the new file is called A.jou.

share|improve this question
up vote 1 down vote accepted

You probably just want to redirect the output of sed to another file:

sed 's/= var/= A/g' template.jou > A.jou

Make sure that you replace just the assignment, e.g. by matching = var only.


Note that 's/var/A/gp will not achieve what you want, since var will globally be replaced, and the replacement will be printed again. You'd get this output instead:

/codeline1
/codeline2
/A = A
/A = A
/codeline3

Also, the w modifier only prints the modified part to the file specified after it, which means your A.jou would only contain /var = A and nothing else. This is why you should redirect the entire sed output to another file.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .