1830241 Members
10016 Online
109999 Solutions
New Discussion

Re: parse help needed

 
SOLVED
Go to solution
Chris H_3
Advisor

parse help needed

Hi all,
I have a router log file that I would like to parse. It contains 4 columns of IP's. I would like to sort them based on column 4, and also add a number to each row of how many occurances of that particular connection.
i.e
10.12.5.24 172.10.5.10 172.10.5.12 172.50.101.5
10.12.5.20 172.10.5.10 172.10.5.12 172.50.101.5
10.12.5.24 172.10.5.10 172.10.5.12 172.50.101.5
10.12.5.24 172.10.5.10 172.10.5.12 172.50.101.5
...
so there is one connection from 10.12.5.20 and 3 from 10.12.5.24, so the output would be

10.12.5.24 172.10.5.10 172.10.5.12 172.50.101.5 3
10.12.5.20 172.10.5.10 172.10.5.12 172.50.101.5 1
...
...

I started with an awk script, something like:
cat filename.txt | awk '{print $4" "$3" "$2" "$1} | sort | uniq

but it didn't look quite right, and I couldn't figure out how to generate the number of occurances, and my perl knowledge is about useless :(

Any help would be appreciated, and if more info is needed, let me know

Thanks!

CH
3 REPLIES 3
Martin P.J. Zinser
Honored Contributor

Re: parse help needed

Hi,

maybe not the greatest, but should do the trick :-)

$file = "temp.txt";

open(IN, $file);
while(){
chomp();
@words = split();
$srec = join(" ", $words[3], $words[0], $words[1], $words[2]);
$sarr{$srec} = $_;
$count{$_}++;
}
close(IN);

foreach $key (sort keys %sarr){
printf "%s %s\n", $sarr{$key}, $count{$sarr{$key}};
}

Hopefully ITRC does not mess this up completely.

Greetings, Martin
Francisco J. Soler
Honored Contributor
Solution

Re: parse help needed

Hi CH,

try this:

sort +4 -5 filename.txt | uniq -c | sort -r

and if you need the counter first:

sort +4 -5 filename.txt | uniq -c | sort -r | awk '{printf $2" "$3" "$4" "$5" "$1"\n"}'


Frank.
Linux?. Yes, of course.
Chris H_3
Advisor

Re: parse help needed

Oooo, that sort is slick, thanks Frank.

Actually, they both did the trick, so thank you both. Cool to see it from two ways.

Thanks again!

CH