Examples


new data file

The following examples use the input file new:

$ cat new Line one. The second line. The third. This is line four. Five. This is the sixth sentence. This is line seven. Eighth and last.


Unless you instruct it not to, sed sends all linesselected or notto standard output. When you use the -n option on the command line, sed sends only certain lines, such as those selected by a Print (p) instruction, to standard output.

The following command line displays all the lines in the new file that contain the word line (all lowercase). In addition, because there is no -n option, sed displays all the lines of input. As a result the lines that contain the word line are displayed twice.

$ sed '/line/ p' new Line one. The second line. The second line. The third. This is line four. This is line four. Five. This is the sixth sentence. This is line seven. This is line seven. Eighth and last.


The command uses the address /line/, a regular expression that is a simple string. The sed utility selects each of the lines that contains a match for that pattern. The Print (p) instruction displays each of the selected lines.

The following command uses the -n option so that sed displays only the selected lines:

$ sed -n '/line/ p' new The second line. This is line four. This is line seven.


Next sed displays part of a file based on line numbers. The Print instruction selects and displays lines 3 through 6.

$ sed -n '3,6 p' new The third. This is line four. Five. This is the sixth sentence.


The next command line uses the Quit instruction to cause sed to display only the beginning of a file. In this case sed displays the first five lines of new just as a head -5 new command would.

$ sed '5 q' new Line one. The second line. The third. This is line four. Five.


program-file

When you need to give sed more complex or lengthy instructions, you can use a program-file. The print3_6 program performs the same function as the command line in a previous example. The -f option tells sed that it should read its program from the file named on the command line.

$ cat print3_6 3,6 p $ sed -n -f print3_6 new The third. This is line four. Five. This is the sixth sentence.


Append

The next program selects line 2 and uses an Append instruction to append a NEWLINE and the text AFTER. to the selected line. Because the command line does not include the -n option, sed copies all the lines from the input file new.

$ cat append_demo 2 a\ AFTER. $ sed -f append_demo new Line one. The second line. AFTER. The third. This is line four. Five. This is the sixth sentence. This is line seven. Eighth and last.


Insert

The insert_demo program selects all the lines containing the string This and inserts a NEWLINE and the text BEFORE. before the selected lines.

$ cat insert_demo /This/ i\ BEFORE. $ sed -f insert_demo new Line one. The second line. The third. BEFORE. This is line four. Five. BEFORE. This is the sixth sentence. BEFORE. This is line seven. Eighth and last.


Change

The next example demonstrates a Change instruction with an address range. When you specify a range of lines for a Change instruction, it does not change each line within the range but rather changes the block of lines to a single occurrence of the new text.

$ cat change_demo 2,4 c\ SED WILL INSERT THESE\ THREE LINES IN PLACE\ OF THE SELECTED LINES. $ sed -f change_demo new Line one. SED WILL INSERT THESE THREE LINES IN PLACE OF THE SELECTED LINES. Five. This is the sixth sentence. This is line seven. Eighth and last.


Substitute

The next example demonstrates a Substitute instruction. The sed utility selects all lines because the instruction has no address. On each line subs_demo replaces the first occurrence of line with sentence. The p flag displays each line where a substitution occurs. The command line calls sed with the -n option, so sed displays only the lines that the program explicitly requests it to display.

$ cat subs_demo s/line/sentence/p $ sed -n -f subs_demo new The second sentence. This is sentence four. This is sentence seven.


The next example is similar to the preceding one except that a w flag and filename (temp) at the end of the Substitute instruction cause sed to create the file named temp. The command line does not include the -n option, so it displays all lines in addition to writing the changed lines to temp. The cat utility displays the contents of the file temp. The word Line (starting with an uppercase L) is not changed.

$ cat write_demo1 s/line/sentence/w temp $ sed -f write_demo1 new Line one. The second sentence. The third. This is sentence four. Five. This is the sixth sentence. This is sentence seven. Eighth and last. $ cat temp The second sentence. This is sentence four. This is sentence seven.


The following bash script changes all occurrences of REPORT to report, FILE to file, and PROCESS to process in a group of files. Because it is a shell script and not a sed program file, you must have read and execute permission to the sub file to execute it as a command (page 265). The for structure (page 538) loops through the list of files supplied on the command line. As it processes each file, the script displays each filename before processing the file with sed. This program uses multiline embedded sed commands. Because the NEWLINEs between the commands are quoted (placed between single quotation marks), sed accepts multiple commands on a single, extended command line (within a shell script). Each Substitute instruction includes a g (global) flag to take care of the case where a string occurs more than one time on a line.

