single space as field separator with awk

自古美人都是妖i 提交于 2019-12-21 04:06:56

问题


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 when one of the fields is empty.

I tried using "a space not followed by a space"( " (?! )" ) as FS but awk does not support negative lookahead. Simple google queries like "single space field separator awk" only sent me to the manual page explaining the special treatment of FS=" ". I must have missed the relevant manual page...

How can I use a single space as field separator with awk?


回答1:


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


来源:https://stackoverflow.com/questions/36008369/single-space-as-field-separator-with-awk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!