Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

/var = var

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:

/var = A

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:

/A = A
/A = A

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.