用sed替换字符包围的句点 [英] Replace period surrounded by characters with sed

查看:58
本文介绍了用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