sed 仅在一定限制内附加到特定行

sed 仅在一定限制内附加到特定行

我只想在特定行的匹配之后附加特定文本。

目前,在示例中可以看到我添加了这一行rpmos xi92;我可以sed按照这个使用附加开关吗锻炼

示例文本

module sac1 ( out_ports , in_ports );
    output out_ports;
    input in_ports;
    pmos_bulk_vt3  xi92 ( net307, rdb, rblt, vnwa );
    pmos_bulk_vt3  xi82 ( vddai, sel_b, net287, vnwa );
endmodule

module sac2 ( out_ports , in_ports );
    output out_ports;
    input in_ports;
    pmos_bulk_vt3  xi92 ( net307, rdb, rblt, vnwa );
    pmos_bulk_vt3  xi82 ( vddai, sel_b, net287, vnwa );
endmodule

命令

sed -i '/module sac1/ , /endmodule/ {/pmos_bulk_vt3  xi92/a rpmos   xi92 \( net307\, rblt\, rdb \)\;}'

所需文本

module sac1 ( out_ports , in_ports );
    output out_ports;
    input in_ports;
    pmos_bulk_vt3  xi92 ( net307, rdb, rblt, vnwa );
    rpmos  xi92 (net307, rblt, rdb );
    pmos_bulk_vt3  xi82 ( vddai, sel_b, net287, vnwa );
endmodule

module sac2 ( out_ports , in_ports );
    output out_ports;
    input in_ports;
    pmos_bulk_vt3  xi92 ( net307, rdb, rblt, vnwa );
    pmos_bulk_vt3  xi82 ( vddai, sel_b, net287, vnwa );
endmodule

答案1

命令后的所有字符都a被视为要附加的字符串,因此sed会抱怨unmatched `{'

一种方法是使用-e(用 测试过GNU sed,不确定可移植性)

$ sed -e '/module sac1/ , /endmodule/ {/pmos_bulk_vt3  xi92/a\    rpmos   xi92 (net307, rblt, rdb );' -e '}' ip.txt    
module sac1 ( out_ports , in_ports );
    output out_ports;
    input in_ports;
    pmos_bulk_vt3  xi92 ( net307, rdb, rblt, vnwa );
    rpmos   xi92 (net307, rblt, rdb );
    pmos_bulk_vt3  xi82 ( vddai, sel_b, net287, vnwa );
endmodule

module sac2 ( out_ports , in_ports );
    output out_ports;
    input in_ports;
    pmos_bulk_vt3  xi92 ( net307, rdb, rblt, vnwa );
    pmos_bulk_vt3  xi82 ( vddai, sel_b, net287, vnwa );
endmodule
  • 请注意,\不需要像(替换部分那样转义字符

或者你也可以使用

$ sed '/module sac1/ , /endmodule/ {/pmos_bulk_vt3  xi92/a\    rpmos   xi92 (net307, rblt, rdb );
}' ip.txt

相关内容