There is a file I have which look
555.92 569.472 582.389 648.078 999.702 1040.75 1386.24 1418.47 1998.26 2182.13 2384.3
I would need to round every number like this
556 569 582
Ideally i don't need to create a tmp file. How to do that ?
There is a file I have which look
555.92 569.472 582.389 648.078 999.702 1040.75 1386.24 1418.47 1998.26 2182.13 2384.3
I would need to round every number like this
556 569 582
Ideally i don't need to create a tmp file. How to do that ?
Run the contents of the file through printf function.
$ xargs -a numbers.txt -n1 printf "%1.f "
556 569 582 648 1000 1041 1386 1418 1998 2182 2384
Also ,Ubuntu has nice little program called numfmt
which allows formating numbers to user defined standards, and human readable.
For example,
$ xargs -a numbers.txt -n1 numfmt --to=si --round=up | xargs echo
556 570 583 649 1.0K 1.1K 1.4K 1.5K 2.0K 2.2K 2.4K
Check man numfmt
for more info.
bash
can only handle integer arithmetic. Use a more powerful language, e.g. Perl:
perl -ane 'printf "%.0f ", $_ for @F' file
-n
reads the input line by line-a
splits each line on whitespace to the @F
array%.0f
is a flow format with zero digits after the decimal pointAnother python solution (one -liner). Run in a terminal:
python3 -c "[print(round(float(n))) for n in open('f').read().split()]"
Where 'f'
is the source file with your floats, between single quotes, e.g.:
python3 -c "[print(round(float(n))) for n in open('/home/jacob/Bureaublad/test').read().split()]"
Output:
556
569
582
648
1000
1041
1386
1418
1998
2182
2384
If you'd like the figures in one row:
python3 -c "[print(round(float(n)), end=' ') for n in open('f').read().split()]"
(Thanks @Oli!)
Output:
556 569 582 648 1000 1041 1386 1418 1998 2182 2384
The command:
python3 -c "[print(round(float(n))) for n in open('f').read().split()]"
in sections:
open('f').read().split()
reads the file 'f'
, splits it into floats (now still as string)
round(float(n))
First interprets the string n
as float, rounds it to an integer
[print(round(float(n))) for n in open('f').read().split()]
finally, generates the print command, to print all rounded floats.
print(..., end=' ')
argument instead of needing to do a separate ' '.join(...)
. It would read something like: for n in open('f').read().split(): print(int(round(float(n))), end=' ')
. Shorter and because you skip list comprehension, it's somewhat easier to follow for Python novices too.
Where is the awk
version? printf
can also be used in awk
:
awk '{for(i=1;i<=NF;i++) {printf "%0.f ",$i} printf "\n"}'
or inplace without a temporary file (Thx @muru)
awk -i inplace '{for(i=1;i<=NF;i++) {printf "%0.f ",$i} printf "\n"}'
Example
% cat foo
555.92 569.472 582.389 648.078 999.702 1040.75 1386.24 1418.47 1998.26 2182.13 2384.3
% awk '{
for(i=1;i<=NF;i++) {
printf "%0.f ",$i
}
printf "%s","\n"
}' foo
556 569 582 648 1000 1041 1386 1418 1998 2182 2384
-i inplace
for in-place editing, thus avoiding a temporary file like OP wants.
Using python
:
#!/usr/bin/env python2
with open('/path/to/file.txt') as f:
for line in f:
numbers = line.strip().split(' ')
for num in numbers:
print int(round(float(num))),
The list numbers
will contain all the numbers spitted on spaces (line.rstrip().split(' ')
)
Then we have used the round()
function to round the floating point numbers
As the inputs are stored as srings we need to use the float()
function to convert them to floats
The int()
function will print the number discarding the decimal points i.e. only the integer portion
Output :
556 569 582 648 1000 1041 1386 1418 1998 2182 2384
This should work for you:
perl -ne '@l=split(/\s+/,$_); foreach(@l){printf("%.0f",$_); print " "}'
you can use it like
echo "555.92 569.472 582.389" | perl -ne '
@l=split(/\s+/,$_);
foreach(@l) {
printf("%.0f",$_);
print " "
}'