Friday, March 30, 2018

sed in-place substitute strings

For simple text search & replace, command line using sed:

# default stdout
$ sed 's/OLD_STR/NEW_STR/g'  file.txt
or
$ cat file.txt | sed 's/OLD_STR/NEW_STR/g'

output could be redirect to another file using pipe
$ !! > output.txt

if care not keeping original file, can substitute in-place using option '-i'
on linux, simply
$ sed -i 's/OLD_STR/NEW_STR/g'  file.txt

on mac osx,add an empty string after '-i'
$ sed -i '' 's/OLD_STR/NEW_STR/g'  file.txt

Labels: , , , , , , , ,