用sed替换字符包围的句点 [英] Replace period surrounded by characters with sed
问题描述
我正在尝试使用sed替换文本文件中的某些句点.我的文件是这样的:
I'm trying to replace certain periods within a text file using sed. My file is something like this:
a.b
aa.bb
aa. b
a . b
我想做的是用'§'替换字符前后的句点.在这种情况下,为"a.b"和"aa.bb".我已经设法用grep做到了,然后sed:
What I'm trying to do is to replace the periods that have characters right before and right after them with a '§'. In this case, "a.b" and "aa.bb". I've managed to do it with grep and then sed:
egrep '[[:alpha:]]\.[[:alpha:]]' | sed 's/\./§/g'
但这不会让我继续操作文件.如果我尝试
But that won't let me continue to manipulate the file. And if I try
sed 's/[[:alpha:]]\.[[:alpha:]]/§/g'
它将类似"a.b"的字符串替换为§",而不是a§b".
it would replace a string like "a.b" to "§" instead of "a§b".
非常感谢您的帮助!
推荐答案
您需要捕获"零件,然后再次打印.
You need to "catch" the parts and then print them again.
sed 's/\([[:alpha:]]\)\.\([[:alpha:]]\)/\1§\2/g'
格式化 \([[::]] \.\([[::]] \)
表示 XXX.YYY
.然后用 \1§\ 2
打印它们,第一部分是 \ 1
,第二部分是 \ 2
,包裹在<代码> \(... ...).
Format \([[:alpha:]]\)\.\([[:alpha:]]\)
indicates XXX.YYY
. Then you print them with \1§\2
, being \1
the first part, \2
the second... wrapped in between \( ... \)
.
If you want further information, this reference may help you: RegExp - keeping parts of a pattern in sed
这篇关于用sed替换字符包围的句点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!