1

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; }'
done

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|     ||

1 Answer 1

3

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=" ".

3
  • 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, 2011 at 18:27
  • 1
    It's looking pretty definite: Using Regular Expressions to Separate Fields... Thanks
    – Peter.O
    Jun 20, 2011 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]. Jun 20, 2011 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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