Search code examples
bashcommand-linecommand-line-interfacegnu

Numfmt --to=none inside a function


I want to create a new number column in a non-human-readable format in a csv using sed. To do so, I'll create a function and then apply it in sed via sed 's/(.*)/foo \1/e. Now, I've tested cat $file | cut -f1 -d" " | numfmt --from=iec --to=none directly on the file, and it returns exactly what I'm looking for. When I try to put it in a function:

foo(){cat $1 | cut -f1 -d" " | numfmt --from=iec --to=none}

numfmt gets very unhappy and insists that none} is an invalid format; when I add a space in between the "none" and the curly brace, numfmt sits patiently waiting for input, instead of closing out the function definition.

What am I missing here?


Solution

  • You need a space after { and a ; before }:

    foo(){ cat $1 | cut -f1 -d" " | numfmt --from=iec --to=none; }
    

    Cleaning up a bit more:

    foo(){ cut -f1 -d' ' "$1" | numfmt --from=iec --to=none; }
    

    or:

    foo(){ numfmt --from=iec --to=none < <(cut -f1 -d' ' "$1"); }