2016-02-25 66 views
1

我想寫一個awk程序來使用pipe命令的輸出生成sql查詢。該命令的輸出會是這樣的awk:試圖使用awk生成查詢

Service name: APP1 
Service name: APP2 
Service name: APP3 
Service name: APP4 
Service name: APP5 
Service name: APP6 
Service name: APP7 
Service name: APP8 
Service name: APP9 

,結果我需要的是這樣的:

select 'APP1' x from dual union all 
select 'APP2' from dual union all 
select 'APP3' from dual union all 
select 'APP4' from dual union all 
select 'APP5' from dual union all 
select 'APP6' from dual union all 
select 'APP7' from dual union all 
select 'APP8' from dual union all 
select 'APP9' from dual 

我需要得到後弦「服務名稱:」字符串,把它在引號之間,並將其放入選擇。 第一行必須在字符串後面有「x」,並且最後一行不能包含union all。字符串上不能有空格。由於我對awk沒有太多的經驗,所以到目前爲止我無法想出辦法做到這一點。 我有這個至今:

srvctl config service -db database | grep 'Service name' | awk 'BEGIN {FS = "[:]"} 
{ gsub(/^[ \t]+|[ \t]+$/, "", $2) 
    if (NR == 1) 
    { 
     printf "'select\ \''" $2 "'\'\ x\ from\ dual\ union\ all\ '\n" 
    } 
    else 
    { 
     printf "'select\ \''" $2 "'\'\ from\ dual\ union\ all\ '\n" 
    } 
}' 

它會生成以下的輸出:

select 'APP1' x from dual union all 
select 'APP2' from dual union all 
select 'APP3' from dual union all 
select 'APP4' from dual union all 
select 'APP5' from dual union all 
select 'APP6' from dual union all 
select 'APP7' from dual union all 
select 'APP8' from dual union all 
select 'APP9' from dual union all 

任何幫助表示讚賞

感謝

回答

2

使用awk的,你可以這樣做:

awk 'NR==1{printf "select \047%s\047 x from dual union all\n", $NF; next} 
     s{print s, "union all"} 
     {s=sprintf("select \047%s\047 from dual", $NF)} END{print s}' file 

select 'APP1' x from dual union all 
select 'APP2' from dual union all 
select 'APP3' from dual union all 
select 'APP4' from dual union all 
select 'APP5' from dual union all 
select 'APP6' from dual union all 
select 'APP7' from dual union all 
select 'APP8' from dual union all 
select 'APP9' from dual 
+1

感謝的人,只是測試,儘管它把X別名每一行,這不是一個問題,這個工作對我蠻好! – sasteck

+0

很高興它的工作。我也修復從第二行開始刪除'x'別名。 – anubhava

+1

真棒謝謝!這更像是一個「乾淨的代碼」的東西,這兩種方法都是有效的sql sintaxes,感謝您抽出時間 – sasteck

1

也許更簡單的這樣

$ awk -v q="'" '{print "select " q$3q (NR==1?" x":"") " from dual union all"}' file | 
    sed '$s/\w* \w*$//' 

select 'APP1' x from dual union all 
select 'APP2' from dual union all 
select 'APP3' from dual union all 
select 'APP4' from dual union all 
select 'APP5' from dual union all 
select 'APP6' from dual union all 
select 'APP7' from dual union all 
select 'APP8' from dual union all 
select 'APP9' from dual 

$ awk -v q="'" -v x=" x" '{print "select " q$3q x " from dual union all"; x=""}' file | 
    sed '$s/\w* \w*$//' 
+0

有趣,這也適用,但您介意解釋sed部分嗎?我看到你用它來刪除最後一個工會,但無法弄清它究竟是如何工作的 – sasteck

+0

sed刪除最後一行中的最後兩個單詞。這簡化了awk以生成一致的輸出。第一個$是最後一行指針,\ w *對應一個單詞,最後$是行結束符。 – karakfa

+0

我現在看到它是如何工作的,非常感謝! – sasteck

1
$ awk 'BEGIN{x=" x"} NR>1{print prev " union all"; x=""} {prev="select \047" $NF "\047" x " from dual"} END{print prev}' file 
select 'APP1' x from dual union all 
select 'APP2' from dual union all 
select 'APP3' from dual union all 
select 'APP4' from dual union all 
select 'APP5' from dual union all 
select 'APP6' from dual union all 
select 'APP7' from dual union all 
select 'APP8' from dual union all 
select 'APP9' from dual 
+1

不錯的代碼,非常簡潔 – sasteck