首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >组织Grep输出

组织Grep输出
EN

Stack Overflow用户
提问于 2019-02-14 03:08:09
回答 3查看 152关注 0票数 1

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

我有这个(没有子弹):

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-14 03:26:19

编辑:,如ghoti先生的评论,添加了一个解决方案,这将提供一个输出顺序的开关关键字,并应在任何awk也工作。

代码语言:javascript
复制
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很适合这个问题。

代码语言:javascript
复制
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

说明:在这里添加解释。

代码语言:javascript
复制
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.
票数 0
EN

Stack Overflow用户

发布于 2019-02-14 03:59:52

一个简单的awk解决方案可能如下所示:

代码语言:javascript
复制
awk -F: 'length() && $1!=s {s=$1;print s} {print $2}' input.txt

这是通过捕获第一个冒号分隔的“字段”来实现的,如果它与前面的行不同,则打印它.然后打印第二个“字段”,这是您的内容。如果您希望输出更精确地匹配,那么就很容易了:

代码语言:javascript
复制
awk -F: 'length() && $1!=s {s=$1;print s ":"} length() {gsub(/^ /,"",$2);print $2}' input.txt

我们使用length()作为条件,因为否则$1!=s测试将匹配空行。

或者,您可以单独在bash中这样做,实现类似的逻辑:

代码语言:javascript
复制
while IFS=: read a b; do [[ -n "$a" ]] && [[ $s != $a ]] && s="$a" && printf '%s:\n' "$s"; printf '%s\n' "${b## }"; done < input.txt

或者为了更容易阅读:

代码语言:javascript
复制
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

我已经将更复杂的格式包含在这个格式中。

票数 0
EN

Stack Overflow用户

发布于 2019-02-14 15:41:07

另一个锥

代码语言:javascript
复制
$ 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

$
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54682642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档