ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In my system , there are text files will be generated monthly , the file name begins with xxx , then year , month ( for example xxxxx201310.txt means Oct 2013 )
I have below command to count how many abc in the month , but it only count the number in this month .
NUMBER=$(cat xxxxx201310.txt |grep -c -s "abc" )
Could advise if I would like to have the following report , it shows the no. of abc for each month , and the total no. of abc , how to make it ? very thanks
no of abc in Oct 2013 = 111
no of abc in Nov 2013 = 222
no of abc in Dec 2013 = 333
"
"
"
Total no of abc = 999
first, you do not need to use cat, grep -c -s abc xxxxx201310.txt is enough
next, grep can handle more files, just try: grep -c -s abc xxxxx*.txt
and finally you need a small script to sum up the numbers and print a formatted output.
Can you do that yourself?
In year 2013 month 09 string abc was seen 1 times
In year 2013 month 09 string def was seen 1 times
In year 2013 month 10 string abc was seen 2 times
In year 2013 month 10 string ghi was seen 2 times
In year 2013 month 10 string jkl was seen 1 times
In year 2013 month 11 string abc was seen 4 times
In year 2013 month 11 string def was seen 1 times
In year 2013 month 11 string mno was seen 1 times
In year 2013 month 11 string pqr was seen 1 times
Note: I tried (but failed) to do this with an awk one-liner, using an array and asorti. I'd love to see how this could be done. Gurus?
Daniel B. Martin
Last edited by danielbmartin; 10-30-2013 at 11:20 AM.
Reason: Improved code
Total=0
MyString="abc"
for File in *.txt;do
Count=$(awk 'BEGIN{C=0}{for (i=1;i<=NF;i++) if ( $i ~ /'${MyString}'/ ){C++}} END{print C}' "$File")
echo "No. of \"${MyString}\" in $(date "+%b %Y" --date="${File:4:6}01") = $Count"
Total=$(( $Count + $Total ))
done
echo "Total No. of \"${MyString}\" = $Total"
I used this to test
Code:
In my system , there are text files will be generated monthly , the file name begins with xxx , then year , month ( for example xxxxx201310.txt means Oct 2013 )
I have below command to count how many abc in the month , but it only count the number in this month .
NUMBER=$(cat xxxxx201310.txt |grep -c -s "abc" )
Could advise if I would like to have the following report , it shows the no. of abc for each month , and the total no. of abc , how to make it ? very thanks
no of abc in Oct 2013 = 111 no of abc in Nov 2013 = 222 no of abc in Dec 2013 = 333
"
"
"
Total no of abc = 999
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.