2013-04-08 140 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