ola
ola

Reputation: 912

Shell Script Count String Occurrence

I am working on a project and need help figuring out how to do a task. I am going to be given a log file, and I need to parse through and count the amount of times something occurs at a certain minute.

For example, if I have a txt file:

Line 3: 0606 221241  successfully copied to **
Line 5: 0606 221242  successfully copied to **
Line 7: 0606 221242  successfully copied to **
Line 9: 0606 221342  successfully copied to **

I want to know how many times something was successfully copied at 2212 So far, I have the following code seperating only lines that have been successful copied and getting the dates seperate...

grep "successfully copied to" Text.log >> Success.txt
awk '{print ($1,  $2)}' Success.txt > datesAndTimes.txt

This gives me

0606 221241
0606 221242
0606 221242
0606 221243

For some reason, I am having trouble figuring out how to count the amount of times each specific time (ex. 0606 2212) occurs. occurs. I only need the minutes, not the seconds (the last two digits of the second column) Eventually I want a log/txt file that says:

0606 2212 3
0606 2213 1

and so on....

If any one has any ideas, I'm having a bit of a brain fart. Thank you in advance!

Upvotes: 2

Views: 854

Answers (1)

anubhava
anubhava

Reputation: 785128

You can get this in awk one liner:

awk '{mm=substr($4, 1, 4); cnt[$3 " " mm]++} END{for(a in cnt) print a " " cnt[a]}' Text.log

Live Demo: http://ideone.com/w2h64d

Upvotes: 2

Related Questions