single space as field separator with awk

前端 未结 1 1987
庸人自扰
庸人自扰 2021-02-07 15:19

I am dealing with a file where fields are separated by a single space.

awk interprets the FS \" \" as \"one or more whitespace\", which misreads my file whe

1条回答
  •  执笔经年
    2021-02-07 15:54

    this should work

    $ echo 'a    b' | awk -F'[ ]' '{print NF}'
    5
    

    where as, this treats all contiguous white space as one.

    $ echo 'a    b' | awk -F' ' '{print NF}'
    2
    

    based on the comment, it need special consideration, empty string or white space as field value are very different things probably not a good match for a white space separated content.

    I would suggest preprocessing with cut and changing the delimiters, for example

    $ echo 'a    b' | cut -d' ' -f1,3,5 --output-delimiter=,
    a,,b
    

    0 讨论(0)
提交回复
热议问题