Awk/sed 2 种模式之间 replacing/prepending 行子集的解决方案

Awk/sed solution for replacing/prepending subset of lines in between 2 patterns

我想对两个模式(EAST_40_1_EP00 &Arr 和#pchan 5)之间的特定行使用 sed/awk 仅 edit/prepend。这是文本块:

   EAST_40_1_EP00 &Arr{            # template for 6-channel units at BRTT 
                            #       nothing specified means to use global parameters
            pchan_map &Arr{         #These map q330 physical channels to SEED net-sta-chan-loc codes
            #       pchan   net_sta_chan[_loc]      calib   calper  segtype 
                     0       $DLNET_$DLSTA_BHZ_00    $DB
                     1       $DLNET_$DLSTA_BHN_00    $DB
                     2       $DLNET_$DLSTA_BHE_00    $DB
                     0       $DLNET_$DLSTA_LHZ_00    $DB
                     1       $DLNET_$DLSTA_LHN_00    $DB
                     2       $DLNET_$DLSTA_LHE_00    $DB
                     3       $DLNET_$DLSTA_BHZ_01    $DB
                     4       $DLNET_$DLSTA_BHN_01    $DB
                     5       $DLNET_$DLSTA_BHE_01    $DB
                     3       $DLNET_$DLSTA_LHZ_01    $DB
                     4       $DLNET_$DLSTA_LHN_01    $DB
                     5       $DLNET_$DLSTA_LHE_01    $DB
            }
            acq_matrix &Tbl{                                # acquisition matrix
            #       str0 str1 str2 str3 str4 str5 str6 str7 
            #       1    10   20   40   50   100  200  
                    m1   0    0    m40  0    0    0    0  # pchan 0
                    m1   0    0    m40  0    0    0    0  # pchan 1
                    m1   0    0    m40  0    0    0    0  # pchan 2
                    m1   0    0    m40  0    0    0    0  # pchan 3
                    m1   0    0    m40  0    0    0    0  # pchan 4
                    m1   0    0    m40  0    0    0    0  # pchan 5
            }
    }

我想使用 sed/awk 在第 11-16 行和第 24,26 行添加注释标记“#”,因此文本块如下所示:

  EAST_40_1_EP00 &Arr{            # template for 6-channel units at BRTT 
                            #       nothing specified means to use global parameters
            pchan_map &Arr{         #These map q330 physical channels to SEED net-sta-chan-loc codes
            #       pchan   net_sta_chan[_loc]      calib   calper  segtype 
                     0       $DLNET_$DLSTA_BHZ_00    $DB
                     1       $DLNET_$DLSTA_BHN_00    $DB
                     2       $DLNET_$DLSTA_BHE_00    $DB
                     0       $DLNET_$DLSTA_LHZ_00    $DB
                     1       $DLNET_$DLSTA_LHN_00    $DB
                     2       $DLNET_$DLSTA_LHE_00    $DB
            #         3       $DLNET_$DLSTA_BHZ_01    $DB
            #         4       $DLNET_$DLSTA_BHN_01    $DB
            #         5       $DLNET_$DLSTA_BHE_01    $DB
            #         3       $DLNET_$DLSTA_LHZ_01    $DB
            #         4       $DLNET_$DLSTA_LHN_01    $DB
            #         5       $DLNET_$DLSTA_LHE_01    $DB
            }
            acq_matrix &Tbl{                                # acquisition matrix
            #       str0 str1 str2 str3 str4 str5 str6 str7 
            #       1    10   20   40   50   100  200  
                    m1   0    0    m40  0    0    0    0  # pchan 0
                    m1   0    0    m40  0    0    0    0  # pchan 1
                    m1   0    0    m40  0    0    0    0  # pchan 2
            #        m1   0    0    m40  0    0    0    0  # pchan 3
            #        m1   0    0    m40  0    0    0    0  # pchan 4
            #        m1   0    0    m40  0    0    0    0  # pchan 5
            }
    }

如果必须的话,我不介意执行两个单独的命令,但一个是理想的。我有两个命令来提取子集行,但我不确定如何修改它们以使其编辑这些行的实际文件:

  sed -n "/EAST_40_1_EP00 &Arr/,/#pchan 5/p" q3302orb_test.pf | sed -n '11,16p' | awk '{print "#"[=14=] }' file

  sed -n "/EAST_40_1_EP00 &Arr/,/#pchan 5/p" q3302orb_test.pf | sed -n '24,26p' | awk '{print "#"[=14=] }' file

有人能帮我解决一下我将如何 1) 将这些命令组合成一个命令,以及 2) 编辑文件本身而不仅仅是打印出行。我试过在不同的地方使用 -i 命令,但没有成功。

提前致谢!

#!/usr/bin/awk -f
BEGIN {
  alpha[11] alpha[12] alpha[13] alpha[14] alpha[15] alpha[16]
  alpha[24] alpha[26]
}
/EAST_40_1_EP00 &Arr/ {
  bravo = 1
  charlie = NR
}
/#pchan 5/ {
  bravo = 0
}
bravo && NR-charlie+1 in alpha {
  [=10=] = "#" [=10=]
}
1
  1. 设置好线数组
  2. 找到起始标记后,将其保存
  3. 同时保存开始标记的行号
  4. 设置停止标记
  5. 标记之间,如果行号好,添加注释
  6. 打印