需要帮助显示所需输出,如下面的示例所示。提前感谢!
我有这个(没有子弹):
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发布于 2019-02-14 03:26:19
编辑:,如ghoti先生的评论,添加了一个解决方案,这将提供一个输出顺序的开关关键字,并应在任何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.发布于 2019-02-14 03:59:52
一个简单的awk解决方案可能如下所示:
awk -F: 'length() && $1!=s {s=$1;print s} {print $2}' input.txt这是通过捕获第一个冒号分隔的“字段”来实现的,如果它与前面的行不同,则打印它.然后打印第二个“字段”,这是您的内容。如果您希望输出更精确地匹配,那么就很容易了:
awk -F: 'length() && $1!=s {s=$1;print s ":"} length() {gsub(/^ /,"",$2);print $2}' input.txt我们使用length()作为条件,因为否则$1!=s测试将匹配空行。
或者,您可以单独在bash中这样做,实现类似的逻辑:
while IFS=: read a b; do [[ -n "$a" ]] && [[ $s != $a ]] && s="$a" && printf '%s:\n' "$s"; printf '%s\n' "${b## }"; done < input.txt或者为了更容易阅读:
while IFS=: read a b; do
if [[ -n "$a" ]] && [[ $s != $a ]]; then
s="$a"
printf '%s:\n' "$s"
fi
printf '%s\n' "${b## }"
done < input.txt我已经将更复杂的格式包含在这个格式中。
发布于 2019-02-14 15:41:07
另一个锥
$ awk -F: ' { if($1!=p) { print $1; p=$1; } $1=""; print }' bantay.txt
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
$https://stackoverflow.com/questions/54682642
复制相似问题