wearetherock
wearetherock

Reputation: 3781

Problem with Awk & Grep

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

Upvotes: 3

Views: 962

Answers (4)

igustin
igustin

Reputation: 1118

awk '{print $1}'

Upvotes: 1

dcruz
dcruz

Reputation: 1094

You can do just:

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

Upvotes: 0

lutz
lutz

Reputation:

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

Upvotes: 1

camh
camh

Reputation: 42438

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.

Upvotes: 9

Related Questions