Computation Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Computation Problem
# 1  
Old 05-11-2006
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  
Old 05-11-2006
How about:
awk -v FS=, -v OFS=, '{print substr($1,1,6),$3-$2}' < datafile
# 3  
Old 05-11-2006
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  
Old 05-11-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

2. AIX

AIX DateTime Computation

Good day people, Kindly advice on below please. 1) Formatting/ Arithmetic operation of given date I understand from the AIX man date and some research that flag -d is not applicable for AIX shell scripting and some of the UNIX command date command is not available in AIX. Please advice... (1 Reply)
Discussion started by: cielle
1 Replies

3. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

4. Programming

computation migration

Hi, i need a code to implement computation migration in c. Can you guys help me out?? (3 Replies)
Discussion started by: tanvi
3 Replies

5. AIX

How to create a filesystem with the correct computation of PP

Hi everyone, im having a problem with the computation of the PP size for creating a filesystem. for example my requirement is to create a new filesystem with 10gig of system on aix 5.1 and aix 5.3 system. here's the result when i run lsvg vgSAN-sparkle could any provide me an exact... (3 Replies)
Discussion started by: cwiggler
3 Replies

6. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

7. Shell Programming and Scripting

ssh script problem problem

Hi Please help me with the following problem with my script. The following block of code is not repeating in the while loop and exiting after searching for first message. input_file ========== host001-01 host001-02 2008-07-23 13:02:04,651 ConnectionFactory - Setting session state... (2 Replies)
Discussion started by: pcjandyala
2 Replies

8. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies
Login or Register to Ask a Question