Rich Shepard
Rich Shepard

Reputation: 117

gawk: printf output ignores OFS

When I use printf() in gawk the OFS separates output fields. However,in this script it ignores the OFS so output fields are separated by commas. Reading about the printf() function I don't see how to separate output fields with a pipe, "|".

BEGIN { FS="|", OFS="|" }
{ split ($6, a, "/"); printf "%s,%s,%s,%s,%s,%s-%s-%s,%s\n", $1"|"$2"|"$3"|"$4"|"$5"|" a[3], a[1], a[2]"|"$7; }

Thanks in advance,

Rich

Upvotes: 0

Views: 372

Answers (1)

Ed Morton
Ed Morton

Reputation: 204310

The f in printf stands for "formatting" - you provide all the formatting as the first argument for printf instead of using the defaults like OFS as a separator or OFMT for numeric output as used by print.

When you write:

printf "%s,%s\n", x, y

you're specifically stating that you want a comma between 2 string fields. If you want to use printf but also want whatever value OFS has between the fields then you'd instead write:

printf "%s%s%s\n", x, OFS, y

which is equivalent to:

print x, y

In any case, I think what you're trying to do is:

BEGIN { FS=OFS="|" }
{ split ($6, a, "/"); $6=a[3] "-" a[1] "-" a[2]; print }

Upvotes: 1

Related Questions