组织Grep输出

时间:2019-02-14 03:08:09

标签: awk

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

我有这个(没有子弹):

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

3 个答案:

答案 0 :(得分:0)

编辑: 根据ghoti先生的评论,添加了一个解决方案,该解决方案将以switch关键字的顺序提供输出,并且也可以在任何catdog中使用。

dfoutput = pd.DataFrame({'col1': ['1', '2', '2', '3'],
                  'col2': ['10', '15', '20', '30'],
                   'cat': [0.2, None, 0.3, 0.5],
                    'dog': [None, 0.9, 0.3, 0.1],
                    'catdog': [None, None, 0.3, None]})


请问您可以试试看,看来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

答案 1 :(得分:0)

一个简单的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

我已经在其中添加了更复杂的格式。

答案 2 :(得分:0)

另一个awk

$ 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

$