4 ms·
To use this solution with a version of sed that does not accept newlines in patterns (i.e. to make it portable), one has to put the commands in a sed commands f
by textmode 8y ago
To use this solution with a version of sed that does not accept newlines in patterns (i.e. to make it portable), one has to put the commands in a sed commands file and run it with sed -f.
How to make the one-liner portable without using a sed commands file?
Maybe something like:
sed 's/baz/elephant/;/^ \{2\}- elephant/{h;G;};/^ \{4\}- elephant/{h;G;};s/elephant/baz/' foo|sed -a wfoo
1. s/baz/elephant/
2. duplicate that line if two or four space indent
3. s/elephant/baz/
4. save
N.B. no temp file used to save changes
cf. jvns.ca blog:
1. search for baz
2. copy that line and paste it
3. s/baz/elephant/
4. save and quit
N.B. temp file in $TMPDIR used to save changes