0

I am new to bash scripting and was looking into what kid of command will help me replace a specific string in an xml file.

The string looks like

uri='file:/var/lib/abc/cde.repo/r/c/e/v/1.1/abc-1.1.jar'

Should be replaced with

uri='file:/lib/abc-1.1.jar'

The strings vary as the jars vary too. First part of the string "file:/var/lib/abc/cde.repo/r/" is constant and is across all strings. The 2nd half is varying

This needs to be done across entire file. Please note that replacing one is easier then doing it for each an every string that varies. I am trying to look for solution to do it in one single command.

I know we can use sed but how? Any pointers are highly appreciated

Thanks

4 Answers 4

3

With sed:

sed "s~uri='file:/var/lib/abc/cde.repo/r/c/e/v/1\.1/abc-1.1.jar~uri='file:/lib/abc-1\.1\.jar'~g"

Basically it is:

sed "s~pattern~replacement~g"

where s is the command substitute and the trailing g means globally. I'm using ~ as the delimiter char as this helps to avoid escaping all that / in the paths. (thanks @Jotne)


Update: In order to make the regex more flexible, you may try this:

sed 's~file.*/\(.*\.jar\)\(.*\)~file:///lib/\1\2~' a.txt 

It searches for file: ... .jar links, grabs the name of the jar file and builds the new links.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks hek2mgl for the quick response! That was very quick. How can you do this repeatedly with first half of the string been static and second half keeps changing (as per question). I want to replace all strings in the file with similar format. 2nd string for example will look like uri='file:/var/lib/abc/cde.repo/t/e/s/t/v/1.9/zzz-1.9.jar' to be replace by 'uri='file:/lib/zzz-1.9.jar'
Try the following sed -re "s~uri='file:\S+/(\S+)'$~uri='file:/lib/\1'~g" file
@hek2mgl With nearly 23000 points here, you should know that sed can use different separator like this: sed "s~uri='file:/var/lib/abc/cde.repo/r/c/e/v/1\.1/abc-1.1.jar~uri='file:/lib/abc-1\.1\.jar~g" This way you do not need to escape all /
@Masti I have to leave for today. If you have patience until tomorrow I can help.. I guess the community might being faster.. :)
@Jotne I think text editing is a task for sed. Althought it is nicely possible with awk, sed should be used here. Or even xslt because regexes and html/xml aren't friends..... cu, and thanks for the ~ ... :)
|
2

Using awk you can do:

awk -F/ '/file:\/var\/lib\/abc\/cde.repo\/r/ {print $1,$3,$NF}' OFS=/ file
uri='file:/lib/abc-1.1.jar'

Static URL, but changing file name.

Comments

0

You do not need to use sed or even awk. You could simply use basename:

prefix='file:/lib/'
uri='file:/var/lib/abc/cde.repo/r/c/e/v/1.1/abc-1.1.jar'
result="${prefix}$(basename ${uri})"
echo ${result}

Comments

0

This worked for me :

sudo sed -i -e "s/stringToChange/TheNewString/g" test.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.