Thomas Paine
Thomas Paine

Reputation: 301

AWK printing bash variable more than once

I'm trying to utilize an AWK one liner that uses a bash variable. The problem is its being printed more then once or giving out an error. The bash variable looks like this (without the echo line):

echo "$time"
49.80
63.4
61
60.4
61

The AWK line I'm trying to use is this:

awk -v time="$time" -F, '{print $1,$2,$3,$4,$5=time}' file

The output is this:

SantaClara 6/7/2015 D 4 49.80
63.4
61
60.4
61
SantaClara 5/29/2015 D 5 49.80
63.4
61
60.4
61
SantaClara 5/21/2015 D 5 49.80
63.4
61
60.4
61
SantaClara 4/29/2015 D 5 49.80
63.4
61
60.4
61
SantaClara 4/22/2015 D 5 49.80
63.4
61
60.4
61

And I'm looking for this:

SantaClara 6/7/2015 D 4 49.80
SantaClara 5/29/2015 D 5 63.40
SantaClara 5/21/2015 D 5 61
SantaClara 4/29/2015 D 5 60.4
SantaClara 4/22/2015 D 5 61

I've several variations on the AWK line and have only gotten errors. What am I doing wrong?

Upvotes: 0

Views: 86

Answers (2)

anubhava
anubhava

Reputation: 784958

In case you want an awk only solution:

awk -F= 'FNR==NR{a[++i]=$0;next} {print $0, a[FNR]}' <(echo "$time") file
SantaClara 6/7/2015 D 4 49.80
SantaClara 5/29/2015 D 5 63.4
SantaClara 5/21/2015 D 5 61
SantaClara 4/29/2015 D 5 60.4
SantaClara 4/22/2015 D 5 61

Upvotes: 1

Mat
Mat

Reputation: 206679

Use paste instead:

$ paste file <(echo "$time")

Use the -d switch if you want a specific delimiter (tab is the default).

Upvotes: 4

Related Questions