2017-10-06 116 views
0

我有一個文本文件,可能使用分號分隔多行並帶有標頭。我必須爲文本文件中的每個列添加雙引號。我試圖讓個人作品使用sed。但是當它們結合時它們不運行。將雙引號添加到文本文件

我的命令:

  1. sed "s/;/\";"\/g a.txt - (與替換列 「;」(例如:d; 「B」, 「C」,「F)

  2. sed "s/^/\"/"g a.txt - ( - 取代的雙圍繞第一列引號)

  3. sed "s/$/\"/"g a.txt - (更換圍繞最後一列雙引號)

當我將它們合併如下:

sed "s/;/\";"\/g";s/^/\"/g";s/$/\"/ g" a.txt 

這不運行。我可以只合並2個以上的sed語句並運行但不是全部3.

+1

請查看[editing-help](http://stackoverflow.com/editing-help)。 – Cyrus

回答

0

將腳本(和變量)用單引號括起來,除非您絕對需要雙引號用於特定目的,例如,讓shell擴展變量。所以,你的腳本應該是(與清理了一些其他的東西):

sed 's/;/";"/g' a.txt - (replace columns with ";" (ex:d;"b";"c";"f) 

sed 's/^/"/' a.txt - (– replace double quotes around first column) 

sed 's/$/"/' a.txt - (replace double quotes around last column) 

和組合:

sed 's/;/";"/g; s/^/"/; s/$/"/' a.txt 

可以在一些SEDS減少一點(那些支持ERES)到:

sed 's/;/";"/g; s/^\|$/"/g' a.txt 
0

您可以嘗試這樣。

sed '2,$s/\([^;]*\)/\"\1"/g' 

從第2行開始保留標題。