2

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!

2
  • 2
    You might be interested in uniq -c. Commented Jun 7, 2013 at 18:29
  • @squiguy that could work! I couldn't think of the command and/or the search I needed to find it. Any advice on how to get the substring of just the first 4 characters of the second column? Commented Jun 7, 2013 at 18:32

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

15 Comments

would you mind explaining it a bit to me? I am newer to script and would like to learn instead of just copying!
sure I can explain. First I get 4 digits of $4 (timestamp) that constituted minute part. Then I create and increment an associative array cnt with the key $3 " " mm e.g. 0606 2212. Once END of file is reached this array is printed on stdout.
thank you, I understand. Is there anyway to order it in its original order? because it is all out of order this way
@user1998581, pipe the output into sort -nk1,2
we can get rid of the grep call too: awk '/successfully copied to/ {mn=...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.