![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple Find file Script..... | elbombillo | Shell Programming and Scripting | 6 | 02-02-2009 08:49 PM |
| simple shell - how to get a parameter typed in a shell script | cmitulescu | Shell Programming and Scripting | 4 | 01-09-2009 08:45 PM |
| simple script detect to find OS version/flavour | fed.linuxgossip | Shell Programming and Scripting | 13 | 08-14-2008 12:04 AM |
| Linux Shell Question: how to print the shell script name ? | meili100 | UNIX for Dummies Questions & Answers | 3 | 07-01-2008 01:55 PM |
| Script to look for data in a file (not that simple) ... | gseyforth | Shell Programming and Scripting | 6 | 02-16-2006 04:03 PM |
| View Poll Results: x | |||
| x |
|
0 | 0% |
| x |
|
1 | 100.00% |
| Voters: 1. You may not vote on this poll | |||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Simple shell script to find and print data
Hi,
I have a log file containing data on emails sent. Looks a bit like this for one email: Code:
Content-Type: text/plain; charset="UTF-8" Date: 12 Jun 2008 14:04:59 +0100 From: from@email.com Subject: xcf4564xzcv To: recip@email.co.uk Size = 364 Jun 12 14:04 smtp_234sldfh.tmp Code:
1,recip@email.co.uk,1,,,1,xcf4564xzcv,1,12 Jun 2008,14:04:59,1,,364 Code:
open(READLOGFILE, "C:\\temp\\email.log") or die("Failed to open file");
open(WRITELOGFILE, ">>C:\\temp\\emailstats.log") or die("Failed to open file");
$numRecords = 0;
$emailSize = 0;
$foundSize = 0;
$foundEmail = 0;
$foundSubject= 0;
$foundDate = 0;
while($line = <READLOGFILE>) {
if($line =~ /^Subject\:\s(\S*)/ ) {
$subject = $1;
$foundSubject = 1;
}
if($line =~ /^Date\:\s(.*)\s(\d\d\:\d\d\:\d\d)\s(.*)/) {
$date = $1;
$foundDate = 1;
$time = $2;
}
if($line =~ /^To\:\s(\S*\@\S*)/ ) {
$to = $1;
$foundEmail = 1;
}
if($line =~ /^Size\s\=\s(\d*)/) {
$foundSize = 1;
$emailSize = $1;
}
if(($foundSubject + $foundEmail + $foundSize + $foundDate) == 4) {
print WRITELOGFILE "1,".$to.",1,,,1,".$subject.",1,".$date.",".$time.",1,,".$emailSize."\n";
$numRecords = $numRecords + 1;
$totalSize = $totalSize + $emailSize;
$emailSize = 0;
$foundSize = 0;
$foundEmail = 0;
$foundSubject= 0;
$foundDate = 0;
}
}
print "\nProcessing Complete";
Last edited by terry2009; 09-22-2009 at 06:45 AM.. Reason: code tags, PLEASE! |
|
||||
|
Assuming the headers appear every time, you can use multiple calls to 'grep' to output one file for each field, and then use 'paste' to merge them, i.e.,
Code:
grep "^Subject" file.txt > subjects.out grep "^To" file.txt > tos.out paste -d"\t" subjects.out tos.out Code:
SUBJECT=
FOUND_SUBJECT=0
cat file.txt | while read line
do
SUBJECT=`echo $line | grep "^Subject"`
if [ $SUBJECT ]
FOUND_SUBJECT=1
# etc
. . .
echo ...
# reset vars
done
echo "Processing complete"
|
|
||||
|
Hi,
Thanks for you post... but could you make this idiot proof for me please, never used UNIX in my life. 1. How would I get 12 Jun 2008 from the line: Code:
Date: 12 Jun 2008 14:04:59 +0100 Code:
/^Date\:\s(.*)\s(\d\d\:\d\d\:\d\d)\s(.*)/ Terry |
|
||||
|
Not sure about grep on aix. Check for a -P option. On RHEL 5, this puts grep into Perl mode, which would presumably allow you to keep your regexes intact.
Otherwise, it's very similar. Use quotes to shell-proof your patterns. Drop the slash delimiters. Since it's a log, you can probs just grep on the keyword at the start of the line, i.e., grep "^Date: " or you could do grep "^Date: [0-9][0-9]:" etc. To get positional vars out of a line you can use awk, I think like this: echo $var | awk '{ print $1 $3 }' Truth be told, though, if you can stick with perl, it's much easier--made for this kind of task. ---------- Post updated at 08:19 AM ---------- Previous update was at 08:17 AM ---------- also, if you're really a newb, you may not know about the man pages. Type 'man awk' or 'man grep' at the command-line. and while you're at it, type 'which perl' . If you get a path to perl, use it. |
|
||||
|
Nah, client doesn't allow use of perl... Its not one of their strategic software, therefore hasn't got security clearance! Good chance to learn some shell though.
Thanks for you're help, I'll get testing. I'm going to try these: Code:
grep "^To" email.log > recipients.out|awk '{print $2}'
grep /^Subject\:\s(\S*)/ email.log > subjects.out
any ideas how I can paste all the outputs together to get it to look like this: Code:
1,recip@email.co.uk,1,,,1,xcf4564xzcv,1,12 Jun 2008,14:04:59,1,,364 ---------- Post updated at 03:12 PM ---------- Previous update was at 01:35 PM ---------- Resolution for anyone who needs it in future... Code:
grep "^To" email.log |awk '{print "1,"$2}' > recipients.out ; grep "^Subject" email.log |awk '{print ",1,,,1,"$2}' > subjects.out ; grep "^Date" email.log |awk '{print ",1,"$2" "$3" "$4}' > date.out ; grep "^Date" email.log |awk '{print ","$5}' > time.out ; grep "^Size" email.log |awk '{print ",1,,"$3}' > size.out ; paste recipients.out subjects.out date.out time.out size.out > emailstats.txt ; rm recipients.out ; rm subjects.out ; rm date.out ; rm time.out ; rm size.out
|
![]() |
| Bookmarks |
| Tags |
| awk, grep, perl, sed, shell |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|