Delimit by space but ignore backslash space
with gnu awk (gawk
) you can use some zero-length assertions like \<
or \>
:
$ echo 'a\ b c' | gawk 'BEGIN{FS="\\> +"} {print $1}'
a\ b
but unfortunately not the full-blown ones from perl
or pcre
(eg. (?<!\\)
, (?<=\w)
, etc):
$ echo 'a\ b, c' | perl -nle '@a=split /(?<!\\)\s+/, $_; print $a[0]'
a\ b,
You could substitute \space with something else and back again afterwards.
sed 's/\\ /\\x20/g' data_file | awk '{ print $1; }' | sed 's/\\x20/\\ /g'
With GNU grep
or compatible:
grep -Po '^(\\.|\S)*'
Or with ERE:
grep -Eo '^(\\.|[^\[:space:]])*'
That treats \
as a quoting operator, for whitespace as a delimiter, but also for itself. That is, on foo\\ bar
input, it returns foo\\
.