Awk: search and replace

I have a file which requires modification via a shell script.

Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. going forward, search for another keyword. 3. Replace this line with user supplied input.

for e.g., my file has the following text:

(some text)
(some text)
(text1_to_search)
(some text)
(text2_to_search_and_replace)
(some text)
(some text)

I need to search the file for a keyword and rewrite the file replace the matching the next search line with the input to this script leaving the remaining content untouched.

Regards

I think sed will be a good choice for what you want to do.

Are you wanting the script to ask for user input, and then change based on that?

I can help you with a script that does that.

Yes.

Actually i am doing 2 changes in a single file.

awk '$0 ~ /\"Search_string\"/ {print; getline; print replacement1; next} {print} ' replacement1="sometext" origFile > tmpfile1

awk '/search_string2/,/Search_String3/ {if ($0 ~ /Search_String3/){print replacement2; next}}; {print}' replacement2="sometext" tmpfile1 > tmpfile2

mv tmpfile2 origFile

Can i club these to happen in one go? Dont like the idea of creating multiple intermediate file.

I would actually suggest using see for this script. You can do inline editing. That means that you will replace files within the file and you won't have to create temp files and move them back to original.

I am in the middle of a meeting. I will respond with a sample script soon.

awk -v ip1="$INPUT1" -v ip2="$INPUT2" '{gsub( /String1/, ip1);gsub( /String2/, ip2);print}' file

regards,
kumar

Try this (may not work on ALL awk implementations):

awk '/text1.*search/{L=1} /text2.*search/ && L {getline < "-"; L=0}1' file

@brianjb

Please provide that sample script you were talking about.

Regards