2016-08-15 37 views
1

我有一個SAS數據集與查詢列表作爲變量之一。下面是變量值之一:如何在Unix/Linux中的2個括號之間提取單詞?

SELECT * FROM (SELECT Table1 file2.txt file.txt QUEUES QDefinitions 
Parameters TRAP-Deposit-DSTran.dat.2016-08-07 FROM CS_CASE WHERE 
ANT_CD='FI_BASE_TENANT')t1 LEFT OUTER JOIN Table2 t2 ON 
t2.CASE_ID=t1.CASE_ID LEFT OUTER JOIN Table3 t3 ON 
t3.SERVICE_XID=t1.SERVICE_XID LEFT OUTER JOIN Table4 t4 ON 
t4.SERVICE_ID=t1.SERVICE_ID WHERE (t1.CASESTATUs_CD = (NEW) OR 
t1.CASE_STATUS_CD = (OPEN)) 
AND (t3.SEARCH_VALUE = (HighVal_Ind) AND t3.SEARCH_VALUE_N <= 3) AND 
(t4.SEARCH_VALUE_Nm = (Curr_Strategy) AND t4.SEARCH_STRG = (095)) 
GROUP BY t1.CASE_REFERENCE,t2.LAST_SCRFP,t1.SERVICE_ID 
ORDER BY t2.LAST_SCRFP DESC 

我想要做的就是我想把值輸入代碼這裏是在()(' ')

例如:

WHERE (t1.CASESTATUs_CD = (NEW) OR t1.CASE_STATUS_CD = (OPEN)) 

我想這個更改爲:

WHERE (t1.CASESTATUs_CD = ('NEW') OR t1.CASE_STATUS_CD = ('OPEN')) 

我該怎麼辦呢?

+6

歡迎來到Stack Overflow。你的問題完全不可讀。請閱讀[markdown help](http://stackoverflow.com/editing-help),然後[編輯]你的問題,使代碼看起來像代碼。你還應該解釋你迄今爲止嘗試解決你的問題,以及如何失敗。 – simbabque

+1

[編輯]你的問題,以澄清什麼'查詢列表作爲變量之一'的意思。你是說這個文本塊包含在shell變量中嗎?如果是這樣,爲什麼,它來自哪裏?一個腳本應該如何識別你想要引用的文本內的文本,你希望與引用中的文本分開引用你不想引用的文本? –

回答

0

你的問題還不清楚,但也許這是你想要什麼:

$ sed -E 's/\(([^()]+)\)/('\''\1'\'')/g' file 
SELECT * FROM (SELECT Table1 file2.txt file.txt QUEUES QDefinitions 
Parameters TRAP-Deposit-DSTran.dat.2016-08-07 FROM CS_CASE WHERE 
ANT_CD='FI_BASE_TENANT')t1 LEFT OUTER JOIN Table2 t2 ON 
t2.CASE_ID=t1.CASE_ID LEFT OUTER JOIN Table3 t3 ON 
t3.SERVICE_XID=t1.SERVICE_XID LEFT OUTER JOIN Table4 t4 ON 
t4.SERVICE_ID=t1.SERVICE_ID WHERE (t1.CASESTATUs_CD = ('NEW') OR 
t1.CASE_STATUS_CD = ('OPEN')) 
AND (t3.SEARCH_VALUE = ('HighVal_Ind') AND t3.SEARCH_VALUE_N <= 3) AND 
(t4.SEARCH_VALUE_Nm = ('Curr_Strategy') AND t4.SEARCH_STRG = ('095')) 
GROUP BY t1.CASE_REFERENCE,t2.LAST_SCRFP,t1.SERVICE_ID 
ORDER BY t2.LAST_SCRFP DESC 

以上在此輸入文件運行:

$ cat file 
SELECT * FROM (SELECT Table1 file2.txt file.txt QUEUES QDefinitions 
Parameters TRAP-Deposit-DSTran.dat.2016-08-07 FROM CS_CASE WHERE 
ANT_CD='FI_BASE_TENANT')t1 LEFT OUTER JOIN Table2 t2 ON 
t2.CASE_ID=t1.CASE_ID LEFT OUTER JOIN Table3 t3 ON 
t3.SERVICE_XID=t1.SERVICE_XID LEFT OUTER JOIN Table4 t4 ON 
t4.SERVICE_ID=t1.SERVICE_ID WHERE (t1.CASESTATUs_CD = (NEW) OR 
t1.CASE_STATUS_CD = (OPEN)) 
AND (t3.SEARCH_VALUE = (HighVal_Ind) AND t3.SEARCH_VALUE_N <= 3) AND 
(t4.SEARCH_VALUE_Nm = (Curr_Strategy) AND t4.SEARCH_STRG = (095)) 
GROUP BY t1.CASE_REFERENCE,t2.LAST_SCRFP,t1.SERVICE_ID 
ORDER BY t2.LAST_SCRFP DESC 
+0

這是我正在尋找的,但是當我運行命令時,它會給出一個額外的引用 - 之前 - ('選擇表1和之後ANT_CD ='FI_BASE_TENANT''),並且它沒有引用HighVal_Ind在我的情況。感謝幫助。 – Nick

+0

對於第一個問題,一種可能的解決方案是在括號表達式('[^()'\'']')中添加'''',對於第二個 - 這是不可能的如果你有其他的輸入產生這種行爲,那麼你需要在你的問題中包含THAT輸入。 –

+0

埃德,非常感謝你,這個函數爲我工作 - sed -E's/\(([^(,$ 1)'\''] +)\)/('\''\ 1'\' ')/ g' - 我保證這將是我的最後一個問題。有沒有什麼辦法可以把字符串中的值放在下面的條件中 - WHERE(t1.CASESTATUs_CD =(NEW,OLD,CLOSED,PENDING))更改爲 - WHERE(t1.CASESTATUs_CD =('NEW','OLD' ,'CLOSED','PENDING')) – Nick

0

這是使用正則表達式的最佳解決。在SAS中,這是使用prxchange函數完成的。假設你的條件中沒有空格,我們檢查中間沒有空格的括號。如果有其他情況,請調整功能。

data query; 
    length want $3000; 
    have= 
     "SELECT * FROM (SELECT Table1 file2.txt file.txt QUEUES QDefinitions 
     Parameters TRAP-Deposit-DSTran.dat.2016-08-07 FROM CS_CASE WHERE 
     ANT_CD='FI_BASE_TENANT')t1 LEFT OUTER JOIN Table2 t2 ON 
     t2.CASE_ID=t1.CASE_ID LEFT OUTER JOIN Table3 t3 ON 
     t3.SERVICE_XID=t1.SERVICE_XID LEFT OUTER JOIN Table4 t4 ON 
     t4.SERVICE_ID=t1.SERVICE_ID WHERE (t1.CASESTATUs_CD = (NEW) OR 
     t1.CASE_STATUS_CD = (OPEN)) 
     AND (t3.SEARCH_VALUE = (HighVal_Ind) AND t3.SEARCH_VALUE_N <= 3) AND 
     (t4.SEARCH_VALUE_Nm = (Curr_Strategy) AND t4.SEARCH_STRG = (095)) 
     GROUP BY t1.CASE_REFERENCE,t2.LAST_SCRFP,t1.SERVICE_ID 
     ORDER BY t2.LAST_SCRFP DESC"; 
    want = prxchange("s/\(([^ ]+)\)/('\1')/", -1, have); 
run; 
相關問題