2017-07-06 134 views
2

左側部分的數據(在可變)合併2個可變數據,如:Linux的殼牌,awk中:由

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" 

右邊部分數據(變量),如:

echo "$rpart"  

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

我會喜歡用列合併2個數據,它是:

"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" 

這個請求是一樣的崗位:how to combine two variable column-by-column in bash,但我的BUSYB黃牛不支持粘貼命令。 我已經試過了命令:

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

那些將根據行合併..

你可以給我建議,我怎麼能達到要求嗎?謝謝。

回答

1

使用awk的你可以這樣做:

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" 
+0

謝謝,也就是說,大括號{}會分別響應引入的(<)數據? –

+1

'<(...)'是進程替換。你必須爲此使用'bash'。 – anubhava

+0

謝謝你的建議,它的工作原理。多變量的方式也是? –

1

建立在你嘗試之一:

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" 

,將與在任何UNIX系統中任何shell awk的任何工作。