打印与字符串匹配的所有行以及该字符串之后的指定行数

打印与字符串匹配的所有行以及该字符串之后的指定行数

我想从日志文件中提取交通枢纽及其连接的相关数据。日志示例:

SCN DD1251       At Glasgow Road - Kilbowie Road                                
Modified                   By ________                                          
Type CR                                                                         
Region  WS  Subregion                                                           
            UPSTREAM               DOWNSTREAM               FILTER              
          NODE     LINK          NODE    LINK                LINK               
        DD1271      C           DD1271    R                                     
        DD1351      D           DD1351    B                                     
                    E                                                           
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
       Stage Suffix for Offset Optimizer  1                                     
Double Cycle Initially ?  N   Force Single / Double Cycling status ? N          
Double Cycle Group       00   Double Cycle Ignore ? N                           
Allow Link Max Saturation N   Link Max Sat Override N                           
Stages       1   2   3   4                                                      
Fixed        N   N   N   Y                                                      
LRT stage    N   N   N   N                                                      
Skip allowed N   N   N   N                                                      
Ped stage    N   N   N   N                                                      
Ped invite   N   N   N   N                                                      
Ghost stage  N   N   N   N                                                      
Offset authority pointer   0     Split authority pointer   0                    
Offset opt emiss weight  000     I/green feedback inhibit  N                    
                                                                                
                                                                                
Bus Authority   00  ACIS node  00000                                            
                                                                                
Bus Mode - Central extensions  N   Local extensions  N  Recalls  N              
           Stage skipping      N   Stage truncation  N Cancels   N              
Bus Priority Selection - Multiple buses  N  Queue Calculation  N                
Hold recall if faulty N  Disable recall  N  Disable long jtim  N  Real Cancel N 
                                                                                
Bus recall recovery type      0  Bus extension recovery type  0                 
Offset Bus authority pointer  0  Split Bus authority pointer  0                 
Bus skip recovery             0  Skip importance factor       0                 
Bus priority status   OFF                                                       
LRT sat 1  000  LRT sat 2  000  LRT sat 3  000                                  
                                                                                
                                                                                
                  PEDESTRIAN FACILITIES                                         
Ped Node                   N        Num Ped Wait Imp Factor     000             
Ped Priority               0        Max Ped Priority Freq        00             
Ped Lower Sat Threshold  000        Ped Upper Sat Threshold     000             
Max Ped Wait Time        000                                                    
               PEDESTRIAN VARIABLE INVITATION TO CROSS                          
Allow Ped Invite       N            Ped Priority Auto           000             
Ped Invite Upper Sat 000 Prio Level 1    2    3    4                            
Max Ped Priority Smoothed Time     000  000  000  000                           
Max Ped Priority Increase Length    00   00   00   00                           
                  CYCLE TIME FACILITIES                                         
Allow Node Independence    N        Operator Node Independence    0             
Ghost Demand Stage         N        Num Ghost Assessment Cycles  15             
Upper Trigger Ghost       04        Lower Trigger Ghost           0             
                                                                                
SCN DD1271       At Glasgow Road - Hume Street                                  
Modified 13-OCT-15 15:06   By BDAVIDSON                                         
Type CR                                                                         
Region  WS  Subregion                                                           
            UPSTREAM               DOWNSTREAM               FILTER              
          NODE     LINK          NODE    LINK                LINK               
        DD1301      T           DD1301    A                                     
        DD1251      R           DD1251    C                                     
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
       Stage Suffix for Offset Optimizer  1                                     
Double Cycle Initially ?  N   Force Single / Double Cycling status ? N          
Double Cycle Group       00   Double Cycle Ignore ? N                           
Allow Link Max Saturation N   Link Max Sat Override N                           
Stages       1   2   3                                                          
Fixed        N   Y   Y                                                          
LRT stage    N   N   N                                                          
Skip allowed N   N   N                                                          
Ped stage    N   N   N                                                          
Ped invite   N   N   N                                                          
Ghost stage  N   N   N                                                          
Offset authority pointer   0     Split authority pointer   0                    
Offset opt emiss weight  000     I/green feedback inhibit  N                    
                                                                                
                                                                                
Bus Authority   00  ACIS node  00000                                            
                                                                                
Bus Mode - Central extensions  N   Local extensions  N  Recalls  N              
           Stage skipping      N   Stage truncation  N Cancels   N              
Bus Priority Selection - Multiple buses  N  Queue Calculation  N                
Hold recall if faulty N  Disable recall  N  Disable long jtim  N  Real Cancel N 
                                                                                
