我在文件中有以下输入sacro.sql
:
{ TABLE "informix".sacro_log row size = 64 number of columns = 3 index size = 0 }
{ unload file name = sacro00518.unl number of rows = 0 }
create table "informix".sacro_log
(
log_id serial not null constraint "informix".nnc_sac_log00,
log_type integer,
log_data text
);
revoke all on "informix".sacro_log from "public" as "informix";
我想用测试替换“创建表“informix”.sacro_log”上面的第二行
我想要的示例输出:
{ TABLE "informix".sacro_log row size = 64 number of columns = 3 index size = 0 }
test
create table "informix".sacro_log
(
log_id serial not null constraint "informix".nnc_sac_log00,
log_type integer,
log_data text
);
revoke all on "informix".sacro_log from "public" as "informix";
答案1
sed '/create table "informix"/i\
test
' data.in >data.out
这将创建data.out
包含以下内容的内容:
{ TABLE "informix".sacro_log row size = 64 number of columns = 3 index size = 0 }
{ unload file name = sacro00518.unl number of rows = 0 }
test
create table "informix".sacro_log
(
log_id serial not null constraint "informix".nnc_sac_log00,
log_type integer,
log_data text
);
i
中的(“插入”)命令将sed
在匹配的模式之前插入指定的文本。插入的文本必须遵循文字换行符(GNU也sed
接受sed '/pattern/i text'
)。
答案2
我不知道你是否仍在等待答案,这可能会有所帮助。
awk -v newline=test -v trigger='create table "informix"' '
{a[NR]=$0}
NR>2 { if (index($0,trigger)>0) { a[NR-2]=newline; }}
END {
for (i=1;i<=NR;i++) { print a[i] }
}
' sacro.sql