Print or delete a range of lines in Perl

Home > Search
  by

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

Line 1
Line 2
Line 3
Line 4
Line 5

 

To print text between Line 2 and Line 4.

/usr/bin/perl -ne 'print if m#Line 2#i .. m#Line 4#i' file1.txt
Line 2
Line 3
Line 4

 

To delete Line 2 through Line 4.

/usr/bin/perl -ni -e 'print unless m#Line 2#i .. m#Line 4#i' 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