Gaiger Chen
Gaiger Chen

Reputation: 300

Linux Shell, Awk : Merge 2 Variable Data by Column

The left part data (in variable) like :

 echo "$lpart"

 "2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55"
 "2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55"
 "2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55"
 "2017-07-03 13:39:5", "-37dB", "7c:e9:d3:f1:61:55"

The right part data (in variable) like :

echo "$rpart"     

"qhy2"
"qhy2"
"Apple Setup"
"qhy2"

I would like to merge the 2 data by column, it is :

 "2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55"  "qhy2"
 "2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55"  "qhy2"
 "2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55"  "Apple Setup"
 "2017-07-03 13:39:5", "-37dB", "7c:e9:d3:f1:61:55"  "qhy2"

This request is the same as the post : how to combine two variable column-by-column in bash, but my busybox does not support paste command. I have tried the commands :

 awk 'BEGIN{print ARGV[1], ARGV[2]}' "$lpart"  "$rpart"
 awk -v awk_lpart="$lpart" -v awk_rpart="$rpart" 'BEGIN{print awk_lpart awk_rpart }'

those will merge by row..

Could you give me suggestion how I could achieve the request ? Thank you.

Upvotes: 2

Views: 340

Answers (2)

Ed Morton
Ed Morton

Reputation: 203209

Building on one of your attempts:

awk -v awk_lpart="$lpart" -v awk_rpart="$rpart" 'BEGIN{
    split(awk_lpart,lp,/\n/)
    split(awk_rpart,rp,/\n/)
    for (i=1; i in lp; i++) {
        print lp[i], rp[i]
    }
}'
"2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55" "qhy2"
"2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55" "qhy2"
"2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55" "Apple Setup"
"2017-07-03 13:39:5", "-37dB", "7c:e9:d3:f1:61:55" "qhy2"

That will work with any awk in any shell on any UNIX box.

Upvotes: 1

anubhava
anubhava

Reputation: 784958

Using awk you can do this:

awk 'NR==FNR {a[FNR]=$1; next} {print $0, a[FNR]}' <(echo "$rpart") <(echo "$lpart")
"2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55" "qhy2"
"2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55" "qhy2"
"2017-07-03 13:39:5", "-39dB", "7c:e9:d3:f1:61:55" "Apple
"2017-07-03 13:39:5", "-37dB", "7c:e9:d3:f1:61:55" "qhy2"

Upvotes: 1

Related Questions