sed Help Needed

I want to search texts between first occurence of the matching pattern and replace it with some other text.pls advice what can be done. I searched alot, i could not find anything relevant.
Ex my input is as follows:

red
yellow
grey
white
blue
red
pink
violet
white

I want to search text "while" between the first occurrence of the two pattern match "red" and "blue" and wnat to replace it with black. I do not want to replace it in second occurrence of matching pattern. Expected output is as follows:

red
yellow
grey
black
blue
red
pink
violet
white

Pls suggets how owuld i write one liner sed command.

sed 0,/white/{s/white/black/} filename

No,

This is not working, rather i knew this before.
You have not mentioned range for the pattern. I just gave sample input file, In reality "white" word can present multiple places in file. I wanna search white in first occurence of two matching pattern precisely (red and blue).

If awk is fine then try

$ awk '!f && /red/,/blue/{f=1; sub(/white/,"black")}1' file

Hi Akshay,

Command worked but can I exclude lines containing red and blue while getting result.
Now I could replace lines between those two search patten but it also replaces the texts which are present on those two search patterns.
Pls tell me script which will exclude lines containing words, red and blue.

I don't know how your original file looks, try this

$ awk '!f && /red/,/blue/{if(/red|blue/){if(/blue/){f=1}next}sub(/white/,"black")}1' file

Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.

Thank you.

The UNIX and Linux Forums

With the right order you don't need next

awk '
/blue/ {f=0}
f {sub(/white/,"black")}
/red/ {f=1}
1'

Hi Akshay,

Now Your command gives me output as follows:

-----------------------------------

<!-- outputEncoding The encoding to use for the page that results -->
<!-- from the SSI processing. [UTF-8] -->
<!-- -->
black
<servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>
</servlet-class>

---------------------------------------------------------------------

My original file is as follows(Including only block which is concerned for the editing:

--------------------------------------------------------

<!-- allowExec Is use of the exec command enabled? [false] -->
<!--
<servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>
org.apache.catalina.ssi.SSIServlet
</servlet-class>

--------------------------------------------------------

Output should have looked like this after executing your command
------------------------------------------------------------

<!-- allowExec Is use of the exec command enabled? [false] -->
<servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>
org.apache.catalina.ssi.SSIServlet
</servlet-class>

---------------------------------------------------------

I wanna replace <!-- between allowExec and SSIServlet. there are multiple occurrence of the block allowExec and SSIServlet. so wanna replace it in first occurrence only.

---------- Post updated at 11:23 PM ---------- Previous update was at 07:48 PM ----------

Hi Akshay,
Your command which you posted very first time is working fine but it replaces the text from the line which contains first matching pattern along with replacing it in between the lines containing the first and second pattern matching lines. I will modify my input file to explain the scenario.

For example:
my input file is :

red white
yellow
grey
white
blue
red
pink
violet
white

I want to have output as follows after search and replacement:

red white
yellow
grey
black
red
pink
violet
white

But the script which you posted first time generates the output as follows:

red black
yellow
grey
black
blue
red
pink
violet
white

Regarding the script which you posted second time, it produces irrelevant results.
Please modify your first script only and let me know to achieve desired output.

awk '
/blue/ {f=0}
f {sub(/white/,"black")}
/red/ && ++block==1 {f=1}
1' file