Bash

列印與字元串匹配的所有行以及該字元串之後的一組行數

  • May 19, 2021

我想從日誌文件中提取交通路口的相關數據及其連接。範例日誌:

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
...
   

但是,我想在每個連結標題之後立即提取行,直到大量空白之前的節點的最終連結,並且不擷取從 Stage Suffix 開始直到下一個連結的任何內容。

有沒有辦法修改我的 BASH 腳本以在它找到的每個匹配實例之後包含額外數量的行?

這是你想要的嗎?

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                                     
                                                                               

引用自:https://unix.stackexchange.com/questions/650415