2013-04-08 142 views
1

我有一個包含這一行的文件。正則表達式^ A和^ S?不能夠sed或grep嗎?

輸入

sls="N" stnid="armID" 
sls="N" stnid="C-ARM #11 w^Aw^Aw^A^Sg^Aw" 
sls="N" stnid="virtualID" 

對於我其中我要像

sls="N" stnid="armID" 
sls="N" stnid="" 
sls="N" stnid="virtualID" 

欲sed的 「C-ARM#11瓦特^胡^胡^ A^SG ^胡」 的輸出和用空白替換它。

問題是沒有正則表達式,我可以找到它將取代它。

我試過sed -e s //^// g s/A // g myfile> newfile。

它沒有工作。

還有一個問題是,當我CAT我的文件,它不顯示我的^ A &^S字符。 文件看起來像這樣。

sls="N" stnid="armID" 
sls="N" stnid="C-ARM #11 wwwgw" 
sls="N" stnid="virtualID" 

請幫助,在此先感謝。

Pal

+0

如果你沒有看到他們時,你'cat'文件,它們是'control-A'字符,而不是'^'後面是'A'。 – Barmar 2013-04-08 18:44:53

回答

1

^A和^ S是不可打印ASCII字符的文本表示。字符串實際上不包含子字符串「^ A」和「^ S」。我相信^ A是ascii代碼1,^ S是19.要在你的sed輸入中包含原始ASCII字符,請使用$(echo「\ 001」)和$(echo「\ 013」)。

+0

非常感謝您的幫助。 – ppal 2013-04-08 19:15:28

0

看起來這些是控制字符(^A實際上是ASCII碼爲1的非打印字符)。我可以想出兩種方法來處理這個問題。首先,嘗試匹配

# If you get the pattern of characters right, this will work: 
sed -e 's/C-ARM #11 w.w.w..g.w//' myfile > newfile 

# This will be less precise, but should still work: 
sed -e 's/C-ARM #11 .*"/"/' myfile > newfile 

或者使用cat那些非打印字符轉換爲打印字符,然後使用sed

cat -A myfile > myfile.fixed 
sed -e 's/C-ARM #11 w^Aw^Aw^A^Sg^Aw//' myfile.fixed > newfile 
+0

非常感謝,它工作。 – ppal 2013-04-08 19:15:00