Problem with Awk & Grep

I like getting the pid window (firefox only) from wmctrl, I tried wmctrl -lp | grep Firefox | awk -F "" {print $ 1} ", but the output does not meet my expectation. Please help.

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 - Qaru - 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, I prefer = 0x012000c4
+3
source share
4 answers
wmctrl -lp | awk '/Firefox/ { print $1 }'

No grep needed. Awk will do it. Also, the default field separator is a space, so it does not need to be specified. Also, use single quotes around your awk script, so the shell does not extend $ 1. This is why your script crashed. $ 1 turned into nothing, and your awk action became a β€œprint” that prints the entire line.

+9

{print $1} . $1.

+1

awk '{print $ 1}'

+1
source

You can only do:

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

Source: https://habr.com/ru/post/1717335/


All Articles