Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate of a real value and discard its non-integer part in awk?

Tags:

awk

I have the following dataset (all positive values)

Input file

1 2.3456
1 5.02
2 3.9763333
2 0.123

I would like to truncate the numbers in the second column and discard its non-integer part.

How would one do it in awk?

Desired output file

1 2
1 5
2 3
2 0

Thanks for your help.

like image 911
Tony Avatar asked Feb 17 '11 10:02

Tony


3 Answers

try the int() function

awk '{$2=int($2)}1' file
like image 182
kurumi Avatar answered Sep 22 '22 06:09

kurumi


awk '{printf("%d %d\n"), $1, $2;}' inputFileName
like image 28
qbert220 Avatar answered Sep 21 '22 06:09

qbert220


Since you want to truncate rather than round, use %d instead of %0.f:

awk '{printf "%s %d\n", $1, $2}'
like image 44
Will Avatar answered Sep 22 '22 06:09

Will