Operating System - HP-UX
1753415 Members
7289 Online
108793 Solutions
New Discussion юеВ

To remove lines using perl

 
Calvin Tan Tjin Wei
Occasional Contributor

To remove lines using perl

Hi all,

I am trying to remove line from a while when i have search for a particular sting.
i.e.

&123&123&12314215&&abc123
&123&123&12314215&hour&abc112
&123&123&12314215&&abc115
&123&123&12314215&hour&abc113
&123&123&12314215&&abc115
&123&123&12314215&&abc115

i would want to remove the lines which contains the whole string which contains hour and the results would be

&123&123&12314215&&abc123
&123&123&12314215&&abc115
&123&123&12314215&&abc115
&123&123&12314215&&abc115
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: To remove lines using perl

Hi Calvin:

# perl -ne 'print unless /&hour/' file

...or in a 'while' loop:

# perl -e 'while (<>) {print unless /&hour/}' file

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: To remove lines using perl

though the suggested

$ perl -ne'/&hour/ or print' file.dta

is fine, it is much easier with grep

$ fgrep -v '&hour' file.dta

There is a right tool for every problem. In many an occasion that would be perl, but imho, here it is not

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Doug O'Leary
Honored Contributor

Re: To remove lines using perl

Hey;

Both previous posts are correct. I would probably use perl if I wanted to change original file or a 'grep -v' if I was piping the output to something else.

perl -i -nle 'print unless (/&hour/)' ${file}

would edit the file in place.

grep -v '&hour' | while IFS=& read ...

if you were going to do something with the output...

That's the nice thing about UNIX; there's usually several 'right' answers - just depends on what you want to do.

Doug O'Leary

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
James R. Ferguson
Acclaimed Contributor

Re: To remove lines using perl

HI (again):

It occurs to me (since you problem description is so nebulous and abstract) that perhaps you want to perform some processing in the loop but skip over some records:

...
while (<>) {
...
next if /&hour/; # resume reading next record, if any
...
}

Regards!

...JRF...