Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.