2014-03-28 96 views
1

數據:AWK:在引號的字符串替換逗號,下劃線和刪除報價

的1.txt

-1,"AAA",[email protected] 
-10,"B ,BB","b, [email protected]" 
-7,C,[email protected] 

我想 1.去除逗號與下劃線 2.取出報價也引用字符串之後

我使用如下:

awk -F'"' -v OFS='' '{ for (i=2; i<=NF; i+=2) gsub("[,]", "_", $i) } 1' 1.txt > 2.txt 

輸出:(注意,「在第一線[」 AAA「]不會被刪除,因爲沒有逗號

-1,"AAA",[email protected] 
-10,"B ,BB",b_ [email protected] 
-7,C,[email protected] 

所以另外我用

awk -F'"' -v OFS='' '{ for (i=1; i<=NF; i+=1) } 1' 2.txt > 3.txt 

-1,AAA,[email protected] 
-10,"B ,BB",b_ [email protected] 
-7,C,[email protected] 

請建議做一個更好的辦法上述

+0

的最簡單的解決方案是不能再認爲逗號分隔的數據是分隔的數據的唯一有效的形式。 awk和UNIX一般假設數據分割字符將僅顯示爲數據分隔符,而不是作爲一個數據值。考慮標籤導出數據或「|」字符。祝你好運。 – shellter

回答

相關問題