The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-13-2005
vanand420 vanand420 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 60
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.
  #2 (permalink)  
Old 07-13-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Got a rather roundabout script to do the job:


Code:
#!/bin/sh
#set -x
line=`head -1 test.tmp`
sec1=`echo $line | cut -d' ' -f2`
hms1=`echo $line | cut -d' ' -f1|awk -F':' '{print $1*3600+$2*60+$3}'`
tail +2 test.tmp | while read line; do
        sec2=`echo $line | cut -d' ' -f2`
        hms2=`echo $line | cut -d' ' -f1|awk -F':' '{print $1*3600+$2*60+$3}'`
        secint=`expr $sec1 - $sec2`
        if [ $hms1 -gt $secint ]; then
                echo "hh:mm:ss subtraction is greater than seconds"
        else
                echo "hh:mm:ss subtraction is less than / equal to seconds"
        fi
        sec1=$sec2; hms1=$hms2
done

test.tmp is the file that holds the data. It is assumed to be present in the same directory as the script.

I am sure that there is some way in awk that you could do this in less than half the number of lines. But unfortunately, my knowledge of awk leaves much to be desired.

Last edited by blowtorch; 07-13-2005 at 04:07 PM..
  #3 (permalink)  
Old 07-13-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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
}

  #4 (permalink)  
Old 07-16-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Thumbs up

Good code, vgersh. I think you forgot to reset time before the loop.
Quote:
Originally Posted by vgersh99

Code:
{
  n=split($1, timeA, FStime)
  time = 0
  for(i=1; i <= n; i++)
   time += timeA[i] * mult[i]
  diff = $2
}
Your code is so attractive, I couldn't resist stripping it.

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
}

  #5 (permalink)  
Old 07-16-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by futurelet
Good code, vgersh. I think you forgot to reset time before the loop.
good catch - thanks.

Welcome to unix.com, futurelet - glad to see you here!
  #6 (permalink)  
Old 07-19-2005
vanand420 vanand420 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 60
Quote:
Originally Posted by vgersh99
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 Work Friend, it works but what to do if to print the lines followed by greater or notGreater.
Thanks in advance.
  #7 (permalink)  
Old 07-21-2005
vanand420 vanand420 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 60
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).
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0