如何更改插入命令中的“sed"斜杠 (/) 分隔符? [英] How can I change the 'sed' slash (/) delimiter in insert command?

查看:69
本文介绍了如何更改插入命令中的“sed"斜杠 (/) 分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sed的替代命令中将分隔符斜线(/)更改为管道(|)的工作方式如下

echo hello |sed 's|你好|世界|'

如何在下面的 sed 插入命令中将分隔符斜线 (/) 更改为管道 (|)?

echo hello |sed '/你好/我世界'

解决方案

我不确定您提到的命令的意图:

echo hello |sed '/你好/我世界'

但是,我假设您想对与模式 hello 匹配的行执行某些操作.假设您想将匹配模式 hello 的行更改为 world.为了实现这一点,你可以说:

$ echo -e "something\nhello" |sed '\|你好|{s|.*|world|}'某物世界

为了使用正则表达式匹配行,可以使用以下形式:

/regexp/\%正则表达式%

其中 % 可以替换为任何其他单个字符(注意在第二种情况下前面的 \).

手册提供了更多详细信息.>

Changing the delimiter slash (/) to pipe (|) in the substitute command of sed works like below

echo hello | sed 's|hello|world|'

How can I change the delimiter slash (/) to pipe (|) in the sed insert command below?

echo hello | sed '/hello/i world'

解决方案

I'm not sure what is intended by the command you mentioned:

echo hello | sed '/hello/i world'

However, I presume that you want to perform certain action on lines matching the pattern hello. Lets say you wanted to change the lines matching the pattern hello to world. In order to accomplish that, you can say:

$ echo -e "something\nhello" | sed '\|hello|{s|.*|world|}'
something
world

In order to match lines using a regexp, the following forms can be used:

/regexp/
\%regexp%

where % may be replaced by any other single character (note the preceding \ in the second case).

The manual provides more details on this.

这篇关于如何更改插入命令中的“sed"斜杠 (/) 分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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