Sorting and wc -l w.r.t seconds


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting and wc -l w.r.t seconds
# 1  
Old 02-11-2013
Sorting and wc -l w.r.t seconds

I have file with below data
Code:
00:00:00        
00:00:00
00:00:00
00:00:01
00:00:01
00:00:01
00:00:01
00:00:01
00:02:01
00:02:01
00:02:01
so on till
23:59:59

I want count of seconds for each hour and minutes

say for
Code:
00:00:00 its 3 and 00:00:01 its 5 and 00:02:01 its 3 and so on till 23rd hour

# 2  
Old 02-12-2013
Since the title of this thread includes "sorting", I assume that your real input is not already sorted by time like you sample input file. The following should do what you want:
Code:
awk '{  c[$1]++}
{printf("c[%s]=%d\n", $1, c[$1])}
END {   for(h = 0; h <= 23; h++)
                for(m = 0; m <= 59; m++)
                        for(s = 0; s <= 59; s++) 
                                if((k = sprintf("%02d:%02d:%02d",h,m,s)) in c)
                                        printf("%s %4d\n", k, c[k])
}' file

As always, if you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk. This script could be made considerably more efficient if your input is sorted by time.

For the given sample input, the output produced is:
Code:
00:00:00    3
00:00:01    5
00:02:01    3
23:59:59    1

# 3  
Old 02-12-2013
Thank you , I will check it out, but can you tell me what it does ?
# 4  
Old 02-12-2013
Quote:
Originally Posted by mirwasim
Thank you , I will check it out, but can you tell me what it does ?
Sorry. I thought it was pretty obvious. But, I see that I left in a debugging statement that probably confused things.

The corrected script is:
Code:
awk '{  c[$1]++}
END {   for(h = 0; h <= 23; h++)
                for(m = 0; m <= 59; m++)
                        for(s = 0; s <= 59; s++) 
                                if((k = sprintf("%02d:%02d:%02d",h,m,s)) in c)
                                        printf("%s %4d\n", k, c[k])
}' file

The first line sets up an array (c) that counts each occurrence of the timestamps in the first field of the file. (Normally, I would have used $0 here instead of $1, but your 1st line has trailing whitespace characters on the line.)

The rest of the lines execute once after the last line has been read from the input file. It creates a key (k) that is the hour, minute, and second for every second in a day (ignoring leap seconds) and if there was an entry in c for that time, it prints the time and the number of lines that had that time as the 1st field in the input file.
# 5  
Old 02-12-2013
for me its SUNOS
Code:
[wasim]$ cat try.ksh
nawk '{  c[$1]++}
END {   for(h = 0; h <= 23; h++)
                for(m = 0; m <= 59; m++)
                        for(s = 0; s <= 59; s++)
                                if((k = sprintf("%02d:%02d:%02d",h,m,s)) in c)
                                        printf("%s %4d\n", k, c[k])
}' $1

no output
Code:
[wasim]$ try.ksh part1.csv

# 6  
Old 02-12-2013
Quote:
Originally Posted by mirwasim
for me its SUNOS
Code:
[wasim]$ cat try.ksh
nawk '{  c[$1]++}
END {   for(h = 0; h <= 23; h++)
                for(m = 0; m <= 59; m++)
                        for(s = 0; s <= 59; s++)
                                if((k = sprintf("%02d:%02d:%02d",h,m,s)) in c)
                                        printf("%s %4d\n", k, c[k])
}' $1

no output
Code:
[wasim]$ try.ksh part1.csv

Do the contents of part1.csv look like the sample input file you supplied in the 1st posting on this thread? (It is unusual to use the .csv filename extension when there are no commas and only one field in your input file.)

Since you don't have a #! line at the start of your script, there is a small chance that this script will be run by ksh, but with this script on a Solaris system, it shouldn't matter.
# 7  
Old 02-12-2013
My Bad,

I did not apply it on correct file,
I need to separate time from it..

sorry for the confusion, will update you on successful execution
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Number of leap seconds

Is there a function call in std library or unit command that returns the number of current leap seconds? GG (4 Replies)
Discussion started by: NAVTime
4 Replies

2. Shell Programming and Scripting

How to delay the process for few seconds

Hi, In my shell script, (as per the requirement), I am creating few files, and the processes are launched parallelly . (by using "&" at the end of the command line). As per the logic, I need to remove these files as well, after creating. But, the problem is, due to parallel processing,... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

3. Shell Programming and Scripting

Convert Seconds to hh:mm:ss

Hi All I need to convert a number of fields in a record from seconds to hh:mm:ss ( or possibly hhh:mm:ss ). I'm guessing awk is the way to go . File has multiple records and each record contains 101 fields - can awk handle that ? The seconds values will be in fields 3 - 101 and could be 0. ... (4 Replies)
Discussion started by: Mudshark
4 Replies

4. HP-UX

Ticks in seconds.

Hello all, Is there any thumb rule or aproximation of the equivalence in second of one tick? Thank you in advance. (1 Reply)
Discussion started by: mig28mx
1 Replies

5. UNIX for Dummies Questions & Answers

getting time in mili seconds

hi all UNIX Gurus, this is my first post...so i posting this with great expectations:o...hoping to get the similar replies... my question is.... need to get timestamp with millisecond in UNIX. Date command gives Year,month day, hour,minute and second but it does not give millisecond. Any... (5 Replies)
Discussion started by: Bhups
5 Replies

6. UNIX for Dummies Questions & Answers

seconds to hh:mm:ss

Any sleek way to convert seconds to hh:mm:ss format . I know it can be done by mod and divide . Looking for a one liner if possible . Example 3600 seconds = 01:00:00 3601 seconds = 01:00:01 (2 Replies)
Discussion started by: akrathi
2 Replies

7. Shell Programming and Scripting

check the file every 15 seconds

Hi All, I need a script which does, script check a file every 15 second, if file not exist, it will create a log file. how can I do it ? thanks Alice (4 Replies)
Discussion started by: alisevA3
4 Replies

8. Shell Programming and Scripting

Get lines in 5 seconds

Hello everybody, how i can get how many lines are writed in a file in the last 5 seconds? For ezample i have 'file1' that is filled by a process automatically and i neet to know how many lines with the word 'EXACTO' were filled the last 5 seconds, can somebody help me? I try with: tail -f... (16 Replies)
Discussion started by: Lestat
16 Replies

9. UNIX for Advanced & Expert Users

how to get number of seconds

How do I get the number of seconds since 1970, within a script, for the previous day at 23:59? I need this value to pass into a sql statement to cleanup records older than the previous day at midnight. It will be automated via cron so no hard coding allowed. Thanks! (2 Replies)
Discussion started by: captainzeb
2 Replies
Login or Register to Ask a Question