Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strip lnes from log files 1

Status
Not open for further replies.

moonhead

Technical User
Apr 24, 2002
11
GB
Dear all
If i have a text file that contains a number of lines that i dont wan't
can i remove the lines based on a generic word within the file
example file:

line apples and pears
line apples and pears
line chips and cheese with a selection of chicken
line chips and cheese with a selection of chicken
line apples and pears
line apples and pears
line apples and pears

i want to remove the line that contains "apples"
and just have the lines:
line chips and cheese with a selection of chicken

thanks
MoonHead("_")


 
perl -pi".orig" -e "$_ = '' if /apples/" file_to_parse.txt

this will also create a backup with a .orig extension.

--jim
 
Hi Jim
is this supposed to be executed in a script or on the command line
maybe i have lost the plot
but i cant seem to get it to work

i am opening the original file
creating a filehandle
opening my output file
creating a filehandle

doing the search and replace removing the keyword line (apples)
printing the results to the output file

how does your code fit in?

many thanks
Andy




 
Sorry about that. Copy and paste that line onto a command line (MS-DOS).

If your on a *nix box, you'll probably need to change the quotes a bit. Like to single quotes instead of double. Just toggle the quotes.

By typing that short line on a command line, verbatim. One thing that maybe difficult to see here, is that in
Code:
"$_ = '' if /apples/"
The two characters after the = sign are two single apostrophes, rather than one double quote.
This line would be equivalent:
Code:
"$_ = undef if /apples/"
Just to clear that up if there was any confusion.

Now to explain:
The magic here is in the command line switches -pi and -e.

What they do:
-p causes perl to assume that your script is within a while(<>){ #script here } loop.

-iEXTENSION tells perl that the files processed by the <> construct are to be edited in place. It creates a back up file with the EXTENSION

-e PERLCODE will make perl not look for the programs file name in the argument list. Instead PERLCODE is the code that Perl executes.

And after the single block of code, that's the file name that we are feeding to the Perl script to be processed by the <> loop, and the snippet you provide.

They don't say perl is a powerful text parser for nothin. Things like this exist just for common situations like yours, where you want to go over a file line by line and make edits in place - and back up the original document.

Any more ?'s ?

--jim
 
Jim
thats great, thanks very much!
i will give it a go.
Andy
 
Is there a way to obtain just the opposite results, i.e., delete everything that doesn't match /apples/ ?

TIA!
 
Thanks much!!! That was exactly what I was looking for. I'm startin' to like this perl thing...

- maurie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top