文件中包含空格和特殊字符的两个句子之间的模式识别?

文件中包含空格和特殊字符的两个句子之间的模式识别?

我有一个文件,我想在其中打印两个图案之间的所有线条。 Pattern1 =# Begin TRACE A Data和 Pattern 2 = # Done Data $capture,我想打印pattern1和pattern2之间的每一行。

文件一:

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

不应打印多余的行或空行,而应仅打印行数据。

答案1

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'

因为只有您想要的行包含逗号。

答案2

Awk解决方案:

awk '/# Begin TRACE A Data/{ f = 1; next }
     /# Done Data \$capture/{ f = 0 }
     NF && f{ gsub(/^P.+=|,| MHz/, ""); print }' file

输出:

-103.976000  2884.982910  
-103.580000  2884.997456  
-103.748000  2885.012001  
-104.020000  2885.026547  
-103.472000  2885.041092  
-103.720000  2885.055638  
-103.752000  2885.070183  
-103.512000  2885.084729  
-103.664000  2885.099274  
-103.948000  2885.113820  
-103.720000  2885.128365  
-103.480000  2885.142911 

相关内容