awk - 组织 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
解决方案
编辑:根据 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.
推荐阅读
- php - Laravel 队列工作者识别
- woocommerce - GoCardless 支付网关不适用于 Woocommerce - 缺少请求
- css - 更改单选按钮的颜色
- linux - 如何将单个文件 tar 到多个目录中?
- php - 如何通过 Laravel 路由中间件将多个参数传递给策略
- python - 如何复制数据框中的整行
- mysql - 在 IIS 中接收电子邮件并插入 MYSQL
- python - 在python中运行和清除不同的程序并清除控制台和变量
- react-native - 当 Modalize 打开时反应原生禁用向左滑动
- jpa - 使用安全 JDBC 通过用户/电子邮件和密码登录