1

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.

1 Answer 1

1

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.

You must log in to answer this question.

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