0

How can I find the word "UP" and replace it with "DOWN" in below data using sed, awk or grep?

document.getElementById("p1").innerHTML = "API Dev :: UP";
document.getElementById("p2").innerHTML = "API QA :: UP";
document.getElementById("p3").innerHTML = "API Reg :: UP";
document.getElementById("p4").innerHTML = "API Prod :: UP";

I am looking to find/replace one line at a time. Something like using for loop and then find/replace one line after other.

Thanks!

1
  • 1
    Please explain one line at a time Commented Dec 14, 2016 at 4:53

3 Answers 3

1

This is sed 101:

sed 's/:: UP/:: DOWN/'
Sign up to request clarification or add additional context in comments.

3 Comments

how can we find/replace one line after the other, but not all line at the same time.
Why would you want to do that? It will be slow.
If I put above line in my script, it would replace all lines at once URL=grep "document.getElementById" index.html | awk -F' ' '{print $7}'` for var in $URL do SERVICE_STATUS=curl -s --head $URL | head -n 1 | awk '{print $2}' if [[ $SERVICE_STATUS -eq "200" ]] then sed 's/:: UP/:: DOWN/' index.html fi done`
0
echo line | sed 's/UP/DOWN/'

Where line is the string you want to replace

Comments

0

While @john-zwinck's answer gives you the solution for this, if you want to have a more restricted pattern, do something like below

 sed -E 's/(API[[:blank:]]*[[:alnum:]]+[[:blank:]]*::[[:blank:]]*)UP/\1DOWN/'
                                                                    filename

which will give you

document.getElementById("p1").innerHTML = "API Dev :: DOWN";
document.getElementById("p2").innerHTML = "API QA :: DOWN";
document.getElementById("p3").innerHTML = "API Reg :: DOWN";
document.getElementById("p4").innerHTML = "API Prod :: DOWN";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.