2014-02-26 60 views
1

我使用:使用sed保留字母範圍正則表達式的字母?

cat this_file | sed ':a;N;$!ba;s/\n[a-z]/ [a-z]/g' > that_file 

示例文本:

However the text \n 
looks like this when it replaces the newline.\n 

輸出:

However the text [a-z]ooks like this when it replaces the newline.\n 

這是什麼叫什麼?任何建議,我做錯了什麼?

回答

3

在該模式中,[a-z]表示一個字符類。在替代品中,替代品一樣。您需要使用反向引用:

cat this_file | sed ':a;N;$!ba;s/\n\([a-z]\)/ \1/g' > that_file 

此外,你可避免無用的使用貓:

sed ':a;N;$!ba;s/\n\([a-z]\)/ \1/g' this_file > that_file 
+0

優秀。謝謝我沒有意識到反向引用。像魅力一樣工作。 – jmunsch