Kumar Aditya
Kumar Aditya

Reputation: 229

Unable to print calculated value in new column of input.csv using awk. Its overwriting first coumn

Input File content

 cat input.csv 
0,1083,23,24,494,'2017-01-26','LACP050K','LACP050'
0,1526,71,86,692,'2017-01-26','LACP050L','LACP050'
0,5573651,259,315,170610,'2017-01-26','LACPT11K','LACPT11'

Need calculate ($1+$2)/8*1024 and paste into new 9th column of csv.

# cat input.csv | awk -F"," '{print $1,$2,$3,$4,$5,$6,$7,$8,($1+$2)/(8*1024)}' 
 **0.132202** 24 494 '2017-01-26' 'LACP050K' 'LACP050'
 **0.186279** 86 692 '2017-01-26' 'LACP050L' 'LACP050'
 **680.3771** 259 315 170610 '2017-01-26' 'LACPT11K' 'LACPT11'

In above out put, first column values replaced instead of adding new column.

Upvotes: 0

Views: 64

Answers (2)

karakfa
karakfa

Reputation: 67507

no need to repeat all fields, $0 is equivalent

$ awk -F, '{print $0 FS ($1+$2)/(8*1024)}' file

0,1083,23,24,494,'2017-01-26','LACP050K','LACP050',0.132202
0,1526,71,86,692,'2017-01-26','LACP050L','LACP050',0.186279
0,5573651,259,315,170610,'2017-01-26','LACPT11K','LACPT11',680.377

this also preserves the input field delimiter.

Upvotes: 0

VIPIN KUMAR
VIPIN KUMAR

Reputation: 3147

try below steps-

1 > cat -v input.csv (to see non printing character)
2 > dos2unix input.csv input1.csv
2 > awk -F"," '{print $1,$2,$3,$4,$5,$6,$7,$8,($1+$2)/(8*1024)}' input1.csv

Upvotes: 1

Related Questions