I want to get the first word in every line from a file. Unfortunately a lot of lines begin with space(s). So I try to get the firs word with the following:

awk -F'[ \t]+' '{print $1}' < MyFile.txt, but it's not working. I try this echo " some string: here" | awk -F'[ \t]+' '{print $1}' and the results is blank line (I thing that it prints empty string). So why this is not working? I want to make it works with the awk command and explicitly passed delimiter (with educational purposes)

Thanks in advance.

  • 9
    you'd be better off without specifying delimiter I think, let awk do its automatic field detection on white-spaces... echo " some string: here" | awk '{print $1}'
    – Sundeep
    Jan 10, 2017 at 14:27
  • 1
    Please post a sample from your input file
    – Eng7
    Jan 10, 2017 at 16:22

3 Answers 3


You are specifying a field separator for Awk that is made up of at least a space or a tab character.

You feed the string some string: here into Awk and ask it to print the first field.

You get no output because the first field is empty.

The fields in this string are

<1:>   <2:some> <3:string:> <4:here>
$ echo '   some string: here' | gawk 'BEGIN {OFS="|";FS="[ \t]+"}{print $1,$2,$3,$4}'

It is more visible with comma-separated input data, like


If you avoid specifying a delimiter, Awk will use any run spaces and/or tabs as the delimiter. Additionally, any such blank characters flanking the line on either side will not be considered when splitting the line into fields.

If your input is JSON or YAML, consider using tools appropriate for parsing those structured document formats rather than using Awk. Suggestions for such tools include jq, yq, mlr, jtc and others.

  • 1
    By default, it's not necessarily only space and tab, in POSIX compliant awks that's all characters for which isblank() is true in the locale, some add newline to the list (not relevant here), some like busybox awk other whitespace characters (the ASCII [:space:] ones). Nov 15, 2022 at 7:44

In awk, the default field separator -F " " or BEGIN{FS=" "} follows a special convention

  • (1) initial spaces and final spaces are ignored
  • (2) splits by [ \t]+

Point (1) -- ignoring initial/final spaces -- just apply when field separator is exactly " ".

This behaviour is in fact what we would expect.

The default awk '{print $1}' works find and so does awk -F' ' '{print $1}'.

There is another similar convention for the input record separator(RS): when RS="" it stands for paragraph separation:

  • (1) separator is one or more empty lines
  • (2) initial and final empty lines are ignored.

Point (2) -- ignoring initial/final empty lines -- just apply when input record separator is exactly "".

  • 5
    Note that depending on the awk implementation, with FS=" " (the default), it will split either on [ \t]+ or [[:blank:]]+. Jan 10, 2017 at 17:08
  • @JJao, thank you, but why my code does not working? And why is your code working? I thought that this should return first field (this between first two whitespaces - empty string). Thanks in advance.
    – DPM
    Jan 10, 2017 at 18:47
  • 1
    @DPM , according to AWK manual, this "special convention", namely point (1) just is activated when FS is exactly ' '
    – JJoao
    Jan 11, 2017 at 8:09

awk ignores leading blank spaces when assigning fields and the default command is print. So this should work fine:

awk '{print $1}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .