Perl one-liner: deleting a line with matching patterns

I am trying to remove a bunch of lines in a file if they match a specific pattern, which is a variable.

I am trying to remove a line that matches abc12, abc13, etc.

I tried writing a C shell script, and this is the code:

**!/bin/csh foreach $x (12 13 14 15 16 17) perl -ni -e 'print unless /abc$x/' filename end** 

This does not work, but when I use single-line without a variable (abc12), it works.

I'm not sure if something is wrong with the pattern matching, or if something else is missing for me.

+5
source share
1 answer

Yes, it is a fact that you use single quotes. This means that $x interpreted literally.

Of course, you also do this very inefficiently, because you process each file several times.

If you want to delete the abc12 lines in abc17 , you can do this at a time:

 perl -n -i.bak -e 'print unless m/abc1[234567]/' filename 
+8
source

Source: https://habr.com/ru/post/1237011/


All Articles