Bus recall recovery type      0  Bus extension recovery type  0                 
Offset Bus authority pointer  0  Split Bus authority pointer  0                 
Bus skip recovery             0  Skip importance factor       0                 
Bus priority status   OFF                                                       
LRT sat 1  000  LRT sat 2  000  LRT sat 3  000                                  
                                                                                
                                                                                
                  PEDESTRIAN FACILITIES                                         
Ped Node                   N        Num Ped Wait Imp Factor     000             
Ped Priority               0        Max Ped Priority Freq        00             
Ped Lower Sat Threshold  000        Ped Upper Sat Threshold     000             
Max Ped Wait Time        000                                                    
               PEDESTRIAN VARIABLE INVITATION TO CROSS                          
Allow Ped Invite       N            Ped Priority Auto           000             
Ped Invite Upper Sat 000 Prio Level 1    2    3    4                            
Max Ped Priority Smoothed Time     000  000  000  000                           
Max Ped Priority Increase Length    00   00   00   00                           
                  CYCLE TIME FACILITIES                                         
Allow Node Independence    N        Operator Node Independence    0             
Ghost Demand Stage         N        Num Ghost Assessment Cycles  15             
Upper Trigger Ghost       04        Lower Trigger Ghost           0    

我已经可以使用以下 Bash 脚本提取第一个相关行:

grep SCN* LOG.TXT > JUNCTIONS.txt

它创建了所有连接点的列表,如下所示:

SCN DD1251       At Glasgow Road - Kilbowie Road                                
SCN DD1271       At Glasgow Road - Hume Street                                  
SCN DD1301       At Glasgow Road - Argyll Road - Cart Street                    
SCN DD1351       At Kilbowie Road - Chalmers Street
...
    

但是,我想在每个链接标题之后立即提取行,直到大量空白之前的节点的最终链接,并且不捕获从阶段后缀开始直到下一个链接的任何内容。

有没有办法修改我的 BASH 脚本,使其在找到的每个匹配实例之后包含额外的行数?

答案1

这是你想要的吗?

sed -n '/^SCN/,/^\s*$/p' LOG.TXT

它打印两个模式之间的线(以及包含它们的线):

  • 'SCN' 出现在行首 ( ^SCN)
  • 以及包含 0 个或多个空白字符的空行 ( ^\s*$)

并输出以下内容:

SCN DD1251       At Glasgow Road - Kilbowie Road                                
Modified                   By ________                                          
Type CR                                                                         
Region  WS  Subregion                                                           
            UPSTREAM               DOWNSTREAM               FILTER              
          NODE     LINK          NODE    LINK                LINK               
        DD1271      C           DD1271    R                                     
        DD1351      D           DD1351    B                                     
                    E                                                           
                                                                                
SCN DD1271       At Glasgow Road - Hume Street                                  
Modified 13-OCT-15 15:06   By BDAVIDSON                                         
Type CR                                                                         
Region  WS  Subregion                                                           
            UPSTREAM               DOWNSTREAM               FILTER              
          NODE     LINK          NODE    LINK                LINK               
        DD1301      T           DD1301    A                                     
        DD1251      R           DD1251    C                                     
                                                                                

答案2

在你的例子中,awk你会想到一个程序:

awk '/^SCN/{f=1} !NF{f=0} f' LOG.TXT > JUNCTIONS.TXT

这会

  • 设置一个标志f1当行开始时SCN
  • 将标志重置为0找到空行时(在这种情况下,我们通过强加没有找到文本字段的条件来允许“视觉上空”行,而不是根本不存在任何字符)
  • 仅当标志为时才打印当前行1

稍微简单一些,使用地址范围而不是显式标志:

awk '/^SCN/,/^[[:space:]]*$/' LOG.TXT > JUNCTIONS.TXT

您的示例的输出将是:

SCN DD1251       At Glasgow Road - Kilbowie Road                                
Modified                   By ________                                          
Type CR                                                                         
Region  WS  Subregion                                                           
            UPSTREAM               DOWNSTREAM               FILTER              
          NODE     LINK          NODE    LINK                LINK               
        DD1271      C           DD1271    R                                     
        DD1351      D           DD1351    B                                     
                    E                                                           

SCN DD1271       At Glasgow Road - Hume Street                                  
Modified 13-OCT-15 15:06   By BDAVIDSON                                         
Type CR                                                                         
Region  WS  Subregion                                                           
            UPSTREAM               DOWNSTREAM               FILTER              
          NODE     LINK          NODE    LINK                LINK               
        DD1301      T           DD1301    A                                     
        DD1251      R           DD1251    C                                

第一个示例不会打印分隔空白行。

相关内容