bash - awk not capturing first line / separator -
i don't understand following behaviour:
this text file:
example.txt 12345 4321 hello hello test blobb 14324 2131 test , incoming ! blubb 52341 1231 last test shutting down bla ...
it consists of x rows of text, 4 tab-separated columns each. need first three, used awk (for first time):
awk '{fs="\t"; ofs="\t"; print $1,$2,$3}' < example.txt > excerpt.txt
the result this:
excerpt.txt 12345 4321 hello 14324 2131 test , incoming ! 52341 1231 last test shutting down ...
the first entry not contain full third column, , printing $1,$2,$3,$4
gives 12345 4321 hello hello
first row. so, apparently separates @ whitespace (both after first , second hello
), , not @ tab. checked if tab snuck in there, not case:
i find confusing, since works correctly other rows.
you setting field separators on every line. then, awk
reads record (line) in way when cursor reached it, first time setting is too late apply first record.
since default field separator space, on first line uses it. then, second record, takes account set.
you need set either in begin
block or before (the effect same):
awk 'begin{fs=ofs="\t"} {print $1,$2,$3}' example.txt > excerpt.txt awk -f"\t" -v ofs="\t" '{print $1,$2,$3}' example.txt > excerpt.txt
alternatively, can "recompile" record $1=$1
. reinterprets record based on current field separators. should work well:
awk '{fs=ofs="\t"; $0=$0; print $1,$2,$3}' example.txt > excerpt.txt
test
testing last thing, on recompiling field.
$ cat hello me , here doing awk $ awk '{fs="\t"; print $2}' doing awk $ awk '{fs="\t"; $0=$0; print $2}' me , doing awk
Comments
Post a Comment