I need to replace two patterns in each line of a file. Lets say for example the file contains the following:
pattern1 a1 b1 c1
pattern2 a2 b2 c2
pattern1 a1 b1 c1
Basically, anywhere “pattern1” appears, a1 needs to replaced with a2 and b1 for b2, and so on. However, I also want to change pattern1 into pattern2.
So far I have something that comes kind of close:
/pattern1/ s/a1 b1 c1/a2 b2 c2/
Note, I want to keep the entire sed script in a single file.
UPDATE
Desired output, this may not be the best example, I’ll answer any questions.
pattern2 a2 b2 c2
pattern2 a2 b2 c2
pattern2 a2 b2 c2
There are various ways to do it.
You can often collapse that to a single line by using semi-colons in place of newlines (but some versions of
sedare fussier about that than others). The-eisn’t strictly necessary in this context.It isn’t entirely clear from the question whether ‘a1’ should be replaced by ‘a2’ or vice versa; this code works off the wording “a1 needs to be substituted for a2” (meaning “replace occurrences of a2 with a1”), but the example suggests the opposite substitution. If the other meaning is required, then use lines like
s/a1/a2/instead ofs/a2/a1/.