首页 > 解决方案 > 组织 Grep 输出

问题描述

需要帮助显示所需的输出,如下面的示例。提前致谢!

我有这个(没有子弹):

SWTICH1:interface GigabitEthernet1/0/1
SWTICH1: switchport mode access
SWTICH1:interface GigabitEthernet1/0/2
SWTICH1: switchport mode access
SWTICH1:interface GigabitEthernet1/0/3
SWTICH1: switchport mode access
SWTICH1:interface GigabitEthernet1/0/4
SWTICH1: switchport mode access
SWTICH1:interface GigabitEthernet1/0/5
SWTICH1: switchport mode access
SWTICH1:interface GigabitEthernet1/0/6
SWTICH1: switchport mode access

SWTICH2:interface GigabitEthernet1/0/1
SWTICH2: switchport mode access
SWTICH2:interface GigabitEthernet1/0/2
SWTICH2: switchport mode access
SWTICH2:interface GigabitEthernet1/0/3
SWTICH2: switchport mode access
SWTICH2:interface GigabitEthernet1/0/4
SWTICH2: switchport mode access
SWTICH2:interface GigabitEthernet1/0/5
SWTICH2: switchport mode access
SWTICH2:interface GigabitEthernet1/0/6
SWTICH2: switchport mode access

我希望它是这样的(没有子弹):

SWTICH1:
interface GigabitEthernet1/0/1
switchport mode access
interface GigabitEthernet1/0/2
switchport mode access
interface GigabitEthernet1/0/3
switchport mode access
interface GigabitEthernet1/0/4
switchport mode access
interface GigabitEthernet1/0/5
switchport mode access
interface GigabitEthernet1/0/6
switchport mode access
SWTICH2:
interface GigabitEthernet1/0/1
switchport mode access
interface GigabitEthernet1/0/2
switchport mode access
interface GigabitEthernet1/0/3
switchport mode access
interface GigabitEthernet1/0/4
switchport mode access
interface GigabitEthernet1/0/5
switchport mode access
interface GigabitEthernet1/0/6
switchport mode access

标签: awk

解决方案


编辑:根据 ghoti 先生的评论,添加一个解决方案,该解决方案将按 switch 关键字的顺序给出输出,并且也应该可以工作awk

awk -F':' '
NF{
  sub(/^ +/,"",$2)
}
!b[$1]++{
  c[++count]=$1
}
{
  a[$1]=(a[$1]?a[$1] ORS $2:$2)
}
END{
  for(i=1;i<=count;i++){
    print c[i] ORS a[c[i]]
  }
}'  Input_file


您能否尝试关注,看起来awk可能很适合这个问题。

awk -F':' 'NF{sub(/^ +/,"",$2);a[$1]=(a[$1]?a[$1] ORS $2:$2)} END{for(i in a){print i ORS a[i]}}' Input_file

说明:在此处添加说明。

awk -F':' '                        ##Setting field separator as colon here.
NF{                                ##Checking if NF value is NOT NULL where NF is number of fields in current line.
  sub(/^ +/,"",$2)                 ##Using sub function of awk which substitutes all initial/starting space from $2(second field) with NULL.
  a[$1]=(a[$1]?a[$1] ORS $2:$2)    ##Creating an array named a whose index is first field of current line and concatenating $2 values to its previous value.
}                                  ##Closing block for NF condition here.
END{                               ##Starting END condition of awk command here.
  for(i in a){                     ##Starting a for loop to traverse through array a here.
    print i ORS a[i]               ##Printing index of array a which is variable i now and ORS(new line) and then value of array a whose index is variable i.
  }                                ##Closing block for for loop here.
}'  Input_file                     ##Mentioning Input_file name here.

推荐阅读