Print or delete a range of lines using sed

Home > Search
  by

Let's say file1.txt contain the following text.

Line 1
Line 2
Line 3
Line 4
Line 5

 

The following sed statement can be used to print the text between Line 2 and Line 4.

~]# sed -n '/Line 1/, /Line 3/ p' file1.txt
Line 2
Line 3
Line 4

 

The following sed statement can be used to delete the text between Line 2 and Line 4.

~]# sed -i '/Line 1/, /Line 3/ d' file1.txt
Line 1
Line 5

 

This could also be done in Perl.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments