sed打印出所有数据而不是我要提取的替换部分

时间:2012-12-03 08:11:52

标签: regex sed

我知道,这个问题之前已经多次以不同的方式提出过。但我无法适应这些解决方案。 我有一个文件header.out,其中包含以下内容:

// some text before this 
            :type = "Primary Grid: Tau Format" ;
            :deformation_timestep = 0. ;
            :deformation_alpha = 0. ;
            :deformation_dalpha = 0. ;
            :deformation_h = 0. ;
            :deformation_dh = 0. ;
            :deformation_step = 0 ;
            :adaptation_level = 0 ;
            :connectivity_check = 1 ;
            :volume_check = 1 ;
            :point_check = 1 ;
            :test_lib_check = 1 ;
            :periodic_pairs_check = 1 ;
            :is_balanced = 0 ;
            :preserve_piles = 0 ;
            :marker_1 = "WingMidTrailing" ;
            :marker_2 = "WingMidLower" ;
            :marker_3 = "WingMidUpper" ;
            :marker_4 = "WingSide" ;
            :marker_5 = "Farfield" ;
            :marker_6 = "SymmetryPlane" ;
            :marker_7 = "WingInnerLower" ;
            :marker_8 = "WingInnerUpper" ;
            :marker_9 = "WingInnerTrailing" ;
            :marker_10 = "WingOuterLower" ;
            :marker_11 = "WingOuterUpper" ;
            :marker_12 = "WingOuterTrailing" ;
 }

我需要提取属于字符串“SymmetryPlane”的标记的ID 所以在这种情况下我想要输出6。使用sed和正则表达式的所有尝试都失败了。 我试过的是:

sed 's/.*marker_\(.*\) = "SymmetryPlane.*/\1/' header.out

但这只给了我

            :type = "Primary Grid: Tau Format" ;
            :deformation_timestep = 0. ;
            :deformation_alpha = 0. ;
            :deformation_dalpha = 0. ;
            :deformation_h = 0. ;
            :deformation_dh = 0. ;
            :deformation_step = 0 ;
            :adaptation_level = 0 ;
            :connectivity_check = 1 ;
            :volume_check = 1 ;
            :point_check = 1 ;
            :test_lib_check = 1 ;
            :periodic_pairs_check = 1 ;
            :is_balanced = 0 ;
            :preserve_piles = 0 ;
            :marker_1 = "WingMidTrailing" ;
            :marker_2 = "WingMidLower" ;
            :marker_3 = "WingMidUpper" ;
            :marker_4 = "WingSide" ;
            :marker_5 = "Farfield" ;
6
            :marker_7 = "WingInnerLower" ;
            :marker_8 = "WingInnerUpper" ;
            :marker_9 = "WingInnerTrailing" ;
            :marker_10 = "WingOuterLower" ;
            :marker_11 = "WingOuterUpper" ;
            :marker_12 = "WingOuterTrailing" ;
}

我不确定为什么会这样。

提前致谢!

1 个答案:

答案 0 :(得分:2)

只需取消输出(-n)并在替换后打印(p):

sed -n 's/.*marker_\(.*\) = "SymmetryPlane.*/\1/p' header.out