![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linux Going Big Time and Prime Time Against Windows, UNIX (WSJ) (Addict 3D) | iBot | UNIX and Linux RSS News | 0 | 06-21-2007 05:10 PM |
| Start time/end time and status of crontab job | thambi | Shell Programming and Scripting | 3 | 05-16-2007 11:24 AM |
| How To Provide Time Sync Using Nts-150 Time Server On Unix Network? | pesty | UNIX for Advanced & Expert Users | 2 | 03-22-2007 02:20 AM |
| Date manipulations | rochitsharma | UNIX for Advanced & Expert Users | 2 | 09-07-2006 10:42 PM |
| date manipulations | user1 | Shell Programming and Scripting | 2 | 07-20-2004 01:22 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Time Manipulations
Hi All
![]() I have a long file having different fields like :- hh:mm:ss seconds 14:15:56 120 14:18:36 12 15:12:36 1500 I want to subtract the hh:mm:ss in line(2) from hh:mm:ss in line(1) & compare the output of substraction (obtained in seconds) with the seconds in line(1) and echo whether it is greater than seconds(120). I want the process to repeat for the whole file as in excel we copy the formula down the column. I tried the command nawk '{printf (substr($0,1,2)...........}' to seperate the hh:mm:ss as 14 15 56 120 14 18 36 12 15 12 36 1500 then using command nawk '{print ($1*3600+$2*60+$3)-prev;prev=($1*3600+$2*60+$3) }' <file> I got the result the result as: 51356 160 3240, all value in seconds. The problem is that I am unable to compare the 160sec with 120 sec and echo the result & loop the process ie compare 3240 sec with 12 sec & so on. Can u help me out. Thanks in Advance. |
|
|||||
|
here's the awk way: nawk -f vana.awk myFile.txt vana.awk: Code:
BEGIN {
FStime=":"
multN=split("3600 60 1", mult, " ");
}
{
n=split($1, timeA, FStime)
for(i=1; i <= n; i++)
time += timeA[i] * mult[i]
diff = $2
}
FNR == 1 {
prevTime=time;
prevDiff=diff
next;
}
{
printf("%s\n", ( ( time - prevTime) > prevDiff ) ? "greater" : "notGreater")
prevTime=time; prevDiff=diff
}
|
|
||||
|
Good code, vgersh. I think you forgot to reset time before the loop. Quote:
Code:
BEGIN {
split("3600 60 1", mult)
FS=":| "
}
{ time = 0
for (i=1; i < 4; i++)
time += $i * mult[i]
diff = $4
}
FNR == 1 {
prevTime = time
prevDiff = diff
next
}
{ print ( ( time - prevTime) > prevDiff ) ? "greater" : "notGreater"
prevTime=time; prevDiff=diff
}
|
|
||||
|
Quote:
Thanks in advance. |
|
||||
|
I tried "`awk '{print $0,"Greater"}'`","`awk '{print $0,"NotGreater"}'`"
to print the line followed by comparision but it prints as it is `awk '{print $0,"Greater"}'` or `awk '{print $0,"NotGreater"}'` .Please help me how to print the lines followed by comparision made(result). |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|