Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following awk script shows how a leading space ' ' is ignored as a field separator, but other characters are treated as a separator with a preceding null $1 field.

Is there some way to make this behaviour consistent for all values of the field separator?

echo -e " Data\tdlm\t\$1\t\$2\t\$3"  
for d in '^' ':' '"' 'x' '/' ' ' ;do
  echo "${d}1${d}2${d}" |awk -F"$d" '
       { printf "|%s|\t|%s|\t|%s|\t|%s|\t|%s|\n", $0, FS, $1, $2, $3; }'

The output:

 Data   dlm     $1      $2      $3
|^1^2^| |^|     ||      |1|     |2|
|:1:2:| |:|     ||      |1|     |2|
|"1"2"| |"|     ||      |1|     |2|
|x1x2x| |x|     ||      |1|     |2|
|/1/2/| |/|     ||      |1|     |2|
| 1 2 | | |     |1|     |2|     ||
share|improve this question
up vote 2 down vote accepted

Set FS to be a regexp, FS = "[ \t]+" (which is 'whitespace'), and it will no longer strip leading and trailing spaces. awk only strips leading and trailing whitespace when FS=" ".

I don't think it's possible to tell awk not to do it, when FS=" ".

share|improve this answer
It works with `awk -F"[ ]" so that gets me most of the way there.. and I at least understand what's going on now.. thanks.. So it seems to be the same story as for any regex: beware of the stray regex special chars... (+1) I'll wait a time before accepting the answer, as perhaps(?) there is a non-regex way to disable the stripping .. IFS has no effect in awk. – Peter.O Jun 20 '11 at 18:27
It's looking pretty definite: Using Regular Expressions to Separate Fields... Thanks – Peter.O Jun 20 '11 at 18:55
It's even specified this way by POSIX: “If FS is <space>, skip leading and trailing <blank>s; fields shall be delimited by sets of one or more <blank>s” [otherwise behave intuitively]. – Gilles Jun 20 '11 at 21:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.