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 every line in file1.txt that contains "Line 1". Typically, you would use grep for this kind of filtering.

~]# sed -n '/Line 1/ p' file1.txt
Line 1

 

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

~]# sed -n '/Line 2/, /Line 4/ 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

 



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