Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a config.xml file which shows:

<userid>myUserId</userid>
<password>myPass</password>

How can I replace myUserId and myPass by using sed?

share|improve this question
1  
replace myUserID and myPass with what? –  Avinash Raj Apr 6 at 17:05
    
with the argument I passed, let say I execute a shell script called "changeUser.sh" ./changeUser.sh myname mypass –  taymindis Woon Apr 6 at 17:07
1  
Do you know the initial value of myUserId and myPass or do you want to place your own values between the tags without knowing what the previous entries were? If so then you're looking for an xml parser. This Q&A might be helpful. –  Glutanimate Apr 6 at 17:38
    
What is the occurrence of those tags, do they appear only once on the entire file? –  Lucio Apr 6 at 18:12
    
How do I apply sed -n '/<serverName/,/<\/serverName/p' big_xml_file.xml In my scenario??how to replace it without knowing what was it? –  taymindis Woon Apr 7 at 0:06

3 Answers 3

sed "s/myUserId/$1/;s/myPass/$2/" originalfile 
share|improve this answer
    
Could you explain? –  Braiam Apr 6 at 17:56
    
if you run the script as: ./changeUser.sh ID PASS, then positional arguments $0, $1, $2 are ./changeUser.sh, ID, PASS. Use ' ' for literals, and use " " for variables. s means substitute. –  user3503167 Apr 6 at 18:02
    
What if I don't know what was my userId and password in between tag? –  taymindis Woon Apr 7 at 0:08
    
probably you can use a regular expression, something like "<password>.*</password>" to match. –  user3503167 Apr 7 at 4:08

Solution Found

echo "cat //body/username" | xmllint --shell config.xml | sed '/^\/ >/d' | sed 's/<[^>]*.//g'

Can anyone explain this complex regex to me (^_^")

share|improve this answer
sed 's/old/new/g' test.txt > test1.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.