Inline text replacement with sed

Replacing values in files is incredibly easy with sed. Here’s some examples:

 

sed 's/match/replace/g' file.txt

Find match, replace with replace, globally (all matches), in file.txt

 

sed 's/match/replace/g' file.txt > file2.txt

Same as before, but write results to new file, file2.txt

 

sed -n 's/match/replace/p' file.txt

-n suppresses output of the results, but /p prints out just the matching patterns that are replaced.

 

sed -i.old 's/match/replace/g' file.txt

Replace matches in the input file with inline replace (-i), renaming original file file.txt.old and writing inline replace results to file.txt

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.