基于多行匹配条件过滤文本
我有以下 sed 命令。我需要在单行中执行以下命令
cat File | sed -n '
/NetworkName/ {
N
/\n.*ims3/ p
}' | sed -n 1p | awk -F"=" '{print $2}'
我需要在单行中执行上述命令。任何人都可以帮忙吗?
假设文件的内容是
System.DomainName=shayam
System.Addresses=Fr6
System.Trusted=Yes
System.Infrastructure=No
System.NetworkName=AS
System.DomainName=ims5.com
System.DomainName=Ram
System.Addresses=Fr9
System.Trusted=Yes
System.Infrastructure=No
System.NetworkName=Peer
System.DomainName=ims7.com
System.DomainName=mani
System.Addresses=Hello
System.Trusted=Yes
System.Infrastructure=No
System.NetworkName=Peer
System.DomainName=ims3.com
,执行命令后,您将仅得到peer作为输出。有人可以帮我吗?
I have the following sed command. I need to execute the below command in single line
cat File | sed -n '
/NetworkName/ {
N
/\n.*ims3/ p
}' | sed -n 1p | awk -F"=" '{print $2}'
I need to execute the above command in single line. can anyone please help.
Assume that the contents of the File is
System.DomainName=shayam
System.Addresses=Fr6
System.Trusted=Yes
System.Infrastructure=No
System.NetworkName=AS
System.DomainName=ims5.com
System.DomainName=Ram
System.Addresses=Fr9
System.Trusted=Yes
System.Infrastructure=No
System.NetworkName=Peer
System.DomainName=ims7.com
System.DomainName=mani
System.Addresses=Hello
System.Trusted=Yes
System.Infrastructure=No
System.NetworkName=Peer
System.DomainName=ims3.com
And after executing the command you will get only peer as the output. Can anyone please help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用单个 nawk 命令。你可以失去无用的猫
你可以使用 sed 以及其他人提出的,但我更喜欢更少的正则表达式和更少的混乱。
上面将网络名称的值保存为“n”。然后,获取下一行并根据“ims3”检查第二个字段。如果匹配,则打印“n”的值。
You can use a single nawk command. And you can lost the useless cat
You can use sed as well as proposed by others, but i prefer less regex and less clutter.
The above save the value of the network name to "n". Then, get the next line and check the 2nd field against "ims3". If matched, then print the value of "n".
将该代码放入单独的
.sh
文件中,并将其作为单行命令运行。Put that code in a separate
.sh
file, and run it as your single-line command.假设您需要域 ims3 的网络名称,则此命令行无需使用 sed:
Assuming that you want the network name for the domain ims3, this command line works without sed:
因此,您需要下一行的域名包含“ims3”的网络名称,而不是下一行包含“ims7”的网络名称(即使示例中的网络名称相同)。
这也避免了虐待猫科动物(更不用说减少执行命令的数量)。
在 MacOS X 10.6.4 上进行了测试,但
没有理由认为它在其他地方也不起作用。然而,经验证据表明 Solaris
sed
与 MacOSsed
不同。这一切都可以通过一个 sed 命令完成,但需要三行:在 Solaris 10 上测试。
So, you want the network name where the domain name on the following line includes 'ims3', and not the one where the following line includes 'ims7' (even though the network names in the example are the same).
This avoids abuse of felines, too (not to mention reducing the number of commands executed).
Tested on MacOS X 10.6.4, but
there's no reason to think it won't work elsewhere too.However, empirical evidence shows that Solaris
sed
is different from MacOSsed
. It can all be done in onesed
command, but it needs three lines:Tested on Solaris 10.
您只需在几乎所有需要在换行符处中断命令或使用分号的地方放置
-e
即可。您不需要额外调用sed
或awk
或cat
。You just need to put
-e
pretty much everywhere you'd break the command at a newline or have a semicolon. You don't need the extra call tosed
orawk
orcat
.