2

I would like to round off the below numbers to nearest wholenumber using awk command and copy it to another column say col11 and col12. can anyone help

eg 1)

column5,column6,,,,,column11,column12, -21733.3, -4129.327,,,,,,, 

output expected

 column5,column6,,,,,column11,column12, -21733.3, -4129.327,,,,,-21733,-4129, 
column5,column6,,,,,column11,column12, 21733.3,4129.327,,,,,,, 

output expected

 column5,column6,,,,,column11,column12, 21733.3,4129.327,,,,,21733,4129, 
2
  • Should .5 round up, or round down, or round to the nearest even number ("unbiased" rounding) or does it not matter? See gnu.org/software/gawk/manual/gawk.html#Round-Function. Commented Jan 25, 2023 at 12:46
  • @EdMorton or en.wikipedia.org/wiki/Rounding for even more ways one might want to round halves. In any case, I agree, adding some 1.9, -1.9, 1.5, -1.5, 2.5, -2.5, 15e-1, 0x1p-2, inf, nan... to the sample and corresponding expected output would have helped. Commented Jan 25, 2023 at 14:33

1 Answer 1

5

If it's a simple CSV with no string field containing newlines or ,s within quotes, then that could be done in awk with:

awk -F, -v OFS=, ' NR > 1 { $11 = sprintf("%.0f", $5) $12 = sprintf("%.0f", $6) } {print}' < your-file 

(NR > 1 to apply it only to records starting with the second, not on the header line)

sprintf("%.0f") rounds to nearest, int() truncates the fractional part.

With mlr, using the column names and handling more complex CSVs (beware there are many variants of CSV formats though)

mlr --csv put '$column11 = round(float(strip($column5))); $column12 = round(float(strip($column6)))' < your-file 

Numbers with surrounding whitespace are considered as string by mlr, hence the stripping which yields another string without those spaces, which can then be converted to float, and that float passed to round().

If there was no whitespace around your numbers, this should be enough:

mlr --csv put '$column11 = round($column5); $column12 = round($column6)' < your-file 
2
  • 2
    sprintf("%.0f") rounds .5 to the nearest even number on many, but not all, systems so 1.5 would round to 2 but 2.5 would also round to 2 - just worth mentioning in case anyone thinks that .5 would always round up or down or that 2.5 would round to 3 (or 2) on all systems. See gnu.org/software/gawk/manual/gawk.html#Round-Function. Commented Jan 25, 2023 at 12:53
  • 1
    @EdMorton This is called "banker's rounding" and is common in most languages. Commented Jan 25, 2023 at 16:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.