Solved

Shell - count number of lines

Posted on 2014-10-27
6
108 Views
Last Modified: 2014-11-06
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
0
Comment
Question by:joaotelles
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40407566
perl -lne '$count{$1}++ if /IMSS=(\d+)/; END{ print "$_\t$count{$_}" for keys %count;}' text.file
0
 

Author Comment

by:joaotelles
ID: 40407590
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
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40407591
/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".
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40407595
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
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40407602
perl -lne '$c{$1}++ if /IMSS=(\d+)/; END{ print "$_\t$c{$_}" for sort{$c{$b}<=>$c{$a}}keys %c}'
0
 

Author Closing Comment

by:joaotelles
ID: 40427478
Tks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now