Operating System - HP-UX
1753767 Members
5613 Online
108799 Solutions
New Discussion юеВ

Re: perl (one line) search string and remove line from file

 
SOLVED
Go to solution
Billa-User
Regular Advisor

perl (one line) search string and remove line from file

hello,

i am not a perl expert and try in unix following perl one liner, hope correct.

example file with field seperator : remove_one_line.txt
1;line1_field2;1
2;line2_field2;2
3;line3_field2;3

export remove_string="line1_field2"

perl -i.old -ne 'print unless /$ENV{'remove_string'}/' remove_one_line.txt
3 REPLIES 3
Kees C
Visitor

Re: perl (one line) search string and remove line from file

Replace the single quotes aroung remove_string with double quotes and it will work:

perl -i.old -ne 'print unless /$ENV/"remove_string"}/' remove_one_line.txt

Note that if remove_string is something like "xyz", it will remove all lines that have "xyz" anywhere in the line, so not only
2;xyz;2
but also
3;uvwxyzABC;3
James R. Ferguson
Acclaimed Contributor
Solution

Re: perl (one line) search string and remove line from file

Hi:

So, your question is? Yes, the script will remove a matched line.

You can satisfy yourself by building a 'remove_one_line.txt' file and dropping gthe '-i.old' argument. When you are satisfied, and wish to update your file, add that back.

While the command works as you want, you can shorten things to:

# remove_string=line1_field2 perl -i.old -ne 'print unless /$ENV{remove_string}/' remove_one_line.txt

That is, there is no need to 'export'; simply declare the variable and value on the same line with whitespace (*not* a semicolon) before 'perl ...'.

Regards!

...JRF...
Billa-User
Regular Advisor

Re: perl (one line) search string and remove line from file

thank you very much, it work's perfect