Here is a code example for del.pl:syslog before the following command: Oct 7 08:30:01 deneb sendmail[13183]: [ID 801593 mail.info] g97CU1r13 +183: from= cmjohnso, size=252, class=0, nrcpts=1, msgid=<200210071230.g97CU1r1318 +3@deneb.us lec.net>, relay=cmjohnso@localhost Oct 7 08:30:01 deneb sendmail[13185]: [ID 801593 mail.info] g97CU1r13 +183: to=cm johnso, ctladdr=cmjohnso (10052/1), delay=00:00:00, xdelay=00:00:00, m +ailer=loca l, pri=120252, relay=local, dsn=2.0.0, stat=Sent % perl -i -p del.pl /var/log/syslog syslog after the above command: cmjohnso@deneb$ cat syslog Oct Oct
Remember, when you are in the del.pl program you are only working on one line at a time, the 'perl -i -p <progrname> <file>' line reads each line of the file, in this case /var/log/syslog and applies the code in the program to each line, printing whatever you print in the code as well as the final content of $_. That is why I set $_ to '', so it did not print back to my file. It would have been just as east to set $_ to what I wanted to be printed to the file and not done the print myself.#!/usr/bin/perl -w use strict; my @line = split(" ",$_); print "$line[0]\n"; $_ = '';
You could write a program somewhat similar to this that evaluated each line and if it needs to be deleted from the file just set $_ to '' otherwise leave $_ alone and it will go back to your final file as it was when it entered the loop.
Here is the text from the "Perl Black Book" by Coriolis:
"Perl lets you make changes to files in-place--that is, make changes to the file directly, without having to explicitly read it in and write it out. To edit files in-place, you use the -i switch with Perl; this switch specifies that files processed with the <> construct to be edited in-place. ....... Note also the -p switch; this switch makes Perl use a while (<>) and print loop around your script to print the changed text back to the file."
In reply to Re: cleaning up logs
by gnu@perl
in thread cleaning up logs
by ddrumguy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |