CIT052 Index > Notes 4

Notes 4

Chapter 5

Section 5.3

Up through section 5.9.5, the book’s examples each do a single sed command. This is not the same as a shell command! If you type this line in the shell:

sed -n '1,5p' datafile

The whole line is a “shell command” which invokes the sed program and asks it to perform the sed command 1,5p. This is a subtle but significant distinction.

Section 5.4

Again, a terrible couple of examples in the text and the second line in example 5.2. It is a good idea to be able to change the normal / delimiter, but why on earth would you change it to a letter? That just makes it hard to read. Here's a better way to do the second example:

sed -n '\|12/10/04|p' datafile # change delimiter to vertical bar

Section 5.5

To see an example of addressing where you have starting and ending lines as patterns, see example 5.20 on page 140. For an example of mixed addressing, where you start on a line number and end with a pattern, see example 5.21.

Section 5.9.11

Example 5.29 shows the use of curly braces to execute a group of sed commands when you get to a certain line or lines, as describe on page 130.

Another use of curly braces, also as described as on page 130, is to nest line ranges. For example, let’s say you want to delete any lines that have the word REMOVE on them, but only on lines 1 through 10 of a file example.txt. You do it this way:

sed -n '1,10{/REMOVE/d}' example.txt

Section 5.10

To emphasize: when you run a sed script on a file, sed will take one line from the file and then execute all the commands in the script on that line. Some of them may not do anything; for example, line 11 won’t be affected by a command with a line range 20,25, but the input line will be tested against that command.