awk 的问题格雷普

发布于 2024-08-05 04:17:31 字数 973 浏览 0 评论 0原文

我喜欢从 wmctrl 获取窗口 pid(仅 firefox),我尝试过 wmctrl -lp |火狐 | grep awk -F" " "{print $1}" 但输出与我的预期不符。请帮忙。

beer@beer-laptop# wmctrl -lp
0x0160001b -1 6504   beer-laptop x-nautilus-desktop
0x016000bd  0 6504   beer-laptop conference - File Browser
0x03e00003  0 0              N/A XBMC Media Center
0x03800081  0 7282   beer-laptop Xbmc_ConferenceWindow.py (~/.qlive/xbmc-conference) - gedit
0x0352f117  0 6963   beer-laptop Ask a Question - Stack Overflow - Chromium
0x01400040 -1 6503   beer-laptop Top Expanded Edge Panel
0x01400003 -1 6503   beer-laptop Bottom Expanded Edge Panel
0x03202deb  0 6866   beer-laptop beer@beer-laptop: ~/.qlive/conference
0x012000c4  0 12134  beer-laptop Common threads: Awk by example, Part 1 - Mozilla Firefox
beer@beer-laptop# wmctrl -lp | grep Firefox | awk -F"  " "{print $1}"
0x012000c4  0 12134  beer-laptop Common threads: Awk by example, Part 1 - Mozilla Firefox
  • 在这种情况下,我更喜欢= 0x012000c4

I like to get window pid (only firefox) from wmctrl, i tried wmctrl -lp | grep Firefox | awk -F" " "{print $1}" but output not match my expect. Help please.

beer@beer-laptop# wmctrl -lp
0x0160001b -1 6504   beer-laptop x-nautilus-desktop
0x016000bd  0 6504   beer-laptop conference - File Browser
0x03e00003  0 0              N/A XBMC Media Center
0x03800081  0 7282   beer-laptop Xbmc_ConferenceWindow.py (~/.qlive/xbmc-conference) - gedit
0x0352f117  0 6963   beer-laptop Ask a Question - Stack Overflow - Chromium
0x01400040 -1 6503   beer-laptop Top Expanded Edge Panel
0x01400003 -1 6503   beer-laptop Bottom Expanded Edge Panel
0x03202deb  0 6866   beer-laptop beer@beer-laptop: ~/.qlive/conference
0x012000c4  0 12134  beer-laptop Common threads: Awk by example, Part 1 - Mozilla Firefox
beer@beer-laptop# wmctrl -lp | grep Firefox | awk -F"  " "{print $1}"
0x012000c4  0 12134  beer-laptop Common threads: Awk by example, Part 1 - Mozilla Firefox
  • In this case my prefer = 0x012000c4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

狼性发作 2024-08-12 04:17:31
wmctrl -lp | awk '/Firefox/ { print $1 }'

不需要 grep。 awk 会做到这一点。另外,默认的字段分隔符是空格,因此无需指定。另外,请在 awk 脚本周围使用单引号,这样 shell 就不会扩展 $1。这就是你的脚本失败的原因。 $1 变成了空,你的 awk 操作变成了“print”,打印了整行。

wmctrl -lp | awk '/Firefox/ { print $1 }'

No need for grep. Awk will do that. Also the default field separator is whitespace, so no need to specify that. Also, use single quotes around your awk script so the shell doesn't expand $1. That's why your script failed. $1 turned into nothing and your awk action became "print", which prints the whole line.

洛阳烟雨空心柳 2024-08-12 04:17:31

{print $1} 周围的双引号替换为单引号。这将阻止 shell 扩展 $1

Replace the double quotes around {print $1} with single quotes. That will prevent the shell from expanding $1.

夕嗳→ 2024-08-12 04:17:31

awk '{打印$1}'

awk '{print $1}'

玩心态 2024-08-12 04:17:31

你可以这样做:

wmctrl -lp | grep firefox | awk '{print $1}'

You can do just:

wmctrl -lp | grep firefox | awk '{print $1}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文