Rounding decimal values in a column

Hi,
I wanted to round all the values in a column to nearest integer. I have multiple files with only two columns and I want to round values in column 2. e.g
input_file

                     A1   23.971578       A2   34.624976       A3   46.403446       A4   375       A5   1       A6   3       A7   351.475553       A8   18       A9   7.537485

I want outfile_file as:

                     A1   24       A2   35       A3   46       A4   375       A5   1       A6   3       A7   351       A8   18       A9   8         

How can I achieve this using shell command or script? Any help will be greatly appreciated!

Thanks

An awk solution:

awk '{for(i=1;i<=NF;i++){if($i~/^[0-9.]+/){$i=sprintf("%0.f",$i)}}}1' file
1 Like

Thanks a lot, Yoda!
It works great!!!

Using awk and no loop

awk 'NR>1 {$2=sprintf("%0.f",$2);print "A"$0}' RS="A" ORS=" "
A1 24 A2 35 A3 46 A4 375 A5 1 A6 3 A7 351 A8 18 A9 8
1 Like