Sed and AWK: How do I count number of instances of an item in the output?
-
TL;DR The idea is : awk '{ IP[$1]++; } END { for(var in IP) print IP[var] } }' getline < sockstat | awk '{print $2 "@" $3}' | grep -v '^PROCESS@PID' I want to count the number of instance of every block in the output from -> sockstat | awk '{print $2 "@" $3}' | grep -v '^PROCESS@PID' Which looks like: ubuntu-geoip-pr@2382 chrome@2453 chrome@2453 chrome@2453 chrome@2453 chrome@2453 chrome@2453 chrome@2453 chrome@2453 rhythmbox@4759 rhythmbox@4759 rhythmbox@4759 Finally, I want the output to be: 1 8 3 This corresponds to the number of occurrences of each of the items in the previous output. Problem in full: The sockstat command outputs the info for some networking stats for the localhost. I first print out a single key from the second and third columns from the output (PROCESS and PID, respectively) in the form PROCESS@PID. Then, I want to calculate the frequency of each unique key from that output. One way to do this is to use the awk getline structure, but that seems works for files, and I have not been able to make it pull input directly from the above command. I do not want to use temporary files, as that takes away the elegance of the solution.
-
Answer:
Given where you posted this and your example input you should be able to use uniq with a flag to count the occurrences of each. Use man to find the right flag since I don't know it off hand and can't research it at the moment.
Ian Cordasco at Quora Visit the source
Other answers
like I've said on the comment, I think your question is badly phrased but just to answer what it says now, here's how you do it: sockstat | awk ' $3 ~ /^[0-9]+$/ { IP[$2 "@" $3]++ } END { for (key in IP) print IP[key] }' but you will want to know what key corresponds to each count so replace the print with: print key, IP[key]
Michael Cetrulo
Adding to Ian's answer, sort the input before you pipe it into uniq. cat filename | sort | uniq -c
Kevin Varkey
if you are looking for redundant lines: cat yourfile.txt | awk '{ c[$0]++ } END { for (i in c) print c[i],i }' | sort -nr but if you want to count at the word level: cat youfile.txt | awk '{for (i=1; i<=NF; i++) c[$i]++} END {for(i in c) print c[i],i}' | sort -nr
Ronald Loui
Related Q & A:
- How do I scope my final count variable properly?Best solution by siue.edu
- How to count number of occurrences of pattern within a string?Best solution by Stack Overflow
- How can I get the count in SQL?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How do I find the item number on a ebay item?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.