-1

I have the following input in a sacro.sql file:

{ 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";

I want to replace 2nd line above "create table "informix".sacro_log" with test

Example output I want:

{ 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";

2 Answers2

1
sed '/create table "informix"/i\
test
' data.in >data.out

This will create data.out with the following contents:

{ 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
);

The i ("insert") command in sed will insert the specified text before the matched pattern. The text inserted must follow a literal newline (GNU sed accepts sed '/pattern/i text' too).

Kusalananda
  • 333,661
0

I do not know if yu are still waiting for an answer, this could help.

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
Walter A
  • 736