2016-11-02 49 views
0

我想僅打印接口Gigabitehternet有說明的行。我用:如何在多行上進行精確匹配linux

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 

感謝您的幫助

+0

大多數'接口GigabitEthernet0'在您需要的輸出沒有說明。 – SLePort

回答

2
#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 
+0

我認爲你可以改變你的第二個命令: 'awk'/interface/ {s = $ 0;} /(description)/ {print s RS $ 1; s =「」;}'./file.txt'。 它將刪除「說明」之前作爲提問者的空白。 –

+0

我不明白 –

+0

這是'(description)'之前的空格。 –