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

发布于 2024-08-06 16:38:43 字数 461 浏览 1 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-08-13 16:38:43

如果您只想搜索 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)
}

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:

#!/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)
}
七堇年 2024-08-13 16:38:43

我在 busybox (在 Ubuntu 上的 Bash 上)中尝试了 litb 的答案,它对我有用。为了进行测试,我使用以下 shebang 来匹配具有到 busybox 的符号链接的位置:

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

并使用以下命令运行测试:

./awk-script file.txt

我还在 busybox sh 下运行了测试(使用 busybox sh) >PATH=/home/username/busybox:$PATH 虽然这不是必需的)并且它在那里工作。

当你说“我仍然悲伤”时。这意味着什么?您是否收到错误消息或不正确的结果?

顺便说一句,除非您正在搜索字符的所有排列,否则您可以像这样执行 grep :

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

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 to 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.

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 -E "(PID:|MPOS)" file.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文