Friday, 3 August 2018

Linux: sed - 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:

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.”
This command overwrites the contents of the target file immediately, which is slightly dangerous. To stay on the safe side, you can create a backup in-situ.
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.
This creates “oldfile.txt.bak,” which contains the unedited text data from the original version of “oldfile.txt.”

0 comments:

Post a Comment