
最初の研究プロジェクトとしてアッ、出力を再フォーマットしたいです。コントロールパネルDebian 11 で次のようにインストールできるコマンドです。
sudo apt install wmctrl
開いているすべてのウィンドウに関する情報を一覧表示するには、次のコマンドを実行します。
wmctrl -lpG
出力例:
0x0120002b 4 7 2 157 3836 2068 my-pc window - AWK - Dealing with spaces in the last column of output from wmctrl - Unix & Linux Stack Exchange — Firefox
上記のコマンドでは各列の意味を覚えていないので、AWKを使用して名前と値のペアに分割したいと思います。
wmctrl -lpG | awk '{print "----------------------\nWindow ID: " $1 "\nDesktop Number: " $2 "\nProcess ID: " $3 "\nx-offset: " $4 "\ny-offset: " $5 "\nwidth: " $6 "\nheight: " $7 "\nMachine Name: " $8 "\nWindow Title: " $9}'
出力例:
----------------------
Window ID: 0x0120002b
Desktop Number: 4
Process ID: 7
x-offset: 2
y-offset: 134
width: 3836
height: 2068
Machine Name: my-pc
Window Title: window
希望の出力:
----------------------
Window ID: 0x0120002b
Desktop Number: 4
Process ID: 7
x-offset: 2
y-offset: 134
width: 3836
height: 2068
Machine Name: my-pc
Window Title: window - AWK - Dealing with spaces in the last column of output from wmctrl - Unix & Linux Stack Exchange — Firefox
しかし、どのように処理するのかわかりません。9番目の列出力wmctrl -lpG
(列9にスペースが含まれているため)。私はただ得ます。最初の単語ウィンドウタイトルフルウィンドウタイトルの代わりに。
この問題を解決する簡単な方法はありますか?
答え1
wmctrl -lpG \
| awk '{
tmp=$0;
# remove first 8 fields from tmp
sub(/^[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ /,"",tmp);
print "----------------------\nWindow ID: " $1 "\nDesktop Number: " $2 "\nProcess ID: " $3 "\nx-offset: " $4 "\ny-offset: " $5 "\nwidth: " $6 "\nheight: " $7 "\nMachine Name: " $8 "\nWindow Title: " tmp
}'
出力:
----------------------
Window ID: 0x0120002b
Desktop Number: 4
Process ID: 7
x-offset: 2
y-offset: 157
width: 3836
height: 2068
Machine Name: sidekick
Window Title: window - AWK - Dealing with spaces in the last column of output from wmctrl - Unix & Linux Stack Exchange — Firefox
答え2
出力がwmctrl
固定幅フィールドのように見えるため、GNU awkを使用できますFIELDWIDTHS
。cat file
その項目がないので、wmctrl
元の印刷ステートメントを維持してください。
$ wmctrl -lpG |
awk -v FIELDWIDTHS='10 3 2 7 7 7 5 6 *' '
{
for (i=1;i<=NF;i++) {
gsub(/^ +| +$/,"",$i)
}
print "----------------------\nWindow ID: " $1 "\nDesktop Number: " $2 "\nProcess ID: " $3 "\nx-offset: " $4 "\ny-offset: " $5 "\nwidth: " $6 "\nheight: " $7 "\nMachine Name: " $8 "\nWindow Title: " $9
}'
----------------------
Window ID: 0x0120002b
Desktop Number: 4
Process ID: 7
x-offset: 2
y-offset: 157
width: 3836
height: 2068
Machine Name: my-pc
Window Title: window - AWK - Dealing with spaces in the last column of output from wmctrl - Unix & Linux Stack Exchange — Firefox
またはawkを使用してください。
$ wmctrl -lpG |
awk '
BEGIN {
OFS = ": "
numCols = split("Window ID:Desktop Number:Process ID:x-offset:y-offset:width:height:Machine Name:Window Title",hdr,/:/)
}
{
print "----------------------"
for (i=1; i<numCols; i++) {
print hdr[i], $i
}
sub("([^ ]+ +){"i-1"}","")
print hdr[i], $0
}
'
答え3
これは一つの可能性です。
$ wmctrl -lpG | awk '{
a="";
for(i=9; i<=NF; i++) {
a = a " " $i
};
print "----------------------\nWindow ID: " $1 "\nDesktop Number: " $2 "\nProcess ID: " $3 "\nx-offset: " $4 "\ny-offset: " $5 "\nwidth: " $6 "\nheight: " $7 "\nMachine Name: " $8 "\nWindow Title: " a }'
----------------------
Window ID: 0x04000007
Desktop Number: 0
Process ID: 18952
x-offset: 1367
y-offset: 102
width: 1600
height: 836
Machine Name: pc
Window Title: ~:bash—Konsole
...