Single space as a field separator with awk

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

awk interprets FS " " as "one or more spaces", which reads my file incorrectly when one of the fields is empty.

I tried using "space followed by no space" ( " (?! )" ) As FS, but awk does not support negative lookups. Simple Google queries, such as awk's single-space separator, sent me to a manual page explaining the special interpretation of FS=" " . I must have missed the corresponding manual page ...

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

+5
source share
1 answer

it should work

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

where as, it treats all adjacent spaces as a whole.

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

based on the comment, it needs special attention, an empty line or a space, since the value of a field is very different things, probably not a very good match for content separated by a space.

I would suggest preprocessing with cut and changing delimiters like

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

Source: https://habr.com/ru/post/1245075/


All Articles