We use sed command mostly to do operations like search and replace. We can also use the sed command just like a grep to print only the matching lines.
The syntax of sed command to print matching lines is shown below:
The syntax of sed command to print lines using line addresses is:
Examples:
Create the following file in your unix or linux machine:
1. Display lines matching a pattern
The following examples prints the lines that contain the pattern virtual using sed and grep commands.
2. Print lines with specific line addresses.
The below sed command displays the line with number 2:
The syntax of sed command to print matching lines is shown below:
sed -n '/pattern/p' filename
The syntax of sed command to print lines using line addresses is:
sed -n 'Np' filename
Examples:
Create the following file in your unix or linux machine:
unix virtual server linux virtual hosting fedora os
1. Display lines matching a pattern
The following examples prints the lines that contain the pattern virtual using sed and grep commands.
> grep 'virtual' filename > sed -n '/virtual/p' filename
2. Print lines with specific line addresses.
The below sed command displays the line with number 2:
sed -n '2p' filename
Post a Comment