Bash script max value per hour


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script max value per hour
# 1  
Old 10-15-2017
Bash script max value per hour

i want to get max value every hour

sample input :
Code:
20:46:22 23
20:46:23 65
20:46:24 30
20:46:25 7
21:46:26 23
21:46:27 28
21:46:28 47
21:46:29 35
22:46:30 5
22:46:31 38
22:46:32 26
22:46:33 19
23:46:34 7
23:46:35 6
23:46:36 3
23:46:37 10


expected :
Code:
20:46:23 65
21:46:28 47
22:46:31 38
23:46:37 10

Can somebody help me on this ?

Last edited by Scott; 10-15-2017 at 09:17 PM.. Reason: Code tags
# 2  
Old 10-15-2017
Code:
sort  -nr  -k2  filename


Last edited by Scott; 10-15-2017 at 09:55 PM.. Reason: Code tags
This User Gave Thanks to garydeena For This Post:
# 3  
Old 10-15-2017
This should be possible with just the sort command, but:
Code:
$ sort -nrk2 somefile | awk -F: '!A[$1]++'
20:46:23 65
21:46:28 47
22:46:31 38
23:46:37 10

This User Gave Thanks to Scott For This Post:
# 4  
Old 10-15-2017
Hi Scott

Thanks for your reply , solved this case with your command :

Code:
cat test.txt | sort -nrk2 | awk -F: '!A[$1]++' | sort -nk1
00:07:15 139
01:00:05 89
02:01:50 58
03:27:07 132
04:03:10 140
05:43:11 161
06:41:36 174
07:37:46 194
08:46:59 213
09:35:15 229
10:02:13 340
11:11:47 268
12:20:00 229
13:32:40 258
14:34:40 205
15:52:56 203
16:41:09 186
17:52:24 235
18:47:05 304
19:53:10 266
20:07:37 196
21:04:48 193
22:03:50 154
23:14:05 148

Thanks
SmilieSmilieSmilie

Last edited by Scott; 10-15-2017 at 10:04 PM.. Reason: Please use code tags
# 5  
Old 10-16-2017
Hello fajar_3t3,

Following may help you too in same.
Code:
awk -F':| '  '{a[$1]=a[$1]>$NF?a[$1]:$NF;b[$1]=$1":"$2":"$3} END{for(i in a){print b[i],a[i]}}'   Input_file

Output will be as follows.
Code:
20:46:25 65
21:46:29 47
22:46:33 38
23:46:37 10

Thanks,
R. Singh
# 6  
Old 10-16-2017
Hi Ravinder,
Note that for(i in a) selects elements from array a in an unsepcified order. So, the output from your script won't necessarily be displayed in increasing time order (even if the input is in sorted order).

Hi fajar_3t3,
If you're going to call sort twice, there is no need to also invoke cat and awk. The command:
Code:
sort -k2,2nr test.txt | sort -t: -k1,1n -u

should produce the same output as the code you showed is in post #4 in this thread and run a little bit faster.

If your input file is in increasing time order (as shown in your sample in post #1), you could also try the single awk command:
Code:
awk -F '[: ]' '
function PrintHigh() {
	if(NR > 1)
		print HighLine
	SaveHigh()
}
function SaveHigh() {
	Hour = $1
	HighLine = $0
	HighValue = $NF
}
NR == 1 {
	SaveHigh()
	next
}
$1 != Hour {
	PrintHigh()
	next
}
$NF > HighValue {
	SaveHigh()
}
END {	PrintHigh()
}' test.txt

which should be still faster since only one process is invoked and the input is read only once and the hourly low-valued lines aren't written at all.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 10-16-2017
Argh. I tried that with two sorts after spending ages trying to do it with one!
Code:
sort -k2nr file | sort -t: -uk1

missed a -n (and arguably ending fields on the -ks), so gave up and reverted to the old favourite, awk Smilie

Nice one, Don!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to be run every one hour

How can we run shell script every one hour. Anyone having code unit for this? (1 Reply)
Discussion started by: Pratiksha Mehra
1 Replies

2. Shell Programming and Scripting

Need a script to see top processes for every hour

Hi All, I am new to Scripting , please give me guidance to write the script to see top processes on the Linux operating system. I executed this script on my Virtual Server(Linux) DATE=`date +%Y%m%d%H%M%S` HOME=/home/xmp/testing/xmp_report RADIUS_PID=`xms -xmp sh pr | grep... (2 Replies)
Discussion started by: madala
2 Replies

3. Shell Programming and Scripting

How to run script automatically every 12 hour once?

Hi ! all, I have once script to remove temporary cache and temporary xml files looks like this, as it is taking more space, I would like to run automatically every 12 hour once, and then I want to receive some log as acknowledgement #!/bin/sh echo "Removing logs and temp files (typically... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

4. Shell Programming and Scripting

How to convert 24 hour time to 12 hour timing?

Hi friends, I want to convert 24 hour timing to 12 hour please help me... my data file looks like this.. 13-Nov-2011 13:27:36 15.32044 72.68502 13-Nov-2011 12:08:31 15.31291 72.69807 16-Nov-2011 01:16:54 15.30844 72.74028 15-Nov-2011 20:09:25 15.35096 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

5. Shell Programming and Scripting

how to loop script for every 1 hour

Hi All, Need to run a1.sh script using nohup command (since crontab facility not there in my unix server) as below: nohup ksh -x a1.sh & a1.sh contains: nohup ksh -x b1.sh 2> b1.log & In a1.sh script i need to trigger b1.sh script every one hour. How to loop b1.sh to run for every 1... (4 Replies)
Discussion started by: HemaV
4 Replies

6. Shell Programming and Scripting

how to stop execution of a script after one hour

I have a shell script that writes some data in a file. I want to stop the script after one hour from start of execution using "EXIT 1". how to do it. I don't want to use CRONTAB. (5 Replies)
Discussion started by: mady135
5 Replies

7. Shell Programming and Scripting

Unix Script for getting date and validating just Hour

Hi, Can someone guide me to write a unix script for getting a hour out of a date command and validating hour to see if its > 7 and < 16. if hours is >7 and <16 then assign a variable value of 0730 and if hour is >16 then assign a variable value of 1630? Help appreciated. Thanks in advance.... (9 Replies)
Discussion started by: zulfikarmd
9 Replies

8. Shell Programming and Scripting

BASH condition for "File older than 1 hour"

I have a monitor script that executes every 5 minutes. I am adding functionality that will detect if a previous execution is hung. I have managed to do that by using a flag that is created when the monitor starts and is then removed when the monitor finishes. The hang check simply looks to see if... (2 Replies)
Discussion started by: Squeakygoose
2 Replies

9. Shell Programming and Scripting

Run a script on the hour but only for 30mins

Hi All, I want to run a script on the hour during a 24 - hour period; easy enough cron will take care of that..however I want the script to only run for only 30mins.. so with the script it knows its 30mins are up so exits. any ideas? Any help, greatly appericated. Thanking you all... (2 Replies)
Discussion started by: Zak
2 Replies
Login or Register to Ask a Question