0

How I pass the arguments to sed command to replace a particular string . I want to replace 8080 with some different port like 8181 or 8282.

grep -rl 8080 /tmp/standalone.xml | xargs sed -i 's/8080/8181/g'
7
  • 8181 is dynamic value. port="$1" grep -rl 8080 /tmp/standalone.xml | xargs sed -i 's/8080/"$port"/g' It will replace 8080 with "$port" not with the inputs Commented Feb 24, 2015 at 11:20
  • 1
    Just use double quotes: sed "s/8080/$port/g", this way the $port will be expanded to its actual value. Commented Feb 24, 2015 at 11:29
  • 1
    This is one where you have the correct tool for the job, and the question is "How do I use it?". Basic substitution is covered in a cursory fashion in the man page, but there are literally 100's of good sed tutorials/examples on the web. This is a valid question, but it is borderline between getting dinged for not doing due-diligence before asking the question. No downvotes, but be aware, stackoverflow, as a programming site, really encourages you to look before you ask. Just guidance for the future -- Welcome to SO Commented Feb 24, 2015 at 11:38
  • 1
    Is /tmp/standalone.xml a folder? Commented Feb 24, 2015 at 11:39
  • No no luck.. name="http" port=""$port"" Commented Feb 24, 2015 at 11:56

2 Answers 2

1

You want to use double quotes instead of single as variables can then be expanded, something like:

 grep -rl 8080 /tmp/standalone.xml | xargs sed -i "s/8080/$port/g"
Sign up to request clarification or add additional context in comments.

2 Comments

port=""$port"" this was coming instead of 8181 or 8222
What exact command are you running, @SANU ? It looks like you are using extra double quotes, while Paul's code should be more than enough.
0

You can sed directly

sed -i "s/$oldValue/$newValue/g" /tmp/standalone.xml

or

sed -i "s/8080/8181/g" /tmp/standalone.xml

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.