LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   CentOS (https://www.linuxquestions.org/questions/centos-111/)
-   -   awk - subtract columns (https://www.linuxquestions.org/questions/centos-111/awk-subtract-columns-4175601158/)

robertkwild 03-06-2017 07:15 AM

awk - subtract columns
 
1 Attachment(s)
hi all,

i want to subtract columns and show the results in new columns

i attach the csv file in question

i want to subtract columns -

2 and 6
3 and 7
4 and 8

and show the results in new columns

any idea on how to do this please

many thanks,

rob

Turbocapitalist 03-06-2017 07:24 AM

With that data, you'll want to set both the field separator (FS) and output field separator (OFS) to a comma to that it will process correctly. Then just print what you want.

Code:

awk 'BEGIN { FS=OFS=","; } { print $1, $2, $1+$2, $3, $4, $2+4; }' calc.txt

robertkwild 03-07-2017 03:48 AM

think i have cracked it -

paste 23-02-2017_XSAreport.csv | awk -F, 'NR>3 {print $1,$4,$5,$6}' OFS=, >> out1.csv

paste 24-02-2017_XSAreport.csv | awk -F, 'NR>3 {print $1,$4,$5,$6}' OFS=, >> out2.csv

in 23/24-02-2017_XSAreport.csv i want to grab columns 1,4,5,6 but miss out the first three rows (NR>3) and they are comma seperated (-F,) and the output should be comma seperated aswell (OFS=,)

paste -d, out1.csv out2.csv >> out.csv

basically combine the two files together but make them comma seperated (-d,)

awk -F, '{print $1,$6 - $2,$7 - $3,$8 - $4}' OFS=, out.csv >>calc.csv

subtract the columns from eachother but leave column 1 alone ie just show it

do you think this is the easiest method as i have tried to learn awk to do all of this but awk is so so hard

many thanks,

rob


All times are GMT -5. The time now is 07:33 AM.