SED Tips

[problem]

Following on from the last post on removing lines with sed, how do we use sed to substitute output on the fly?

[/problem]

[solution]

This says substitute occurences of PATT with REPLACE global (or all occurences).


sed 's/PATT/REPLACE/g' filename

[/solution]

[example]

So to replace all occurences of Unix with UNIX:


sed 's/Unix/UNIX/g' filename

We can also replace patterns, with variable values, like this:


sed "s/PATT/${THEVAR}/g" filename

[/example]

[reference]

[tags]SED, Unix Coding School[/tags]

[/reference]

If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *