[Za-pm] Script

Oskar Pearson oskar at qualica.com
Wed Jun 4 06:17:18 CDT 2003


On Wed, Jun 04, 2003 at 08:33:33AM +0200, Bartho Saaiman wrote:
> I want to filter a log file that I currently have to manipulate
> manually. So I was thinking to myself that this would be nice if I could
> do this with Perl. If it would be easier in bash, suggestions would also
> be welcome. So here is the scenario:
> 
> Output of log file (sms.log):
> <snip>
> [Fri May 23 11:02:12 SAST 2003] SMS  user at domain.co.za sent 43
> characters to 271234567891
> [Fri May 23 18:16:02 SAST 2003] SMS  "Some User" <user at domain.co.za>
> sent 150 characters to 271234567891
> [Sat May 24 12:51:37 SAST 2003] SMS  "Some User" <user at domain.co.za>
> sent 151 characters to 271234567891
> [Mon May 26 15:16:00 SAST 2003] SMS  Some User <user at domain.co.za> sent
> 142 characters to 271234567891
> </snip>
> 
> So the first problem is that the user (Some User) detail is logged in
> three different ways. I am also only interested in the email addres as I
> can use this to do accountting with. I am currentl using bash like this:
> 
> [bartho at hercules bartho]$ cat  smslog |grep "May"| grep "2003" |awk \
> 	'{print $8, $9, $10}'
> user at domain.co.za sent 47
> "Some User" <user at domain.co.za>
> Some User <user at domain.co.za>
> 
> Now this is where my problem starts. I probably need to use regular
> expressions to feed it the month and the domain. The year I could
> probably use in a regex too, but this doesn't change to often. Then I
> ned to send this to a clean file only containing the emails that this
> originated from. I do not need to sort them as unique since I have to
> add them up, similar to 'wc -l'

I'm not sure I understand the output you want, I'm afraid. I'm also
assuming that the log lines above weren't meant to be wrapped? (ie
they were one long line)

how about something like this:

oskar at core1:~$ cat t.txt
[Fri May 23 11:02:12 SAST 2003] SMS  user at domain.co.za sent 43 characters to 271234567891
[Fri May 23 18:16:02 SAST 2003] SMS  "Some User" <user at domain.co.za> sent 150 characters to 271234567891
[Sat May 24 12:51:37 SAST 2003] SMS  "Some User" <user at domain.co.za> sent 151 characters to 271234567891
[Mon May 26 15:16:00 SAST 2003] SMS  Some User <user at domain.co.za> sent 142 characters to 271234567891
oskar at core1:~$ cat t.pl
while (<STDIN>) {
        my ($date, $address, $characters, $cell) =
                $_ =~ /^\[(.+)\] SMS +(.+) sent (.+) characters to (.+)$/;
        print "$cell no, $characters chars (from $address at $date)\n";
}
oskar at core1:~$ perl t.pl < t.txt
271234567891 no, 43 chars (from user at domain.co.za at Fri May 23 11:02:12 SAST 2003)
271234567891 no, 150 chars (from "Some User" <user at domain.co.za> at Fri May 23 18:16:02 SAST 2003)
271234567891 no, 151 chars (from "Some User" <user at domain.co.za> at Sat May 24 12:51:37 SAST 2003)
271234567891 no, 142 chars (from Some User <user at domain.co.za> at Mon May 26 15:16:00 SAST 2003)
oskar at core1:~$

Oskar
--
Oskar Pearson <oskar at qualica.com>
Qualica Technologies (Pty) Ltd
web: http://www.qualica.com/



More information about the Za-pm mailing list