2

I am using numfmt to convert a remote file's Content-Length from bytes to IEC format, i.e. 21M

$ curl -sI http://jruby.org.s3.amazonaws.com/downloads/1.7.12/jruby-bin-1.7.12.tar.gz|grep Length|cut -d' ' -f2|numfmt --from=auto --to=iec
'numfmt: invalid suffix in input: '21981488

Stumped at why numfmt complaining about invalid suffix. When I split the command and check the output of cut, and manually echo the value to numfmt, it works as expected

$ curl -sI http://jruby.org.s3.amazonaws.com/downloads/1.7.12/jruby-bin-1.7.12.tar.gz|grep Length|cut -d' ' -f2 -
21981488
$ echo 21981488|gnumfmt --from=auto --to=iec
21M

Am I missing something here?

2 Answers 2

3

It seems that the output from curl contains carriage returns and the number being passed to numfmt includes one. You could remove that before passing to numfmt:

curl ... | awk -F: '/Length/{sub("\r", "", $2); print $2}' | numfmt ...

Moreover, note that grep and cut can usually be replaced with an alternate single command.

2

There was a bug in coreutils-8.21 where we didn't quote the erroneous content correctly. If you run with the already released 8.22 it gives more obvious error:

numfmt: invalid suffix in input: ‘21981488\r’

Note if there are spaces or tabs after the number, then numfmt will ignore them (as it uses isblank()), but to ignore the \r would require using isspace(). Ignoring just blanks is correct I thnk.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .