打印带有列号的页脚

时间:2015-07-29 08:30:33

标签: linux shell awk count

我发现此命令{print NF}显示总列数:

$ nova list | awk '{print NF}' | sort -n | uniq
1
9
10

但我希望为每一列打印他们的号码。

请参阅带有字段分隔符|的示例:

$ nova list | head
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| ID                                   | Name                                    | Status | Networks                                     |
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| 45bd0bc3-96b4-4193-ae76-59115b4ee528 | rert                                    | ACTIVE | netblock5=192.168.0.10                 |
| 6682aa37-b766-437e-9b16-ce1076ce2410 | test5                                   | ACTIVE | netblock5=192.168.0.110                |
| 6f08fcf3-ea71-4f33-a01a-9b0712385511 | test2                                   | ACTIVE | netblock5=192.168.0.111                |
| 8f628408-1ace-4792-85b6-e134fe1f07cb | test55                                  | ACTIVE | netblock5=192.168.0.52, 192.168.222.46 |
| 458aa8cb-42c2-4aa6-ab30-c6858bcd85f3 | derggdre                                | ACTIVE | netblock5=192.168.0.63, 192.168.222.49 |
| 67f4bd0c-0e4d-4ba1-8765-dc7d7831c8f8 | dgrfdrf                                 | ACTIVE | netblock5=192.168.1.86                 |
| 846ffa7d-76a4-4c70-8d82-23b5a205ad77 | ttttt                                   | ACTIVE | netblock5=192.168.1.27                 |
  1                                     2                                          3        4

4 个答案:

答案 0 :(得分:1)

让我们了解awk在这里做了什么

nova list | awk '{print NF}' | sort -n | uniq

这里是awk'{printf NF}'NF是字段分隔符作为空格的字段数。所以输出低于NF = 9(也计算管道'|'符号)。

| ID                                   | Name                                    | Status   | Networks                                     |

同样适用于以下数据行

| 846ffa7d-76a4-4c70-8d82-23b5a205ad77 | ttttt                                   | ACTIVE | netblock5=192.168.1.27                 |

在你的输出中你得到1和10也是nova list命令输出中的某些行必须有单个字段或10个字段。

现在出现问题,您希望打印具有NF或字段值及其NF值的字段。

nova list | awk '{for(I=1;I<=NF;I++){printf I"-"$I" "}printf "\n"}'

它不会在文件的最后一个打印字段编号,而是打印数据。

答案 1 :(得分:0)

Perl救援:

