awk subtraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk subtraction
# 1  
Old 12-31-2008
awk subtraction

hi i have file 1 as follows:

Code:
6
7
8
9
10

i have file 2 as follows:
5
5
5
5
5
[/code]

i want file 3 as follows:
Code:
-1
-2
-3
-4
-5

i am subtracting every row from one file from every row from the second file and saving it to another file..i would like to use awk to do this..

so far i have:

Code:
awk '{for(i=1; i<=NF; i++) {val[i]=$1} {printf("%1.11f",val[i])}}' col > try #file1
awk '{for(i=1; i<=NF; i++) {val[i]=$1} {printf("%1.11f",val[i])}}' col1 > try1 #file2

since awk loses the variable from one statement to another, is there any way i can combine the two statements together and perform the subtraction?

thanks in advance.
# 2  
Old 12-31-2008
Code:
% head file[12]
==> file1 <==
6
7
8
9
10

==> file2 <==
5
5
5
5
5
% awk 'getline f1 < "file1" { print $1 - f1 }' file2
-1
-2
-3
-4
-5

# 3  
Old 12-31-2008
it worked!! thanks for your help!
# 4  
Old 12-31-2008
Quote:
Originally Posted by npatwardhan
hi i have file 1 as follows:

Code:
6
7
8
9
10

i have file 2 as follows:
Code:
5
5
5
5
5

i want file 3 as follows:
Code:
-1
-2
-3
-4
-5


Code:
paste file1 file2 | awk '{ print $2 - $1 }' > file3

# 5  
Old 12-31-2008
Or:

Code:
% paste -d- file2 file1|bc            
-1
-2
-3
-4
-5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Date Subtraction

Hello All, I am a newbie to unix shell scripting and need to write a script that displays the difference between two variables that stores date value. For example, F1=`ls -ltr file1* | tail -1 |tr -s ' ' |cut -d' ' -f6,7,8` F2=`ls -ltr file2* | tail -1 |tr -s ' ' |cut -d' ' -f6,7,8` F1... (3 Replies)
Discussion started by: priyaa2010
3 Replies

2. Shell Programming and Scripting

Need Help on date subtraction

I have dates as follows in a file 20121029135649 20121029135721 20121030091540 20121030093420 20121030094340 20121030095427 20121030095856 20121030100104 20121030100251 All these dates are in sorted order. I need to find out the difference between the dates as follows 2nd row... (6 Replies)
Discussion started by: meetsriharsha
6 Replies

3. Shell Programming and Scripting

Subtraction using arrays

Hello all . I have two arrays. ${ARRAY_MOUNT_POINT_CAPACITY} ${ARRAY_MOUNT_POINT_CAPACITY}. Whats the synatx of subtracting their values , placing them in variable V1 and then echoeing it ??? Ive tried expr and let ...gives me ./test_code.sh: difference: bad number (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

4. Shell Programming and Scripting

Subtraction

Hi #!/bin/sh month=`date +%m` year=`date +%Y` echo $month a=02 # Retaining Data for Current and Previous Month lmonth=`expr $month - $a` if test "$lmonth" = "0" then lmonth=12 year=`expr $year - 1` fi echo $year echo $lmonth The output is (3 Replies)
Discussion started by: Abhayman
3 Replies

5. Shell Programming and Scripting

The awk subtraction giving exponential values.Please help resolve it

Hi friends, I have a file list1 which has these 2 columns like 616449 0 434453 1 2151083 0 2226536 0 2132382 0 2136814 0 I have to put the result of col1 -col2 into another file list2 linewise. e.g. It gives the below result if use the below code: awk '{ print $1 - $2 }' list1 >... (2 Replies)
Discussion started by: kunwar
2 Replies

6. Shell Programming and Scripting

awk subtraction and print output

FileA F97S 83 530 K569E 531 736 output shud be F16S K40E it is code sed 's///g' FileA |awk '{print $1-$2+2}' it will print 16 40 anything can come with this output?? (1 Reply)
Discussion started by: cdfd123
1 Replies

7. Shell Programming and Scripting

use of uninitialized value in subtraction

Hallo all i am trying to execute this script ............... But this is throwing the error...... use of uninitialized value in subtraction in at icd_convert.pl line 156 use of uninitialized value in subtraction in at icd_convert.pl line 157 use of uninitialized value in subtraction in at... (1 Reply)
Discussion started by: suvenduperl
1 Replies

8. Shell Programming and Scripting

Date subtraction

hi, i set up a script on my server to do a particular task once files from an external system are ftpd in the format compaq_20100110 (YYDDMM). Interestingly, the source of ftp is sending the files in the format e.g 20100109 i.e. previous date and for some reason this fails.kindly see my script... (2 Replies)
Discussion started by: bigtejus
2 Replies

9. Shell Programming and Scripting

AWK subtraction in multiple columns

AWK subtraction in multiple columns Hi there, Can not get the following: input: 34523 934 9485 3847 394 3847 3456 9384 awk 'NR==1 {for (i = 1; i <= NF; i++) {n=$i; next}; {n-=$i} END {print n}' input output: 21188 first column only,... (2 Replies)
Discussion started by: awkward
2 Replies

10. UNIX for Dummies Questions & Answers

subtraction from date

hi gurus! i realize that my question shows my stupidness, but i need your help! i have: s_date=`date +%m-%d-%Y_%I%p` variable and i need the same, but minus one hour.. what i made: s_date=time(`date +%m-%d-%Y_%I%p`) - 3600 but i'm getting: daily_exports.sh: line 20: syntax error near... (4 Replies)
Discussion started by: MarGur
4 Replies
Login or Register to Ask a Question