$ cat sub for file do         echo $file         mv $file $$.subhld         sed 's/REPORT/report/g              s/FILE/file/g              s/PROCESS/process/g' $$.subhld > $file done rm $$.subhld $ sub file1 file2 file3 file1 file2 file3


In the next example, a Write instruction copies part of a file to another file (temp2). The line numbers 2 and 4, separated by a comma, select the range of lines sed is to copy. This program does not alter the lines.

$ cat write_demo2 2,4 w temp2 $ sed -n -f write_demo2 new $ cat temp2 The second line. The third. This is line four.


The program write_demo3 is very similar to write_demo2 but precedes the Write instruction with the NOT operator (!), causing sed to write to the file those lines not selected by the address.

$ cat write_demo3 2,4 !w temp3 $ sed -n -f write_demo3 new $ cat temp3 Line one. Five. This is the sixth sentence. This is line seven. Eighth and last.


The following example demonstrates the Next instruction. When it processes the selected line (line 3), sed immediately starts processing the next line without displaying line 3.

$ cat next_demo1 3 n p $ sed -n -f next_demo1 new Line one. The second line. This is line four. Five. This is the sixth sentence. This is line seven. Eighth and last.


The next example uses a textual address. The sixth line contains the string the, so the Next instruction causes sed not to display it.

$ cat next_demo2 /the/ n p $ sed -n -f next_demo2 new Line one. The second line. The third. This is line four. Five. This is line seven. Eighth and last.


The next set of examples uses the file compound.in to demonstrate how sed instructions work together.

$ cat compound. in 1. The words on this page... 2. The words on this page... 3. The words on this page... 4. The words on this page...


The following example substitutes the string words with text on lines 1, 2, and 3 and the string text with TEXT on lines 2, 3, and 4. The example also selects and deletes line 3. The result is text on line 1, TEXT on line 2, no line 3, and words on line 4. The sed utility made two substitutions on lines 2 and 3: text for words and TEXT for text. Then sed deleted line 3.

$ cat compound 1,3 s/words/text/ 2,4 s/text/TEXT/ 3 d $ sed -f compound compound.in 1. The text on this page... 2. The TEXT on this page... 4. The words on this page...


The ordering of instructions within a sed program is critical. Both Substitute instructions are applied to the second line in the following example, as in the previous example, but the order in which the substitutions occur changes the result.

$ cat compound2 2,4 s/text/TEXT/ 1,3 s/words/text/ 3 d $ sed -f compound2 compound.in 1. The text on this page... 2. The text on this page... 4. The words on this page...


Next compound3 appends two lines to line 2. The sed utility displays all the lines from the file once because no -n option appears on the command line. The Print instruction at the end of the program file displays line 3 an additional time.

$ cat compound3 2 a\ This is line 2a.\ This is line 2b. 3 p $ sed -f compound3 compound.in 1. The words on this page... 2. The words on this page... This is line 2a. This is line 2b. 3. The words on this page... 3. The words on this page... 4. The words on this page...


The next example shows that sed always displays appended text. Here line 2 is deleted but the Append instruction still displays the two lines that were appended to it. Appended lines are displayed even if you use the -n option on the command line.

$ cat compound4 2 a\ This is line 2a.\ This is line 2b. 2 d $ sed -f compound4 compound.in 1. The words on this page... This is line 2a. This is line 2b. 3. The words on this page... 4. The words on this page...


The next example uses regular expressions in the addresses. The regular expression in the following instruction (^.) matches one character at the beginning of every line that is not empty. The replacement string (between the second and third slashes) contains two SPACEs followed by an ampersand (&). The ampersand takes on the value of whatever the regular expression matched.

$ sed 's/^./  &/' new   Line one.   The second line.   The third. ...


This type of substitution is useful for indenting a file to create a left margin. See Appendix A for more information on regular expressions.

You can also use the simpler form s/^/ / to add SPACEs to the beginnings of lines. However, in addition to placing SPACEs at the beginnings of lines with text on them, this instruction places a SPACE at the beginning of every empty linesomething the preceding command does not do.

You may want to put the preceding sed instruction into a shell script so that you do not have to remember it (and retype it) each time you want to indent a file. The chmod utility gives you read and execute permission to the ind file.

