Easy Sed examples to understand different sed commands.
Now lets jump on to some real time usage of ‘sed’ commands.
As already mentioned we will be using the below format to invoke sed.
sed -options ‘commands seperated by semicolon’ input_text_file
The input text fle that we are going to use for all the below examples is:
bash-3.2$ cat test.txt 1. leaf is green 2. sky is blue 3. snow is white 4. moon is white. 5. flower is yellow.
Provided below a brief description (from man page) of the commands used in the examples.
p - (lowercase p)Print the current pattern space. P - (Uppercase P)Print up to the first embedded newline of the current pattern space. d - Delete pattern space; start new cycle. q - Immediately quit the sed script without processing any more input. s/regexp/replacement/ - Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp. = - Print the current line number. $ - Match the last line.
Examples
1. How to print the total number of lines in a file using sed ?
bash-3.2$ sed -n '$=' test.txt 5 bash-3.2$
Explanation: ‘=’ prints the line number. $ specifies sed to print the line number only for the last line. -n disables autoprint, so that pattern space is not printed
for every cycle.
2. How to select only the first n number of lines using sed ?
bash-3.2$ sed -n '1,3p' test.txt 1. leaf is green 2. sky is blue 3. snow is white bash-3.2$
Explanation: Here 1,3 is an address condition. It specifies sed to print pattern space ‘p’ only for lines 1 to 3.
– How to print only first n number of lines using sed – alternate way ?
bash-3.2$ sed '3q' test.txt 1. leaf is green 2. sky is blue 3. snow is white bash-3.2$
Explanation: Here autoprint is enabled by removing the -n (silent) option. ‘3q’ quits sed in the third line. As autoprint is enabled, sed prints the pattern space till line 3.
3. How to select or print only odd lines using sed ?
bash-3.2$ sed -n 'p;n' test.txt 1. leaf is green 3. snow is white 5. flower is yellow bash-3.2$
Explanation: Here we disable autoprint using -n option. Then print the first line using ‘p’. Then ‘n’ replaces the 1st line in the pattern space with the 2nd line. 1st cycle is completed.
In the next cycle, line 3 will be read into the pattern space and printed using ‘p’. Then ‘n’ replaces line 3 in pattern space with line 4. 2nd cycle is completed. This continues till the last line and only odd lines get printed.
– How to select or print only odd lines using sed ? Alternate way – Using address conditions.
bash-3.2$ sed -n '1~2p' test.txt 1. leaf is green 3. snow is white 5. flower is yellow bash-3.2$
Explanation: Here 1~2 is the address condition which says print line 1, then move 2 lines; print 3rd line, then move 2 lines; print 5th line and continue till the end.
For printing all lines starting from say 4th line, we can use 4~1p with -n option.
4. How to select or print only even lines using sed ?
bash-3.2$ sed -n 'n;p' test.txt 2. sky is blue 4. moon is white bash-3.2$
Explanation: Here we disable autoprint using -n. Then ‘n’ replaces the 1st line in the pattern space with the 2nd line. Then print the second line using ‘p’. 1st cycle is completed.
In the next cycle, line 3 will be read into the pattern space, but ‘n’ replaces line 3 in pattern space with line 4. Then print the 4th line using ‘p’. 2nd cycle is completed. This continues till the last line and only even lines get printed.
– How to select or print only even lines using sed ? Alternate way – Using address conditions.
bash-3.2$ sed -n '2~2p' test.txt 2. sky is blue 4. moon is white bash-3.2$
Explanation: Here 2~2 is the address which says print line 2 and then move 2 lines then print 4th line. It continues the same till the end of file thus printing only even lines.
5. How to replace a particular word only in a particular line using sed ?
Replace ‘white’ to ‘Modified white’ only for line 3.
bash-3.2$ sed '3s/\(white\)/Modified \1/' test.txt 1. leaf is green 2. sky is blue 3. snow is Modified white 4. moon is white 5. flower is yellow bash-3.2$
Explanation: Here the number 3 at the start of the script specifies to modify line 3. The format of the substitution command is s/regexp/replacement/flags. The point to note here is the \1. If we want to use the matched pattern in the replacement we can put the pattern inside “\(” and “\)” and then refer it as \1 and \2 (till \9) in the replacement. Here \1 is nothing but the matched pattern “white”.
– Another example to show the usage of \1 and \2 in sed substitution command.
In the below example we search for the pattern ‘is white’ in the third line and print them in the replacement as \1 and \2.
bash-3.2$ sed '3s/\(is\)\(white\)/deleted: \1 and \2/' test.txt 1. leaf is green 2. sky is blue 3. snow deleted: is and white 4. moon is white 5. flower is yellow bash-3.2$
6. How to remove/delete the lines that contain a certain pattern?
To remove all lines that contain the word ‘white’.
bash-3.2$ sed -e '/white/d' test.txt 1. leaf is green 2. sky is blue 5. flower is yellow bash-3.2$
Explanation: The option -e enables autoprint of pattern space during each cycle. /white/ is an address condition that specifies only lines that contain the word white.
Then ‘d’ specifies deletion of the pattern space that matches the address condition. Thus all lines that contain the word ‘white’ are deleted.