Shell 要剪切的脚本 /proc/softirqs

Shell script to cut /proc/softirqs

下面是"cat /proc/softirqs "的输出:

                    CPU0       CPU1       CPU2       CPU3
          HI:         24         13          7         54
       TIMER:  344095632  253285150  121234786  108207697
      NET_TX:    2366955        319        695     316044
      NET_RX:   16337920   16030558  250497436  117201444
       BLOCK:      19631       2747       2353    5067051
BLOCK_IOPOLL:          0          0          0          0
     TASKLET:        298         93        157      20965
       SCHED:   74354472   28133393   30646119   26217748
     HRTIMER: 4123645358 2409060621 2466360502  401470590
         RCU:   26083738   17708780   15330534   16857905

我的另一台机器有 24 个 cpu 核,输出难以阅读, 我喜欢输出只有 cpu0 , cpu2 , cpu4 , cpu6, .... 我知道可以使用 cut 或 awk 来做到这一点, 但不知道如何使用它来获得均匀的输出列。

编辑:

awk -F" " '{printf("%10s\t%s\n", ,) }'

会得到

      CPU1  CPU3
        24  7
 344095632  121234786
   2366955  695
  16337920  250497436
     19631  2353
         0  0
       298  157
  74354472  30646119
4123645358  2466360502
  26083738  15330534

很遗憾,CPU1应该是CPU0,CPU3应该是CPU2, 第一行只有4列,我可以跳过第一行吗 在这个 shell ?!

编辑2:

watch -d "cat /proc/softirqs | awk -F" " '{printf("%10s\t%s\n",,)}' "

遇到如下错误:

Every 2.0s: cat /proc/softirqs | awk -F  '{print    }' Tue Jun 21 10:23:22 2016

Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options: (standard)
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs
        -v var=val              --assign=var=val
Short options:          GNU long options: (extensions)
        -b                      --characters-as-bytes
        -c                      --traditional
        -C                      --copyright
        -d[file]                --dump-variables[=file]
        -e 'program-text'   --source='program-text'
        -E file                 --exec=file
        -g                      --gen-pot
        -h                      --help
        -L [fatal]              --lint[=fatal]
        -n                      --non-decimal-data
        -N                      --use-lc-numeric
        -O                      --optimize
        -p[file]                --profile[=file]
        -P                      --posix
        -r                      --re-interval
        -S                      --sandbox
        -t                      --lint-old
        -V                      --version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
        gawk '{ sum +=  }; END { print sum }' file
        gawk -F: '{ print  }' /etc/passwd

我还应该尝试什么?!

编辑3:

最终可行的shell想要:

# define function encapsulating code; this prevents any need for extra layers of quoting
# or escaping.
run() {
    awk  'NR>1{printf("%20s\t%10s\t%s\n",,,)}' </proc/softirqs|egrep 'TIMER|RX'
}

# export function
export -f run

# run function in subshell of watch, ensuring that that shell is bash
# (other shells may not honor exported functions)
watch -d  "bash -c run"

将代码传递给 watch 的子进程以避免转义错误的一种简单方法是使用导出函数:

# define function encapsulating code; this prevents any need for extra layers of quoting
# or escaping.
run() {
  awk -F" " '{printf("%10s\t%s\n",,)}' </proc/softirqs
}

# export function
export -f run

# run function in subshell of watch, ensuring that that shell is bash
# (other shells may not honor exported functions)
watch "bash -c run"

为了避免对导出函数的依赖,还可以使用 declare -feval 可用的形式检索函数的源代码,并使用 printf %q 将其转义以继续处理外部 shell 由 watch:

调用
run() {
  awk -F" " '{printf("%10s\t%s\n",,)}' </proc/softirqs
}
printf -v run_str '%q' "$(declare -f run); run"
watch "bash -c $run_str"

要跳过第一行,请执行:

awk -F" " 'NR>1{printf("%10s\t%s\n", ,) }'

为什么需要-F" ",对我来说是个谜。你也可以这样写:

awk 'NR>1{printf("%10s\t%s\n", ,) }'

(至于watch部分,见其他answer/s。)