我想寫一個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
任何幫助表示讚賞
感謝
感謝的人,只是測試,儘管它把X別名每一行,這不是一個問題,這個工作對我蠻好! – sasteck
很高興它的工作。我也修復從第二行開始刪除'x'別名。 – anubhava
真棒謝謝!這更像是一個「乾淨的代碼」的東西,這兩種方法都是有效的sql sintaxes,感謝您抽出時間 – sasteck