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
problem with dd command or maybe AFS problem Anta Shell Programming and Scripting 0 08-25-2006 11:10 AM
SSH Problem auth problem budrito UNIX for Advanced & Expert Users 1 03-17-2004 10:12 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 05-11-2006
PradeepRed PradeepRed is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 25
Computation Problem

Hi Folks

I have a file with the following sample input
919416586748,1200,31200
919416283619,3521,33521
919416811900,2440,64940
919416576012,13670,43670

Now i want to subtract the second value (where , is field separator) from the third value and display the output as follows

919416,30000
919416,30000
919416,62500
919416,30000

i wrote a simple program

Code:
for var in `cat 1`
do
a1=`echo $var | cut -d"," -f1|cut  -c1-6`
a2=`echo $var | cut -d"," -f2`
a3=`echo $var | cut -d"," -f3`
a4=`expr $a3 - $a2`
echo "$a1,$a4" >> 2
done

This works fine for files which are of small size. But my input file has over 1 million entries and hence is taking a damn long time to process.
Can anybody recommend a much faster method for the same?

Thanks in advance
Regards
  #2 (permalink)  
Old 05-11-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
How about:
awk -v FS=, -v OFS=, '{print substr($1,1,6),$3-$2}' < datafile
  #3 (permalink)  
Old 05-11-2006
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Quote:
awk -v FS=, -v OFS=, '{print substr($1,1,6),$3-$2}' < datafile
awk 'BEGIN{FS=OFS=","}{print substr($1,1,6),$3-$2}' datafile
  #4 (permalink)  
Old 05-11-2006
PradeepRed PradeepRed is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 25
Thanks a lot folks

Both the queries worked. I really need to brush up on the awk command. This is the second time where a one line awk command has bailed me out where i had spent hours using some inefficient scripts.

Regards
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 10:29 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