AWK - 处理 wmctrl 输出最后一列中的空格

AWK - 处理 wmctrl 输出最后一列中的空格

作为第一个学习项目awk,我想重新格式化输出控制面板命令,在 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

但是,我无法弄清楚如何处理第九纵队输出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 for 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
...

相关内容