linux - 我想一次替换文件中多次出现的文本

标签 linux ubuntu

我想在linux中替换文件中的文本
这种行会出现多次,而且格式也不同
例子:
12345 = 你自己:1376*5435
96235 = 你自己:6723*8112
这类文本会在文件中出现多次,唯一常见的是“你自己”,即中间文本
我需要用下面的文字替换它
“### = 你自己:###”
请帮我立即完成这项任务

最佳答案

如果位数正是您在示例中提供的,您可以使用 sed命令(将 filename 视为文件名):

sed -E 's/[0-9]{5}\ =\ yourself\ :\ [0-9]{4}\*[0-9]{4}/### = yourself : ###/' filename
如果位数会有所不同,只需将其更改为:
sed -E 's/[0-9].+\ =\ yourself\ :\ [0-9].+\*[0-9].+/### = yourself : ###/' filename
我使用以下输入来测试它:
12345 = yourself : 1376*5435
54353 = yourself : 3245*3221
96235 = yourself : 6723*8112
Ps: sed 命令内不需要转义空格。我的错。此外,如果您想就地更改它,请传递标志 -i .

关于linux - 我想一次替换文件中多次出现的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69928566/

相关文章:

postgresql - JasperReports 服务器无法在 Tomcat6 上运行 : Cannot create PoolableConnectionFactory

c++ - gsoap - 使用 -lcrypt 编译

python - 在 apache beam DirectRunner 中使用 KafkaIO 时出错

linux - 无法使用 snap 连接到 ubuntu 中 intellij 中的 docker 守护进程

c++ - 如何使用我自己的库 C++ ubuntu

python - 在python中获取文件的自定义属性

django - 与 pgAdmin 的远程 PostgreSQL 连接

linux - 如何在 Linux 中向 shell 脚本添加不同的调度程序?

c++ - Oprofile 与 perf

python - 函数 np.interp 和 pandas Python 出错