Writing a script that compares two different files
Hi, I'm having trouble figuring out how to match to find matches in two different files when comparing timestamps. The fields I'm wanting to match up are in the format:
Jul 26 09:33:02 I have tried reading the file line by line and using awk '{print $1,$2,$3}' which only gets and stores the timestamp in one of the files. I've been looking around and saw this example: awk 'FNR==NR{!a[$3]++;next }{ b[$3]++ } END{ for(i in a){ for(k in b){ if (a[i]==1 && i ~ k ) { print i } } } }' $FILE $FILE2 Which sorta works but its way over my head at the moment. The two files can be found in your /var/log/syslog and /var/log/auth.log (using Ubuntu 11.04) Thank You |
Hi, welcome to LQ!
Quote:
Cheers, Tink |
Ah thanks. You know I reference this site a lot but I haven't actually posted anything here =) Anyways, my goal is to take an entry from syslog for instance:
Jul 26 11:35:44 bdouglas kernel: [70761.603498] usb 2-1.1.4: new high speed USB device using ehci_hcd and address 12 and an entry from auth.log: Jul 26 13:17:01 bdouglas CRON[11888]: pam_unix(cron:session): session closed for user root And compare the contents of both log files by their timestamps. If their timestamps match the exact hour:min:sec, I want both entries printed. |
I don't know if this is less over your head, but... (I don't know awk, unfortunately, but I can understand bash):
Code:
while read line; Hope this helps, |
Hmm. That is pretty straight forward. I like what I see so far. I can do an awk $3 which grabs the 3rd field not separated by spaces. I like how you feed a file into another file, I was getting the impression you were overwriting the auth.log file but the alligator is pointing the other way. I'll play with this and see what I can come up with. I would also like to see other variations of doing this since it looks a bit long winded. Thank You
|
And an "awk" method ..
Code:
FNR==NR{ |
Where does file 1 and file 2 go exactly. Having a hard time visualizing. Thanks
|
Here's a Perl version; easy to read regexes and very flexible.
It assumes no more than one match per second, re-opens the 2nd file for each rec in first file, much like post#4 soln. You could amend it to actually compare the dates in meaningful terms, ie so it knows when its passed the date/time in the 2nd file and doesn't waste time checking further recs; otoh this would mean checking all recs until a match or EOF... Perl is very quick, so you prob don't need to worry about date matching. Code:
#!/usr/bin/perl -w |
Quote:
Code:
awk -f munky file1 file2 |
Ah gotcha gotcha I'll take a look at it thank you for the reply
|
All times are GMT -5. The time now is 01:51 AM. |