Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to assign the default field separator to FS

50 views
Skip to first unread message

Marc de Bourget

unread,
Sep 16, 2016, 8:01:01 AM9/16/16
to
This might be a strange question or there may be an obvious solution.
Anyway: How to assign the default field separator of AWK to FS?
I'm writing code to set FS from command line which finally looks like:
FS = OPTIONS_VALUES["-field_separator"]

E.g. FS can be set to ":", "\t" ...
But how to set FS to the default FS of AWK (white space)?
Of course this can be done by not setting FS at all,
but is there another solution? (Just being curious ...).

Geoff Clare

unread,
Sep 16, 2016, 8:41:05 AM9/16/16
to
According to POSIX:

The default value of the FS variable shall be a single <space>.
[...]
If FS is <space>, skip leading and trailing <blank> and <newline>
characters; fields shall be delimited by sets of one or more
<blank> or <newline> characters.

(A consequence of this is that if you want fields to be delimited
by a single <space> character, you need to set FS to a regular
expression that matches a <space>, e.g. FS="[ ]" or FS=" {1}".)

--
Geoff Clare <net...@gclare.org.uk>

Kenny McCormack

unread,
Sep 16, 2016, 8:47:58 AM9/16/16
to
In article <aa192495-8298-4320...@googlegroups.com>,
Marc de Bourget <marcde...@gmail.com> wrote:
>This might be a strange question or there may be an obvious solution.
>Anyway: How to assign the default field separator of AWK to FS?

The short answer is: FS = " "

Or you could start by doing:

$ gawk 'BEGIN {print FS}'|od -bc
0000000 040 012
\n
0000002
$

--
To most Christians, the Bible is like a software license. Nobody
actually reads it. They just scroll to the bottom and click "I agree."

- author unknown -

Marc de Bourget

unread,
Sep 16, 2016, 9:09:05 AM9/16/16
to
Le vendredi 16 septembre 2016 14:47:58 UTC+2, Kenny McCormack a écrit :
> Marc de Bourget wrote:
> >This might be a strange question or there may be an obvious solution.
> >Anyway: How to assign the default field separator of AWK to FS?
>
> The short answer is: FS = " "
>
> Or you could start by doing:
>
> $ gawk 'BEGIN {print FS}'|od -bc
> 0000000 040 012
> \n
> 0000002
> $
>
> --
> To most Christians, the Bible is like a software license. Nobody
> actually reads it. They just scroll to the bottom and click "I agree."
>
> - author unknown -

Great! Thank you both Kenny and Geoff.
I wasn't aware that FS = " " can be used for the default separator
and includes both tabs and spaces.
After your hints, I have now found some information about that:
https://docs.freebsd.org/info/gawk/gawk.info.Field_Separators.html

Marc de Bourget

unread,
Sep 16, 2016, 9:17:29 AM9/16/16
to
As an additional note from the link above:
If I really wanted to use a single space as field separator I have to use:
FS = "[ ]"

Marc de Bourget

unread,
Sep 16, 2016, 9:23:16 AM9/16/16
to
Sorry, Geoff: I should have read your answer more carefully.
You already mentioned excactly this case. Thank you once more!

Ed Morton

unread,
Sep 16, 2016, 12:55:21 PM9/16/16
to
On 9/16/2016 8:09 AM, Marc de Bourget wrote:
> Le vendredi 16 septembre 2016 14:47:58 UTC+2, Kenny McCormack a écrit :
>> Marc de Bourget wrote:
>>> This might be a strange question or there may be an obvious solution.
>>> Anyway: How to assign the default field separator of AWK to FS?
>>
>> The short answer is: FS = " "
>>
>> Or you could start by doing:
>>
>> $ gawk 'BEGIN {print FS}'|od -bc
>> 0000000 040 012
>> \n
>> 0000002
>> $
>>
>> --
>> To most Christians, the Bible is like a software license. Nobody
>> actually reads it. They just scroll to the bottom and click "I agree."
>>
>> - author unknown -
>
> Great! Thank you both Kenny and Geoff.
> I wasn't aware that FS = " " can be used for the default separator
> and includes both tabs and spaces.

It does more than that though, otherwise you could just write FS="[[:blank:]]+"
or FS="[ \t]+", specifying the default FS also skips leading and trailing white
space so given this input:

a b

with FS=" " NF is 2 and "a" is $1 while with FS="[[:blank:]]+" NF is 3 and "a"
is $2:

$ echo ' a b' | awk -F' ' '{print NF, "<" $1 ">"}'
2 <a>
$ echo ' a b' | awk -F'[ \t]+' '{print NF, "<" $1 ">"}'
3 <>


Hopefully you came across that in your research but since you seem to be new to
using awk I figured it was worth mentioning before it bites you later :-).

Ed.

Marc de Bourget

unread,
Sep 16, 2016, 3:01:56 PM9/16/16
to
Thank you very much for your hints, Ed.
I'm not new to using AWK, actually I have beeen using AWK more than 20 years.
I know it sounds like a beginner's question but I dared to ask it anyway :-)
I almost always use FS = "\t" so I haven't thought much about other seps yet.

Marc de Bourget

unread,
Sep 16, 2016, 3:33:09 PM9/16/16
to
I have found more great hints from you, Ed, thank you for these one:
http://stackoverflow.com/questions/30405694/default-field-separator-for-awk

However, just to clarify a bit: The original intension of my question was
not to ask "What is the default field separator in AWK?",
but how to assign the default field separator to FS for some reason.
So, the solution for the original question is FS = " ".
But of course all the other hints are very interesting, too. Thank you all!
0 new messages