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
Custom short cuts not working on JDS cooldude SUN Solaris 1 04-28-2008 04:41 PM
pdksh cuts off command prompt cjackson0 UNIX for Dummies Questions & Answers 5 12-31-2007 03:27 PM
Unknown event - daemon mode cuts programs from resources baranowb UNIX for Advanced & Expert Users 1 06-24-2007 11:35 PM
Slow FTP & SMB rockboles UNIX for Dummies Questions & Answers 4 12-11-2005 07:52 PM
multiple cuts syntax problem Gerry405 UNIX for Dummies Questions & Answers 12 10-31-2005 09:23 AM

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 01-19-2009
dlam dlam is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 37
Multiple echos and cuts too slow

Hi guys, hopefully this hasn't been asked before - couldn't see the question anywhere.

I have a large number of timestamps (hh-mm-ss-millisecond)
that I need to find the difference between e.g.:

14-11-07-513
14-11-07-644

Now the script that I have just knocked up is horrifically slow, the crux of which is converting the hh, mm, dd into milliseconds:

(( TOTALTIMESTART= (`echo $var | cut -d'-' -f4-8 | cut -d'<' -f1 | cut -c1,2` * 3600000) + \
(`echo $var | cut -d'-' -f4-8 | cut -d'<' -f1 | cut -c4,5` * 60000) + \
(`echo $var | cut -d'-' -f4-8 | cut -d'<' -f1 | cut -c7,8` * 1000) + \
(`echo $var | cut -d'-' -f4-8 | cut -d'<' -f1 | cut -c10,11,12`) ))

Once done on two values it subtracts them from each other and usese the difference.

There is obviously a better way of doing the part above, but I can't think for the life of me what it would be.

Any thoughts?
  #2 (permalink)  
Old 01-19-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,972

Code:
$ time ( echo "14-11-07-513" | awk -F- '{ print $1*3600000+$2*60000+$3*1000+$4 }' )
51067513

real    0m0.005s
user    0m0.004s
sys     0m0.000s

  #3 (permalink)  
Old 01-20-2009
dlam dlam is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 37
Wow, thanks for that. Exactly what I needed.
  #4 (permalink)  
Old 01-20-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099

Code:
#!/usr/bin/perl
@arr=("14-11-07-513","14-11-07-644");
for($i=0;$i<=$#arr;$i++){
	my @tmp=split("-",$arr[$i]);
	$brr[$i]=$tmp[0]*3600000+$tmp[1]*3600+$tmp[2]*60+$tmp[3];
	print $brr[$i],"--->\n";
}
print $brr[1]-$brr[0];

  #5 (permalink)  
Old 01-21-2009
dlam dlam is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 37
Hi guys, well I was still having speed problem (albeit it was taking about 10 seconds to process the file instead of 50 seconds - it is a really big file) because both solutions required me to use a cut to fill the variables which obviously slowed it down a fair bit. I did a bit more digging around and thought I would share what I have come up with instead which takes less than 1 second to complete:


Code:
while read monkey
do
   set x $monkey
   TOTALTIMESTART=$(($((${2}*36000000))+$((${3}*60000))+$((${4}*1000))+$5))
   TOTALTIMEEND=$(($((${6}*36000000))+$((${7}*60000))+$((${8}*1000))+$9))
   (( TIMEVAR=($TOTALTIMEEND-$TOTALTIMESTART) ))
   do some stuff
done < $1

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:45 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