如何将多个sed命令组合为一个 [英] How to combine multiple sed commands into one

查看:100
本文介绍了如何将多个sed命令组合为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件上运行4个不同的sed命令.为了调整这4个命令的性能,我想将它们组合为一个.每个命令都是带有-E开关的复杂命令.搜索了很多论坛,但没有得到我的具体答案.

I have 4 different sed commands which I am running on a file. And in order to tune in the performance of these 4 commands, I want to combine them into one. Each command is a complex command with -E switch. Searched many many forums but could not get my specific answer.

sed -i -E ':a; s/('"$search_str"'X*)[^X&]/\1X/; ta' "$newfile"
sed -i -E '/[<]ExtData[>?" "]/{:a; /Name=/{/Name="'"$nvp_list_ORed"'"/!b}; /Value=/bb; n; ba; :b; s/(Value="X*)[^X"]/\1X/; tb; }' "$newfile"
sed -i -E ':a; s/('"$search_str1"'X*)[^X\<]/\1X/; ta' "$newfile"
sed -i -E ':a; s/('"$search_str2"'X*)[^X\/]/\1X/; ta' "$newfile"

我想结合他们说类似的话

And i want to combine them say something like

sed -i -E'command1'-e'command2'-e'command3'-e'command4'"$ newfile"

sed -i -E 'command1' -e 'command2' -e 'command3' -e 'command4' "$newfile"

但是它不起作用.因为可能是-E和-e不能组合.

But it is not working. Because may be -E and -e can't be combine.

请让我知道.

谢谢!普尼特(Puneet)

Thanks !! Puneet

推荐答案

-E 表示扩展的正则表达式",并且是一个独立的标志, -e 表示"expression"并且必须后跟 sed 表达式.
您可以将它们组合在一起,但是如果要使用多个 -e ,则每个 sed 表达式都必须以 -e 开头,而第一个表达式不是这种情况.

-E means "extended regex" and is a standalone flag, -e means "expression" and must be followed by a sed expression.
You can combine them, but each of your sed expression must be preceded by a -e if you want multiple of them, which isn't the case of your first one.

sed -i -E -e 'command1' -e 'command2' -e 'command3' -e 'command4' "$newfile"

第二种选择是用相同的表达式编写每个命令:

A second option is to write each command in the same expression :

sed -i -E 'command1;command2;command3;command4' "$newfile"

但是,由于您使用的是标签,因此我不会依赖此选项; John1024 指出,某些实现可能不支持它.

However, since you're using labels I wouldn't rely on this option ; some implementations may not support it as John1024 pointed out.

最后,正如疯子物理学家所述,您可以编写 sed 表达式到您将通过 -f 选项引用的文件.
该文件必须按行包含一个sed表达式(您可以通过在每行后加 \ 后加后缀来编写多行表达式,从而避免换行).

Lastly, as mentionned by Mad Physicist, you can write your sed expressions to a file which you'll reference through the -f option.
The file must contain a single sed expression by line (you can write multiline expressions by suffixing each line but the last by a \, thus escaping the line-feed).

这篇关于如何将多个sed命令组合为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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