Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell - count number of lines

Posted on 2014-10-27
6
Medium Priority
?
120 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 85

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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
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 85

Accepted Solution

by:
ozo earned 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

782 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