Hello Everyone,
I have a file with 5 fields in each line just like mentioned below. Also the 4th field is time elapsed(hh:mm:ss) since the process is running
xyz abc status 23:00:00 idle
abc def status 24:00:00 idle
def gji status 27:00:02 idle
fgh gty status 00:00:00 idle
Here I want to print entire lines where time elapsed is greater than 24 Hours. Please help.
Try
awk 'substr($4,1,2)>=24' file
The above script is not working in case time is 119:00:00 since in this case first two letters are 11 and they are less than 24. Please help.
How about now
awk '{split($4,a,":"); if (a[1]>=24) print}'
1 Like
Sir , You are Great . Can you please explain the code syntax/logic
Hello rahul2662,
Following is the explaination for same.
awk '{split($4,a,":"); if (a[1]>=24) print}'
split($4,a,":"); ### spliting field 4th into an array named a with delimiter :
if (a[1]>=24) #### Now checking array a's first element eg-> (24:00:00) BOLD one either it is grater than 24 or not if yes then print else do nothing
Thanks,
R. Singh
2 Likes
You said that you wanted to print lines where the elapsed time is greater than 24 hours. The code:
awk '{split($4,a,":"); if (a[1]>=24) print}' file
will also print lines where the time is exactly 24:00:00 (which is NOT greater than 24 hours). If you really want to only print lines where the elapsed time is greater than 24 hours you could try either of the following:
awk '{split($4,a,":"); if (a[1]>=24 || (a[1]==24 && (a[2] + a[3]))) print}' file
or:
awk '(h=($4+0))>24 || (h==24 && substr($4,length($4)-4)!="00:00")' file
If you want to try any of these on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
.
If file
contains:
xyz abc status 23:00:00 idle
abc def status 24:00:00 idle
def gji status 27:00:02 idle
fgh gty status 00:00:00 idle
dwc u&lf status 24:00:10 idle
dwc u&lf status 100:01:23 idle
the last two scripts above produce the output:
def gji status 27:00:02 idle
dwc u&lf status 24:00:10 idle
dwc u&lf status 100:01:23 idle
while the 1st script above produces the output:
abc def status 24:00:00 idle
def gji status 27:00:02 idle
dwc u&lf status 24:00:10 idle
dwc u&lf status 100:01:23 idle
1 Like