UNIX(AIX)命令帮助 - Sed& AWK

时间:2015-08-12 11:08:34

标签: unix awk sed command aix

我在 AIX 6.1。

上运行它

此命令的预期用途是按以下格式显示以下信息:

GetUsedRAM GetUsedSwap CPU_0_System CPU_0_User ... CPU_N_System :< EM> CPU_N_User

该命令由几个子命令组成:

echo `vmstat 1 2 | tr -s ' ' ':' | cut -d':' -f4,5,14-15 | tail -1 | sed 's/\([0-9]*:[0-9]*:\)\([0-9]*:[0-9]*\)/\1/'``mpstat -a 1 1 | tr -s ' ' '|' | head -8 | tail -4 | cut -d'|' -f 25,27 | awk -F "|" '{printf "%.0f:%.0f:",$2,$1}' | sed '$s/.$//'| sed -e "s/ \{1,\}$//"| awk '{int a[10];split($1, a,":");printf("%d:%d:%d:%d:%d:%d:%d:%d",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7])}'`

为了清晰起见我将进行格式化:

echo \
`vmstat 1 2 |
    tr -s ' ' ':' |
    cut -d':' -f4,5,14-15 |
    tail -1 |
    sed 's/\([0-9]*:[0-9]*:\)\([0-9]*:[0-9]*\)/\1/' \
` \
`mpstat -a 1 1 |
    tr -s ' ' '|' |
    head -8 |
    tail -4 |
    cut -d'|' -f 25,27 |
    awk -F "|" '{printf "%.0f:%.0f:",$2,$1}' |
    sed '$s/.$//' |
    sed -e "s/ \{1,\}$//" |
    awk '{int a[10];split($1, a,":");printf("%d:%d:%d:%d:%d:%d:%d:%d",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7])}' \
`

我理解所有 tr 剪切 ,以及(粗略) vmstat / mpstat 命令。第一个 sed 是我迷路的地方,我尝试在较小的段中运行命令并且不太确定为什么它似乎整体工作但不是在我在下一个 TR

我对 awk 命令也不太确定,虽然我模糊地理解这个前提,作为允许格式化输出的函数。

同样,我对sed是一个命令的模糊理解,允许在某些文件中替换某些字符串/字符。

我无法弄清楚上述情况中的具体实现是什么。

在整个命令的上下文中,是否有人能够确切地说明每个 sed awk 步骤中发生的事情?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

简化

这两个更简单的命令将获得完全相同的输出:

# GetUsedRAM:GetUsedSwap:CPU_0_System:CPU_0_User:…CPU_N_System:CPU_N_User

# Select fields 4,5 of last line, and format with :
comm1=`vmstat 1 2 |
        awk '$4~/[0-9]/{avm=$4;fre=$5} END{printf "%s:%s",avm,fre}'
       `
# Select fields 27 (sy) and 25 (us) for four cpu, print as decimal.
comm2=`mpstat -A 1 1 |
        awk -v firstline=6 -v cpus=4 '
             BEGIN{start=firstline-1; end=firstline+cpus;}
             NR>start && NR<end   {printf( ":%d:%d", $27,$25)}'
       `

echo "${comm1}${comm2}"

描述

原始命令的描述

