Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete lines matching a certain pattern in Perl?

Tags:

perl

I'd like to do something similar to sed in Perl, namely be able to delete lines matching a certain pattern.

Given this input:

abcd
edfd
abcd
derder
abcd
erre

I want to remove the lines containing bc. How can I do this?

like image 201
xyz Avatar asked Dec 07 '11 15:12

xyz


2 Answers

I had to use double quotes on Windows:

perl -ne "print unless /bc/" file
like image 113
xyz Avatar answered Nov 14 '22 23:11

xyz


This is a FAQ.

How do I change, delete, or insert a line in a file, or append to the beginning of a file?

If you're programming in Perl then it's well worth taking a couple of hours to familiarise yourself with the FAQ.

like image 45
Dave Cross Avatar answered Nov 14 '22 22:11

Dave Cross