Computing.Net > Forums > Unix > Subtracting columns using awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Subtracting columns using awk

Reply to Message Icon

Name: hej
Date: September 3, 2009 at 03:35:06 Pacific
OS: Windows XP
Subcategory: General
Comment:

How do I subtract column #2 in one file from column #2 in another file and then print column #1 and the result of the subtraction into a third file?

Thanks a lot!



Sponsored Link
Ads by Google

Response Number 1
Name: user57
Date: September 8, 2009 at 01:40:46 Pacific
Reply:

Try the below, redirecting the output to a third file:
file1
AA 10
AA 10
AA 10

File2
BB 50
BB 40
BB 30


$ nawk 'FNR==NR {arr[NR]=$2; next} { print $2-arr[FNR]}' file1 file2
40
30
20


0

Response Number 2
Name: StevePr
Date: September 11, 2009 at 22:34:43 Pacific
Reply:

Should work for whichever flavor of awk you use;

gawk -F'field separator eg space' '{print $2 - $1}' filename > newfile


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Subtracting columns using awk

subtract single value using AWK www.computing.net/answers/unix/subtract-single-value-using-awk/8503.html

Using AWK www.computing.net/answers/unix/using-awk/6583.html

Using AWK www.computing.net/answers/unix/using-awk/6580.html