$ cat ind sed 's/^./  &/' $* $ chmod u+rx ind $ ind new   Line one.   The second line.   The third. ...


Stand-alone script

When you run the preceding shell script, it creates two processes: It calls a shell, which in turn calls sed. You can eliminate the overhead associated with the shell process by putting the line #!/usr/bin/sed -f (page 267) at the start of the script, which runs the sed utility directly. You need read and execute permission to the file holding the script.

$ cat ind2 #!/usr/bin/sed -f s/^./\  /


In the following sed program, the regular expression (two SPACEs followed by * $) matches one or more SPACEs at the end of a line. This program removes trailing SPACEs at the ends of lines, which is useful for cleaning up files that you created using vim.

$ cat cleanup sed 's/ *$//' $*


The cleanup2 script runs the same sed command as cleanup but stands alone: It calls sed directly with no intermediate shell.

$ cat cleanup2 #!/usr/bin/sed -f s/ *$//


Hold space

The next sed program makes use of the Hold space to exchange pairs of lines in a file.

$ cat s1 # Copy Pattern space (line just read) to Hold space. h # Read the next line of input into Pattern space. n # Output Pattern space. p # Copy Hold space to Pattern space. g # Output Pattern space (which now holds the previous line). p $ sed -nf s1 new The second line. Line one. This is line four. The third. This is the sixth sentence. Five. Eighth and last. This is line seven.


The commands in the s1 program process pairs of input lines. This program reads a line and stores it; reads another line and displays it; and then retrieves the stored line and displays it. After processing a pair of lines the program starts over with the next pair of lines.

The next sed program adds a blank line after each line in the input file (i.e., it double-spaces a file).

$ sed 'G' new Line one. The second line. The third. This is line four. ...


The G instruction appends a NEWLINE and the contents of the Hold space to the Pattern space. Unless you put something in the Hold space, it is empty. Thus the G instruction copies a NEWLINE to each line of input before sed displays the line(s) from the Pattern space.

The s2 sed program reverses the order of the lines in a file just as the tac utility does.

$ cat s2 # On all but the first line, append a NEWLINE and the # contents of the Hold space to the Pattern space. 2,$G # Copy the Pattern space to the Hold space. h # Delete all except the last line. $!d $ sed -f s2 new Eighth and last. This is line seven. This is the sixth sentence. Five. This is line four. The third. The second line. Line one.


This program includes three commands: 2,$G, h, and $!d. To understand this script it is important to understand how the address of the last command works: The $ is the address of the last line of input and the ! negates the address. The result is an address that selects all except the last line of input. In the same fashion you could replace the first command with 1!G: Select all except the first line for processing; the results would be the same.

Here is what happens as s2 processes the new file:

1.

The sed utility reads the first line of input (Line one.) into the Pattern space.

  1. The 2,$G does not process the first line of inputbecause of its address the G instruction starts processing at the second line.

  2. The h copies Line one. from the Pattern space to the Hold space.

  3. The $!d deletes the contents of the Pattern space. Because there is nothing in the Pattern space, sed does not display anything.

2.

The sed utility reads the second line of input (The second line.) into the Pattern space.

  1. The 2,$G adds what is in the Hold space (Line one.) to the Pattern space. The Pattern space now has The second line.NEWLINELine one.

  2. The h copies what is in the Pattern space to the Hold space.

  3. The $!d deletes the second line of input. Because it is deleted, sed does not display it.

3.

The sed utility reads the third line of input (The third.) into the Pattern space.

  1. The 2,$G adds what is in the Hold space (The second line.NEWLINELine one.) to the Pattern space. The Pattern space now has The third.NEWLINEThe second line.NEWLINELine one.

  2. The h copies what is in the Pattern space to the Hold space.

  3. The $!d deletes the contents of the Pattern space. Because there is nothing in the Pattern space, sed does not display anything.

...

8.

The sed utility reads the eighth (last) line of input into the Pattern space.

  1. The 2,$G adds what is in the Hold space to the Pattern space. The Pattern space now has all the lines from new in reverse order.

  2. The h copies what is in the Pattern space to the Hold space. This step is not necessary for the last line of input but does not alter the program's output.

  3. The $!d does not process the last line of input. Because of its address the d instruction does not delete the last line.

  4. The sed utility displays the contents of the Pattern space.




A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net