简体   繁体   English

如何将此bash命令行折叠为awk语句?

[英]How can I collapse this bash command line into an awk statement?

I've created an awk script and use it like this: 我创建了一个awk脚本并像这样使用它:

# 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脚本以包含grep命令(正在搜索“ PID:”或“ MPOS”的工作)?

awk-script is: 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)
}

If you just want to search for PID: or MPOS , you can say that if you don't find them in the line, you want to skip following rules: 如果你只是想搜索PID:MPOS ,可以这么说,如果你没有在该行找到他们,你想跳过以下规则:

#!/usr/bin/awk -f

!/PID:|MPOS/ { 
        next 
}

/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 tried litb's answer in busybox (on Ubuntu in Bash) and it worked for me. 我在busybox尝试了litb的答案(在Bash的Ubuntu上),它对我有用 For testing, I used the following shebang to match where I have symbolic links to busybox : 为了进行测试,我使用了以下shebang来匹配我有指向busybox符号链接的位置:

#!/home/username/busybox/awk -f

And ran the test using: 并使用以下命令运行测试:

./awk-script file.txt

I also ran the test under busybox sh (with PATH=/home/username/busybox:$PATH although not necessary for this) and it worked there. 我还在busybox sh下运行了该测试(使用PATH=/home/username/busybox:$PATH虽然对此不是必需的),但它在那里工作。

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: 顺便说一句,除非您要搜索字符的所有排列,否则可以这样进行grep:

grep -E "(PID:|MPOS)" file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM