2015-10-06 52 views
0

我有一個變量「a」,其中包含錯誤,我想將其視爲一個數組,並用換行符分隔。如何使用shell腳本或awk將單行轉換爲單行?如何在shell scipt中處理典型的數組集合?

輸入數據

a=(BEGIN DE_DUP_DROP_DY_TABLE1('$Table_Name'); END; 
Error at line 1 
ORA-06550: line 1, column 7: 
PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared 
ORA-06550: line 1, column 7: 
PL/SQL: Statement ignored) 

預期輸出

BEGIN DE_DUP_DROP_DY_TABLE1('$Table_Name'); END; | Error at line 1 | ORA-06550: line 1, column 7: | PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared |ORA-06550: line 1, column 7: | PL/SQL: Statement ignored 
+0

告訴你什麼是大多由於嵌套括號而導致語法錯誤。你的意思是你有一個字符串變量包含顯示的文本多行?輸出應該有用空白管道分隔的行嗎?空白管坯是一種麻煩;用管道分隔輸出的話,這將是相對平庸的。我看到示例輸出不是100%一致的;在第二個ORA-06550之前的管道之後沒有空間。嘰!計算機應該如何處理不能一致的人? –

回答

1

考慮:

a="BEGIN DE_DUP_DROP_DY_TABLE1('$Table_Name'); END; 
Error at line 1 
ORA-06550: line 1, column 7: 
PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared 
ORA-06550: line 1, column 7: 
PL/SQL: Statement ignored" 

可以使用生成一行輸出:

echo "$a" | tr '\n' '|' | sed 's/|/ | /g' 

國債收益率:

BEGIN DE_DUP_DROP_DY_TABLE1(''); END; | Error at line 1 | ORA-06550: line 1, column 7: | PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared | ORA-06550: line 1, column 7: | PL/SQL: Statement ignored | 

如果需要的最終|刪除(它來自換行符在echo輸出端),然後將其刪除:

$ echo "$a" | tr '\n' '|' | sed -e 's/|/ | /g' -e 's/ | $//' 
BEGIN DE_DUP_DROP_DY_TABLE1(''); END; | Error at line 1 | ORA-06550: line 1, column 7: | PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared | ORA-06550: line 1, column 7: | PL/SQL: Statement ignored 
$ 

或者不產生它:

$ printf "%s" "$a" | tr '\n' '|' | sed 's/|/ | /g' 
BEGIN DE_DUP_DROP_DY_TABLE1(''); END; | Error at line 1 | ORA-06550: line 1, column 7: | PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared | ORA-06550: line 1, column 7: | PL/SQL: Statement ignored 
$ 

當然,這些輸出是非常一致的,併爲每個管道(與樣本輸出不同)放置一個空間。如果數據中有管道,則會出現問題(以管道周圍的意外/不需要的空間的形式出現)。

如果,或許,你有每行一個單獨元素的數組,可以適應上面的代碼非常簡單地處理它(使用printf):

$ a=("BEGIN DE_DUP_DROP_DY_TABLE1('$Table_Name'); END;" 
"Error at line 1" 
"ORA-06550: line 1, column 7:" 
"PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared" 
"ORA-06550: line 1, column 7:" 
"PL/SQL: Statement ignored") 
$ printf "%s\n" "${a[@]}" | tr '\n' '|' | sed -e 's/|/ | /g' -e 's/ | $//' 
BEGIN DE_DUP_DROP_DY_TABLE1(''); END; | Error at line 1 | ORA-06550: line 1, column 7: | PLS-00201: identifier 'DE_DUP_DROP_DY_TABLE1' must be declared | ORA-06550: line 1, column 7: | PL/SQL: Statement ignored 
$ 
相關問題