Solved

Shell - count number of lines

Posted on 2014-10-27
6
106 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
Comment Utility
perl -lne '$count{$1}++ if /IMSS=(\d+)/; END{ print "$_\t$count{$_}" for keys %count;}' text.file
0
 

Author Comment

by:joaotelles
Comment Utility
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
Comment Utility
/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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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
Comment Utility
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
Comment Utility
Tks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

11 Experts available now in Live!

Get 1:1 Help Now