10 Basic examples of SED to perform per line action (print/delete)

In this article I will show you various sed expressions which can be used and combined to print or delete specific line from a file

IMPORTANT NOTE:

My examples provided are only to print the action performed, to use the same for delete action remove '-n' and replace 'p' with 'd' on individual command
To save the output to a new file after performing delete action use below syntax

# sed -e 'your regex' /old/file > /new/file2

To save the action in the same file use below syntax

# sed -i 'your regex' /path/to/file

Our sample file

# cat /tmp/file
one
two
three
four
five
six

 

1. Print the first line from a file

Below command will print the first line from the file

# sed -ne '1p' /tmp/file
one

 

2. Print the last line from a file

In regex "$" means last hence the same can be used to print the last line
Below sed regex can be used

# sed -ne '$p' /tmp/file
six

 

3. Print specific line number

In my sample file I have six lines so lets try to print 2nd line
Use the below syntax to only print 2nd line

# sed -ne '2p' /tmp/file
two

Below regex will print only the 4th line

# sed -ne '4p' /tmp/file
four

 

4. Print multiple line based on line number

In the above example we printed single line but the same can be done if you have the list of line numbers where you want to perform your work.
To print multiple line using line numbers use ";" (semi colon) to do the work as shown in below example
You can also print the 2nd and 4th file numbers using the below command

# sed -ne '2p;4p' /tmp/file
two
four

The same can be done using the below syntax for 'n' no of line numbers

# sed -ne '2p;5p;7p' /path/to/file

Here I am printing the 2nd,5th and 7th line
 

5. Print lines between specific line numbers from a file

In the above example we are deleting/printing specific line numbers but what if you wish to Print lines between two line numbers
In the above command semi-colon was used to print specific line numbers, but to print between a range use comma as shown in below example
Using below sed regex you can print the content between 'two' and 'four'

# sed -ne '2,4p' /tmp/file
two
three
four

 

6. Print all other lines except for the provided line number

Here we would like to print all the rest of the content of our file instead the line numbers which we want to save/print. Instead of using 'p' we will use '!p' which should do the work.
Print the content of the file excluding the content between 2nd and 4th line

# sed -ne '2,4!p' /tmp/file
one
five
six

Next print all line except first line

# sed -ne '1!p' /tmp/file
two
three
four
five
six

Next print all line except last line

# sed -ne '$!p' /tmp/file
one
two
three
four
five

 

7. Print all lines matching a pattern

Below example can help you print a(list of) specific line which matches a pattern, for this eg the pattern we are searching os 'two'

# sed -ne '/two/p' /tmp/file
two

 

8 Print all the lines between two matching patterns

In our sample file I would like to print the content between 'one' and 'four', so our sed must perform the required action between these two matching patterns
Similarly to print the same

# sed -ne '/one/,/four/p' /tmp/file
one
two
three
four

 

9. Print 'n' number of lines after a match

Use '+n' to print 'n' number of lines after a match using below syntax
If you wish to print the match and one additional line following the match use below command

# sed -ne '/one/,+1p' /tmp/file
one
two

Similarly if you wish to print 2 lines following the match

# sed -ne '/one/,+2p' /tmp/file
one
two
three

 

10. Print all the lines which has numerical value

Here lets change our sample file to below

# cat /tmp/file
one 1
2 two
three 2003
4 four
five 55
six 666

Here firstly print all the lines which starts with numerical value

# sed -ne '/^[0-9]/p' /tmp/file
2 two
4 four

Print all lines with has two or more numerical value only

# sed -ne '/[0-9][0-9]/p' /tmp/file
three 2003
five 55
six 666

Print all lines which ends with numerical value

# sed -ne '/[0-9]$/p' /tmp/file
one 1
three 2003
five 55
six 666