2012-12-10 118 views
2

我試圖改變從這個文件中的文本:如何使用sed交換兩列?

file.txt,c:\path\to\file 
file.txt,c:\path with spaces\to\file 
file.txt,c:\path\to\file with spaces 
file.txt,c:\path\to\file with spaces 
file.txt,c:\path\to\file with spaces 

對於這種輸出的(一個文件路徑):

c:\path\to\file\file.txt 
c:\path with spaces\to\file\file.txt 
c:\path\to\file with spaces\file.txt 
c:\path\to\file with spaces\file.txt 
c:\path\to\file with spaces\file.txt 

這種近乎工作,但需要一個' 「,在該行的末尾:

sed '[email protected]\(.*\),\(.*\),\(.*\)@\2,\1,\[email protected]' file 

任何幫助,將不勝感激,我不知道SED那麼好......

編輯

這爲我工作,但我仍想在裏面添加一個 「\」:

sed '[email protected]\(.*\),\(.*\)@\2,\1,\[email protected]' file 

回答

6

轉義反斜線,\\

sed 's/^\(.*\),\(.*\)$/\2\\\1/g' file 
         --^^-- 

此外,你只需要兩個捕獲組。


但是因爲我更多的是一個awk的傢伙。

awk -F, '{ print $2 "\\" $1 }' file 
+0

謝謝Sed神!! :-) –

+0

@MikeQ,不客氣。但是,我更喜歡'awk';) – Alexander

+0

啊!我也是,我更喜歡awk,但我實際上在批處理中使用gnu awk(不要問),它害怕造成問題的空間...... –