Link to home
Start Free TrialLog in
Avatar of joaotelles
joaotellesFlag for United States of America

asked on

Shell - count number of lines

Hi,

Im running solaris 10 and I would to know an efficient command to do the following:

I have the following line in a huge txt file that appears several times:
2014-10-27 17:01:21.710 [163] Debug      xxxxReceiverServlet            Incoming request url : http://otasnq-nmnet-nfce:7900/httpeventadapter/SP_xxx/xxxxAdapter/ST.TERMINAL_SWITCH query string ST.IMSS=310260484040375&ST.IMES=35396306042408


But the IMSI and the IMEI differs, for example another example of this line is:
2014-10-27 17:01:21.613 [163] Debug      xxxxReceiverServlet            Incoming request url : http://otasnq-nmnet-nfce:7900/httpeventadapter/SP_xxx/xxxxAdapter/ST.TERMINAL_SWITCH query string ST.IMSS=310260107550737&ST.IMES=35842005010112

But they are always in the same postion in the line...

So, how can I get the count of of dif. IMSS in a txt file of this... to have an output for example:

310260107550737 10 (10 times the number 310260107550737  appeared in different lines like the one above)
310260484040375   5(5 times)
.
.
and so on.

Tks,
J
Avatar of ozo
ozo
Flag of United States of America image

perl -lne '$count{$1}++ if /IMSS=(\d+)/; END{ print "$_\t$count{$_}" for keys %count;}' text.file
Avatar of joaotelles

ASKER

Tks! can you make it sort for the count number?

Now Im getting this:
310260775139739 3
310260009462568 2
310260678548652 3
310260741985944 6
310260117788407 3
310260560117207 6


Could it be this:
310260560117207 6
310260741985944 6
310260775139739 3
.
.


Tks in advance,
J
/usr/xpg4/bin/awk -F'ST.IMSS=|&ST.IMES' '{print $2}' file.txt |sort | uniq -c

(Will show the count on the left side)

or

/usr/xpg4/bin/awk -F'ST.IMSS=|&ST.IMES' '{A[$2]+=1} END {for(n in A) print n, A[n]}' file.txt

If sorting the output is a concern add " | sort" at the end:

/usr/xpg4/bin/awk -F'ST.IMSS=|&ST.IMES' '{A[$2]+=1} END {for(n in A) print n, A[n]}' file.txt | sort

In all cases above you can replace "/usr/xpg4/bin/awk" with "nawk".
Sort by count:

/usr/xpg4/bin/awk -F'ST.IMSS=|&ST.IMES' '{print $2}' file.txt |sort | uniq -c | sort -n

/usr/xpg4/bin/awk -F'ST.IMSS=|&ST.IMES' '{A[$2]+=1} END {for(n in A) print n, A[n]}' file.txt | sort -k2,n
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tks.