整个命令是两个命令的串联。

  1. 第一个命令:
  2. output of the vmstat is shown in this link
    第4列和第5列是'avm'和'fre'。第14和15列的输出, 似乎是'我们'(用户)和'sy'(系统)。我说似乎没有输出 来自用户可以确认。

    The first command
    `vmstat 1 2 |               # Execute the command vmstat.
        tr -s ' ' ':' |         # convert all spaces to colon (:).
        cut -d':' -f4,5,14-15 | # select fields 4,5,14,and 15
        tail -1 |               # select last line.
        sed 's/\([0-9]*:[0-9]*:\)\([0-9]*:[0-9]*\)/\1/' \ # See below.
    `
    

    sed命令在冒号之前选择括号内的所有数字[0-9]* 重复两次。然后再次(没有最后一个冒号)。这就是整体 字符串分为两部分:«(dd:dd:)(dd:dd)»(d表示数字)。
    最后,它取代了内部选择的整个字符串 第一个大括号/\1/
    所有这些复杂性只会删除由cut选择的字段14和15。

    具有完全相同输出的简单命令是:

    Select fields 4,5 of last line, and format with (:).
    `vmstat 1 2 | awk '
         $4~/[0-9]/{avm=$4;fre=$5} END{printf "%s:%s:",avm,fre}'
    `
    
    1. 第二个命令:
    2. mpstat -A的输出类似于this one from Linux 也类似于AIX mpstat -d output

      但是,计算机上的mpstat -a(ALL)的AIX 6.1的确切输出 使用可能有几个变化。无论如何,在预期的决赛的指导下 输出所需:CPU_0_System:CPU_0_User:... CPU_N_System:CPU_N_User。

      似乎要选择的列应为us(用户)和sy (sys)使用cpu进行所有cpu使用的时间百分比, 这似乎是计算机上的四个。

      manual for AIX 6.1 mpstat is here

      它包含选项时显示的所有40列的列表 使用-a ALL:

      CPU min maj mpcs mpcr dev soft dec ph cs ics bound rq push
      S3pull S3grd S0rd S1rd S2rd S3rd S4rd S5rd S3hrd S4hrd S5hrd
      sysc us sy wa id pc %ec ilcs vlcs lcs %idon %bdon %istol %bstol %nsp
      

      我们和sy被列为字段27和28,但是显示了命令 由用户选择字段编号25和27.关闭但不相同。该 确认的唯一方法是从用户接收命令的输出。

      为了进行测试,我将使用output of mpstat 5 1 from here

      # mpstat 5 1
      
      System configuration: lcpu=4 ent=1.0 mode=Uncapped
      
      cpu   min  maj  mpc  int   cs  ics   rq  mig lpa   sysc us sy wt id   pc  %ec  lcs
      0 4940    0    1  632  685  268    0  320 100 263924 42 55  0  4 0.57 35.1  277
      1  990    0    3 1387 2234  805    0  684 100 130290 28 47  0 25 0.27 16.6  649
      2 3943    0    2  531  663  223    0  389 100 276520 44 54  0  3 0.57 34.9  270
      3 1298    0    2 1856 2742  846    0  752 100  82141 31 40  0 29 0.22 13.4  650
      
      ALL 11171    0    8 4406 6324 2142    0 2145 100 752875 39 51  0 10 1.63 163.1 1846
      
      
      The second command
      `mpstat -A 1 1 |            # execute command
          tr -s ' ' '|' |         # replace all spaces with (|).
          head -8 |               # select 8 first lines.
          tail -4 |               # select last four lines.
          cut -d'|' -f 25,27 |    # select fields 25 and 27
          awk -F "|" '{printf "%.0f:%.0f:",$2,$1}' |  # print the fields as integers.
          sed '$s/.$//' |             # on the last line ($), substitute the last character (.$) by nothing.
          sed -e "s/ \{1,\}$//" |     # remove trailing space(s).
          awk '{
              int a[10];
              split($1, a,":");
              printf("%d:%d:%d:%d:%d:%d:%d:%d",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7])
          }' \
      `
      

      关于int:对于旧版本的awk,调用没有括号的函数相当于在$ 0上调用函数。 int等价于int($ 0),它不打印也不使用。 a [10]的值也是如此。

      split在[i]中设置命令的每个值。然后,a [i]的所有值都打印为小数。

      等效,更简单的方法是:

      Command #2
      `mpstat -A 1 1 |
       awk -v firstline=6 -v cpus=4 '
              BEGIN{start=firstline-1; end=firstline+cpus;}
              NR>start && NR<end   {printf( ":%d:%d", $27,$25)}'
      `
      
相关问题