user2435030
user2435030

Reputation: 35

append a line after pattern match ONLY after first occurrence

I have multiple file which looks like following

module ( 
input a;
output b;
);

xx inst_xx (
.in     (xxin),
.ou     (xxou)
);

assign a = b;

endmodule 

I want to append a line after ONLY after first occurrence of ); pattern

`include "defines.vh" 

so finally it should look like

module ( 
input a;
output b;
);
`include "defines.vh"

xx inst_xx (
    .in     (xxin),
    .ou     (xxou)
);

assign a = b;

endmodule 

Anything using sed or awk would be useful.

Upvotes: 2

Views: 1309

Answers (2)

potong
potong

Reputation: 58351

This might work for you (GNU sed):

sed $'/);/{a`include "defines.vh"\n;:a;n;ba}' file

or:

sed -e '/);/{a`include "defines.vh"' -e ':a;n;ba}' file

This matches the first ); and appends the required string then reads and prints the remainder of the file looping on the n command.

Upvotes: 1

jas
jas

Reputation: 10865

$ awk '{ print } !flag && /);/ { print "`include \"defines.vh\""; flag = 1 }' file
module (
input a;
output b;
);
`include "defines.vh"

xx inst_xx (
.in     (xxin),
.ou     (xxou)
);

assign a = b;

endmodule

Upvotes: 1

Related Questions