LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS
User Name
Password
CentOS This forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.

Notices


Reply
  Search this Thread
Old 03-06-2017, 07:15 AM   #1
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Rep: Reputation: Disabled
awk - subtract columns


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
Attached Files
File Type: txt calc1.txt (354 Bytes, 96 views)
 
Old 03-06-2017, 07:24 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
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
 
Old 03-07-2017, 03:48 AM   #3
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk ceratain columns sryzdn Linux - Newbie 1 10-25-2013 04:49 AM
[SOLVED] help on awk contain between two columns phpshell Programming 10 04-17-2013 12:33 AM
[SOLVED] how to subtract selective values in multi row multi column (awk or sed ?) nricardo Linux - Newbie 2 10-20-2012 09:55 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
extracting columns with awk gav251 Programming 7 03-10-2011 08:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration