crazysantaclaus
crazysantaclaus

Reputation: 623

Why does input for sort in awk has to be piped?

Just a short one for the understanding of awk: why does

awk '{split ($4,a,".");print $1,$2,$3,a[1]|"sort -nk3 "}'

work (and sort by the third column, as it is supposed to be), whereas this one

awk '{split ($4,a,".");print $1,$2,$3,a[1];"sort -nk3 "}'

does not? I expected that sort processes the output from the previous command anyway, with or without a pipe?

Upvotes: 0

Views: 48

Answers (1)

karakfa
karakfa

Reputation: 67507

In

awk '{split ($4,a,".");print $1,$2,$3,a[1]; "sort -nk3 "}'

there is no sort, just an no op string with contents "sort ..", print > or print | invokes different behavior with interacting the shell. Note that you can pipe it outside of awk as well.

awk '{split ($4,a,"."); print $1,$2,$3,a[1]}' | sort -nk3

will work fine.

tldr; use pipe to invoke sort

Upvotes: 4

Related Questions