Skip to Content
sed & awk, 2nd Edition
book

sed & awk, 2nd Edition

by Dale Dougherty, Arnold Robbins
March 1997
Intermediate to advanced
432 pages
11h 31m
English
O'Reilly Media, Inc.
Content preview from sed & awk, 2nd Edition

Testing and Saving Output

In our previous discussion of the pattern space, you saw that sed:

  1. Makes a copy of the input line.

  2. Modifies that copy in the pattern space.

  3. 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 testfile

does 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 > newfile

The 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 newfile

This 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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The AWK Programming Language, 2nd Edition

The AWK Programming Language, 2nd Edition

Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger

Publisher Resources

ISBN: 1565922255Supplemental ContentErrata Page