Hi Guys,
i am having a csv file where i need to round off numerical column to 2 decimal precision in specific columns. i need to ignore the first two line i.e the header columns and manipulate rest of the lines of the csv file. My columns are specific i.e i need to round off only 2nd,4th and 6th field of the file without affecting other fields.
heading
head1,head2,head3,head4,head5,head6
aa,12,bb,-123,cc,0
bb,-12.456666,zz,-123.23333,cc,12.1
i need the below output
heading
head1,head2,head3,head4,head5,head6
aa,12.00,bb,-123.00,cc,0.00
bb,-12.45,zz,-123.23,cc,12.10
I have tried with below
head -2 file.csv>file.tmp
awk 'BEGIN{OFS=FS=","}
{
$2=sprintf("%.2f",$2)
$4=sprintf("%.2f",$4)
$6=sprintf("%.2f",$6)
}1' file.csv >file_conv.tmp
tail +2 file_conv.tmp>>file.tmp
mv file.tmp > file.csv