user2093607
user2093607

Reputation: 45

Formatting awk output

I am trying using format the output from awk such that the output is delimited by comma. Right now I have this:

ids ="`smartctl -A /dev/ssd |awk '/^[0-9]/ {if ($4 < $6) {print $1}}'`"

and my output looks like this:

111
222
333

but I want something like this:

111,222,333

Also my regex doesn't match with numbers 1 to 99, why?

Upvotes: 2

Views: 128

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85913

Just fixed your awk command to use printf:

smartctl -A /dev/ssd |awk '/^[0-9]/ {if ($4 < $6){printf "%s%s",sep,$1;sep=","}}'

If you want a trailing newline throw END{print ""} on the end.

Upvotes: 2

Kent
Kent

Reputation: 195289

quick and dirty:

your command|awk...|paste -s -d','

e.g:

kent$  paste -s -d',' <<< "a
b
c
d"
a,b,c,d

Upvotes: 3

Related Questions