Understanding sed command with example -Part 1

sed is a *nix stream editor command with the ability to filter and transform input text files line by line. sed command works very well with awk command and they are used together by System Administrators to simplify day to day activities. The syntax for sed command is
sed [options] commands <input-file>
One thing that distinguishes sed from other editors are its ability to filter text in a pipeline. In this first of the two post series on sed we will cover 3 of the most often used commands. They are
  1. Print (p) command
  2. Quit (q) command
  3. Delete (d) command
Before going into the details lets create a file name example.txt. The rest of this blog will be explained based on this file. The content of the file is given below
cat example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6

sed by default prints the entire content of a file, just like cat command. For example the below command will print contents of example.txt file line by line.
sed '' example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6

print (p) command

sed used with command p will print the input stream on the screen. sed if not used without -n option will duplicate the input. i.e sed will print each line twice as shown in the  below example
sed p example.txt
Sample Output:
This is line 1
This is line 1
This is line 2
This is line 2
This is line 3
This is line 3
This is line 4
This is line 4
This is line 5
This is line 5
This is line 6
This is line 6
By using p (print) command with the option -n we can suppress the automatic printing. i.e when used with -n, sed will only print if there is an explicit print request. The below example shows how to use the option -n with p (print) command. Also note that the duplicate lines are now missing
sed -n p example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
You might have noticed that the default output of sed command and the above output with print command are the same. So what is the need for print command? The answer is that print command lets you choose what to print on the screen. Given below are some sed examples to print a single or range of lines using line numbers are patterns.

#1) To print the third line of the file
sed -n 3p example.txt
Sample Output:
This is line 3


#2) To print a range, 3rd,4th and 5th line, execute
sed -n 3,5p example.txt
Sample Output:
This is line 3
This is line 4
This is line 5


#3) Similarly to search a pattern and print the corresponding output, execute
sed -n '/line 3/p' example.txt
Sample Output:
This is line 3


#4) To search and print a range using patterns, execute
sed -n '/line 3/,/line 5/p' example.txt
Sample Output:
This is line 3
This is line 4
This is line 5


#5)To print lines from line number 3 to last line, execute
sed -n '3,$'p example.txt
Sample Output:
This is line 3
This is line 4
This is line 5
This is line 6


quit (q) command

the quit command will print from the input, line by line until a certain line or a pattern  is matched and will quit printing the rest of the lines once the condition is met. Unlike print command, quit command is incapable of handling ranges.

#6)To display the first four lines of the input file and will abort printing after line four.
sed 4q example.txt
Will print the first 4 lines and will quit printing afterwards.
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4


#7)To quit command works with patterns also. The below example will print the contents of the file example.txt until a pattern "line 4" is reached and will quit printing afterwards
sed '/line 4/q' example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4



delete (d) command

Delete command is executed with certain conditions. When the conditions matches sed will remove a line or a range of lines from the input and will print the rest of the lines. The condition can be a single or range of line number/pattern.

#8)To delete line 5 and display the rest of the contents from example.txt, execute
sed 5d example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 6


#9) To display contents of example.txt except lines 2,3 and 4, execute
sed 2,4d example.txt
Sample Output:
This is line 1
This is line 5
This is line 6


#10) To delete line containing the pattern 'line 5' and display the rest of the contents from example.txt, execute
sed '/line 5/d' example.txt
Sample Output:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 6


#11)To display contents of example.txt except lines 2,3 and 4, execute
sed '/line 2/,/line 4/d' example.txt
Sample Output:
This is line 1
This is line 5
This is line 6

In the next part of the series we will discuss about the substitute and insert command with few examples. For more details on sed see the man pages

Comments

Popular posts from this blog

Understanding awk command with examples

How to install a Software in Linux