Testing and Saving Output
In our previous discussion of the pattern space, you saw that sed:
Makes a copy of the input line.
Modifies that copy in the pattern space.
Outputs the copy to standard output.
What this means is that sed has a built-in safeguard so that you don’t make changes to the original file. Thus, the following command line:
$ sed -f sedscr testfiledoes not make the change in testfile. It sends all lines to standard ouput (typically the screen)—the lines that were modified as well as the lines that are unchanged. You have to capture this output in a new file if you want to save it.
$ sed -f sedscr testfile > newfileThe redirection symbol “>” directs the output from sed to the file newfile. Don’t redirect the output from the command back to the input file or you will overwrite the input file. This will happen before sed even gets a chance to process the file, effectively destroying your data.
One important reason to redirect the output to a file is to verify your results. You can examine the contents of newfile and compare it to testfile. If you want to be very methodical about checking your results (and you should be), use the diff program to point out the differences between the two files.
$ diff testfile newfileThis command will display lines that are unique to testfile preceded by a “<” and lines unique to newfile preceded by a “>”. When you have verified your results, make a backup copy of the original input file and then use the mv command to overwrite the ...