Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single space as field separator with awk

Tags:

awk

gawk

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?

like image 271
asachet Avatar asked Mar 15 '16 10:03

asachet


People also ask

How do you give space as field separator in awk?

FS can be set to "[ ]" to use a single space as field separator.

How do you create a separator in awk?

If you type ' -F\t ' at the shell, without any quotes, the ' \ ' gets deleted, so awk figures that you really want your fields to be separated with TABs and not ' t 's. Use ' -v FS="t" ' or ' -F"[t]" ' on the command line if you really do want to separate your fields with ' t 's.

Which option is used to mention field delimiter in awk?

The field separator is represented by the predefined variable FS .

Can awk use multiple field separators?

As you can see, you can combine more than one delimiter in the AWK field separator to get specific information.


1 Answers

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
like image 74
karakfa Avatar answered Sep 16 '22 16:09

karakfa