如何将此 bash 命令行折叠成 awk 语句?
我创建了一个 awk 脚本并像这样使用它:
# grep -E "[PM][IP][DO][:S]" file.txt | awk-script
How can I修改 awk 脚本以包含 grep 命令的工作(正在搜索“PID:”或“MPOS”?
awk脚本是:
#!/usr/bin/awk -f
/Sleeve/ {
printf("%8d, %7d, %7.2f, %7.2f, %7.2f\n", $5, $6, $7, $30, $31)
}
/Ambient/ {
printf("%8d, %7d,,,, %7.2f, %7.2f\n", $5, $6, $7, $8)
}
/MPOS:/ {
printf("%8d, %7d,,,,,, %5d, %5d\n", $4, $5, $2, $3)
}
I've created an awk script and use it like this:
# grep -E "[PM][IP][DO][:S]" file.txt | awk-script
How can I modify the awk script to include the effort of the grep command (which is searching for either "PID:" or "MPOS"?
awk-script is:
#!/usr/bin/awk -f
/Sleeve/ {
printf("%8d, %7d, %7.2f, %7.2f, %7.2f\n", $5, $6, $7, $30, $31)
}
/Ambient/ {
printf("%8d, %7d,,,, %7.2f, %7.2f\n", $5, $6, $7, $8)
}
/MPOS:/ {
printf("%8d, %7d,,,,,, %5d, %5d\n", $4, $5, $2, $3)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想搜索
PID:
或MPOS
,您可以说,如果您没有在行中找到它们,您想要跳过以下规则:If you just want to search for
PID:
orMPOS
, you can say that if you don't find them in the line, you want to skip following rules:我在
busybox
(在 Ubuntu 上的 Bash 上)中尝试了 litb 的答案,它对我有用。为了进行测试,我使用以下 shebang 来匹配具有到busybox
的符号链接的位置:并使用以下命令运行测试:
我还在
busybox sh
下运行了测试(使用busybox sh
) >PATH=/home/username/busybox:$PATH 虽然这不是必需的)并且它在那里工作。当你说“我仍然悲伤”时。这意味着什么?您是否收到错误消息或不正确的结果?
顺便说一句,除非您正在搜索字符的所有排列,否则您可以像这样执行 grep :
I tried litb's answer in
busybox
(on Ubuntu in Bash) and it worked for me. For testing, I used the following shebang to match where I have symbolic links tobusybox
:And ran the test using:
I also ran the test under
busybox sh
(withPATH=/home/username/busybox:$PATH
although not necessary for this) and it worked there.When you say "I still have grief." what does that mean? Are you getting error messages or incorrect results?
By the way, unless you're searching for all permutations of the characters, you can do your grep like this: