Setting input and output files
By default, sed will operate on standard input. However, that’s not too useful. To run sed on a file, specify the filename at the end of the command, like so:sed 's/cat/dog/g' oldfile.txt > newfile.txt
This will run sed on “oldfile.txt” and save the command’s output to “newfile.txt.” If “newfile.txt” does not exist, it will be created by the command. If you don’t specify an output file, sed will echo the file’s new text into standard input.
If you want to overwrite the contents of a file, you’ll need to use the
-i
flag. This flag makes the edits “in place.”sed -i '' 's/cat/dog' oldfile.txt
To create a backup of the file, put an extension after the
-i
. It doesn’t need to be a functional extension: “.bu” or “.bak” work fine. This will create a backup file with an extension.sed -i.bak 's/cat/dog' oldfile.txt
0 comments:
Post a Comment