nova list | \
    perl -ne 'print;
              $s = $_ if /\|/;
              }{
              $s =~ s/[^|]/ /g;
              $s =~ s/\|/++$i/ge;
              print " $s\n"
             ' 
  • -n逐行读取输入行
  • 如果$s包含|(以跳过最后边框),则会在}{中打印并记住每一行
  • 当输入结束|时,不是|的所有内容都会被空格替换
  • 所有$s =~ s/(??{" {".((length(0+$i))-1)."}"})\|/++$i/ge; 都被数字替换
  • 打印结果

如果列数&gt;需要进行更多调整。 10(数字超过1个字符):

jqassistant

答案 2 :(得分:0)

您可以获取文件的最后一行&#34; clean&#34;这样它就成了字段数字的页脚:

nova list | awk -F"|" '1; 
                END {gsub (/[^|]/," ")
                       for(i=1;i<=NF;i++) 
                           sub(/\|  /, "  " i)
                       gsub(/\|/," ")
                       print
                    }'

此:

  • 用空格替换|以外的所有内容。
  • 用自动增加的数字替换所有字符串"| "
  • 替换尾随|
  • 打印结果。

测试

$ cat a
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| ID                                   | Name                                    | Status | Networks                                     |
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| 45bd0bc3-96b4-4193-ae76-59115b4ee528 | rert                                    | ACTIVE | netblock5=192.168.0.10                 |
| 6682aa37-b766-437e-9b16-ce1076ce2410 | test5                                   | ACTIVE | netblock5=192.168.0.110                |
| 6f08fcf3-ea71-4f33-a01a-9b0712385511 | test2                                   | ACTIVE | netblock5=192.168.0.111                |
| 8f628408-1ace-4792-85b6-e134fe1f07cb | test55                                  | ACTIVE | netblock5=192.168.0.52, 192.168.222.46 |
| 458aa8cb-42c2-4aa6-ab30-c6858bcd85f3 | derggdre                                | ACTIVE | netblock5=192.168.0.63, 192.168.222.49 |
| 67f4bd0c-0e4d-4ba1-8765-dc7d7831c8f8 | dgrfdrf                                 | ACTIVE | netblock5=192.168.1.86                 |
| 846ffa7d-76a4-4c70-8d82-23b5a205ad77 | ttttt                                   | ACTIVE | netblock5=192.168.1.27                 |

见输出:

$ awk -F"|" '1; END {line=$0; fields=NF; gsub (/[^\|]/," "); for(i=1;i<=fields;i++) sub(/\|  /, "  " i); gsub(/\|/," "); print}' a
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| ID                                   | Name                                    | Status | Networks                                     |
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| 45bd0bc3-96b4-4193-ae76-59115b4ee528 | rert                                    | ACTIVE | netblock5=192.168.0.10                 |
| 6682aa37-b766-437e-9b16-ce1076ce2410 | test5                                   | ACTIVE | netblock5=192.168.0.110                |
| 6f08fcf3-ea71-4f33-a01a-9b0712385511 | test2                                   | ACTIVE | netblock5=192.168.0.111                |
| 8f628408-1ace-4792-85b6-e134fe1f07cb | test55                                  | ACTIVE | netblock5=192.168.0.52, 192.168.222.46 |
| 458aa8cb-42c2-4aa6-ab30-c6858bcd85f3 | derggdre                                | ACTIVE | netblock5=192.168.0.63, 192.168.222.49 |
| 67f4bd0c-0e4d-4ba1-8765-dc7d7831c8f8 | dgrfdrf                                 | ACTIVE | netblock5=192.168.1.86                 |
| 846ffa7d-76a4-4c70-8d82-23b5a205ad77 | ttttt                                   | ACTIVE | netblock5=192.168.1.27                 |
  1                                      2                                         3        4                                       

或保留字段分隔符:

$ awk -F"|" '1; END {line=$0; fields=NF; gsub (/[^\|]/," "); for(i=1;i<=fields;i++) sub(/\|  /, "| " i); print}' a
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| ID                                   | Name                                    | Status | Networks                                     |
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| 45bd0bc3-96b4-4193-ae76-59115b4ee528 | rert                                    | ACTIVE | netblock5=192.168.0.10                 |
| 6682aa37-b766-437e-9b16-ce1076ce2410 | test5                                   | ACTIVE | netblock5=192.168.0.110                |
| 6f08fcf3-ea71-4f33-a01a-9b0712385511 | test2                                   | ACTIVE | netblock5=192.168.0.111                |
| 8f628408-1ace-4792-85b6-e134fe1f07cb | test55                                  | ACTIVE | netblock5=192.168.0.52, 192.168.222.46 |
| 458aa8cb-42c2-4aa6-ab30-c6858bcd85f3 | derggdre                                | ACTIVE | netblock5=192.168.0.63, 192.168.222.49 |
| 67f4bd0c-0e4d-4ba1-8765-dc7d7831c8f8 | dgrfdrf                                 | ACTIVE | netblock5=192.168.1.86                 |
| 846ffa7d-76a4-4c70-8d82-23b5a205ad77 | ttttt                                   | ACTIVE | netblock5=192.168.1.27                 |
| 1                                    | 2                                       | 3      | 4                                      |

答案 3 :(得分:0)

nova list | { 
    read line; echo "$line"        # read and print the first line
    read header; echo "$header"    # read, remember and print the 2nd line
    cat                            # all the rest of the nova list output
    # then, use the header, and transform the words into numbers
    echo "$header" | perl -pe 's/(\w+)/ sprintf "%-*d", length($1), ++$n /ge'; 
}

输出

+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| ID                                   | Name                                    | Status | Networks                                     |
+--------------------------------------+-----------------------------------------+--------+----------------------------------------------+
| 45bd0bc3-96b4-4193-ae76-59115b4ee528 | rert                                    | ACTIVE | netblock5=192.168.0.10                 |
| 6682aa37-b766-437e-9b16-ce1076ce2410 | test5                                   | ACTIVE | netblock5=192.168.0.110                |
| 6f08fcf3-ea71-4f33-a01a-9b0712385511 | test2                                   | ACTIVE | netblock5=192.168.0.111                |
| 8f628408-1ace-4792-85b6-e134fe1f07cb | test55                                  | ACTIVE | netblock5=192.168.0.52, 192.168.222.46 |
| 458aa8cb-42c2-4aa6-ab30-c6858bcd85f3 | derggdre                                | ACTIVE | netblock5=192.168.0.63, 192.168.222.49 |
| 67f4bd0c-0e4d-4ba1-8765-dc7d7831c8f8 | dgrfdrf                                 | ACTIVE | netblock5=192.168.1.86                 |
| 846ffa7d-76a4-4c70-8d82-23b5a205ad77 | ttttt                                   | ACTIVE | netblock5=192.168.1.27                 |
| 1                                    | 2                                       | 3      | 4                                            |

实际上,我可以说这一切都是一个快速的perl脚本:

echo "$nova_list" | perl -ne '
        $header = $_ if $. == 2; 
        print;
    } {
        $header =~ s/(\w+)/ sprintf "%-*d", length($1), ++$n /ge; 
        print $header;
'