Reputation: 413
I have a file which requires modification via a shell script.
Need to do the following: 1. search for a keyword in the file. 2. replace the next line to this keyword with my supplied line text.
for e.g., my file has the following text:
(some text) (some text) (text_to_search) (text_to_replace) (some text) (some text) (some text)
I need to search the file for and rewrite the file replace the line leaving the remaining content untouched.
How can this be done?
Regards
Upvotes: 1
Views: 674
Reputation: 16399
awk ' zap==1 {print "my new line goes here"; zap=0; next}
/my pattern/ {zap=1; print $0; next}
{print $0} ' infile > newfile
Assuming I got what you wanted....
var="this is the line of text to insert"
awk -v lin="$var" ' zap==1 {print lin ; zap=0; next}
/my pattern/ {zap=1; print $0; next}
{print $0} ' infile > newfile
the awk internal variable lin
, defined: -v lin="$var"
is named lin
, lin
comes from the external bash variable var
.
Upvotes: 2
Reputation: 2253
cat file
first
second
third
fourth
fiveth
set var = "inserted"
sed '/second/a\'$var file
first
second
inserted
third
fourth
fiveth
Upvotes: 0
Reputation: 2253
sed -i '/pattern/r/dev/stdin' file
the script will wait for us to enter the newline and press ctrl +d interactively +
Upvotes: 0