Awk

文件中包含空格和特殊字元的兩個句子之間的圖形辨識?

  • May 3, 2018

我有一個文件,我想在其中列印兩個模式之間的所有行。Pattern1 =# Begin TRACE A Data和 Pattern 2 = # Done Data $capture,我想列印 pattern1 和 pattern2 之間的每一行。

文件 1:

# Lower Limit 
LIMIT_FLAG=0
LIMIT_POINT0=2884982910000.000000 -102800 -1
LIMIT_POINT1=2892982910000.000000 -102800 -1

# Limit Done

# Begin SPA Emission Mask
MASK SEGMENTS=0
MASK REFERENCE MODE=0
MASK REFERENCE LEVEL=0
MASK CENTER FREQUENCY=0
**
# SPA Emission Mask Done

# Begin SPA Data
<AP    P_DATA>

** # Begin TRACE A Data **  
P_0=-103.976000 , 2884.982910 MHz  
P_1=-103.580000 , 2884.997456 MHz  
P_2=-103.748000 , 2885.012001 MHz  
P_3=-104.020000 , 2885.026547 MHz  
P_4=-103.472000 , 2885.041092 MHz  
P_5=-103.720000 , 2885.055638 MHz  
P_6=-103.752000 , 2885.070183 MHz  
P_7=-103.512000 , 2885.084729 MHz  
P_8=-103.664000 , 2885.099274 MHz  
P_9=-103.948000 , 2885.113820 MHz  
P_10=-103.720000 , 2885.128365 MHz  
P_11=-103.480000 , 2885.142911 

# Done Data $capture
# Begin SPA Emission Mask
MASK SEGMENTS=0
MASK REFERENCE MODE=0
MASK REFERENCE LEVEL=0
MASK CENTER FREQUENCY=0

# End SPA Data
<APP_DATA_END>

# End SPA Data
<APP_DATA_END>

預期輸出:

-103.976000   2884.982910
-103.580000   2884.997456
-103.748000   2885.012001
-104.020000   2885.026547
-103.472000   2885.041092
....
....

不應列印額外的行或空白行,而應僅列印行數據。

sed -n '/# Begin TRACE A Data/,/# Done Data $capture/{s/ MHz//;s/,/ /;s/.*=//p;}' filename
  • /pattern1/,/pattern2/僅選擇從第一個模式到第二個模式的行,因此內部的所有內容{}僅針對該範圍執行
  • s/ MHz//刪除尾隨單元
  • s/,/ /用空格替換逗號
  • s/.*=//p刪除所有內容=並列印模式,因此只有範圍內的行=才會被列印(選項-n抑制預設輸出)

實際上,對於您的範例數據,您也可以這樣做

sed -n 's/ MHz//;s/.*=//;s/,/ /p'

因為只有您想要的行包含逗號。

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