我只想打印GigabiteInternet接口有描述的行。我用过:
cat filter.txt | awk '/interface/ || /description/'
所以作为filter.txt的输出,我想看到以下内容。
interface GigabitEthernet0/26
description test1
interface GigabitEthernet0/27
description test2
interface GigabitEthernet0/28
description test3
interface GigabitEthernet0/29
description test 4
tst:~$ cat filter.txt
interface Port-channel1
interface FastEthernet0
interface GigabitEthernet0/1
interface GigabitEthernet0/2
interface GigabitEthernet0/3
interface GigabitEthernet0/4
interface GigabitEthernet0/5
interface GigabitEthernet0/6
interface GigabitEthernet0/7
interface GigabitEthernet0/8
interface GigabitEthernet0/9
interface GigabitEthernet0/10
interface GigabitEthernet0/11
interface GigabitEthernet0/12
interface GigabitEthernet0/13
interface GigabitEthernet0/14
interface GigabitEthernet0/15
interface GigabitEthernet0/16
interface GigabitEthernet0/17
interface GigabitEthernet0/18
description test1
interface GigabitEthernet0/19
description test2
interface GigabitEthernet0/20
interface GigabitEthernet0/21
interface GigabitEthernet0/22
interface GigabitEthernet0/23
interface GigabitEthernet0/24
interface GigabitEthernet0/25
interface GigabitEthernet0/26
description test3
interface GigabitEthernet0/27
description test4
interface GigabitEthernet0/28
description test5
interface GigabitEthernet0/29
description test6
interface GigabitEthernet0/30
谢谢你的帮助
最佳答案
#use sed
sed -n '/interface/h;/description/{s/^ //;H;x;p}' file.txt
#use awk
awk '/interface/{s=$0;}/description/{$1=$1;print s RS $0 ;s="";}' file.txt
#output:
interface GigabitEthernet0/18
description test1
interface GigabitEthernet0/19
description test2
interface GigabitEthernet0/26
description test3
interface GigabitEthernet0/27
description test4
interface GigabitEthernet0/28
description test5
interface GigabitEthernet0/29
description test6
关于linux - 如何在多行Linux上进行精确匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40375829/