Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell - count number of lines

Posted on 2014-10-27
6
Medium Priority
?
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 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

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.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
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…

721 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