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


Linux shell history

Useful tips:

  • history – list history of all recorded shell history
  • history n – list last n statements
  • !n – execute nth statement from history
  • !! execute last statement