replace %20 in xml file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replace %20 in xml file
# 1  
Old 04-09-2011
Question replace %20 in xml file

Hi,

I'd like to use sed in order to replace %20 and other "special" characters that are represented with % and some number combination in xml file. Typical line looks like this:

/Users/imac1/Music/iTunes/iTunes 20Media/Music/The%20Rolling%20Stones/Forty%20Licks%20%5BDisc%202%5D/2-13%20Keys%20To%20Your%20Love.mp3

Thank you.

Andrej
# 2  
Old 04-09-2011
Code:
sed 's/%20/_/g' infile

or
Code:
sed 's/[%][0-9][0-9]*/_/g' infile

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 04-09-2011
Here I have removed the %20:
Code:
echo /Users/imac1/Music/iTunes/iTunes 20Media/Music/The%20Rolling%20Stones/Forty%20Licks%20%5BDisc%202%5D/2-13%20Keys%20To%20Your%20Love.mp3 | sed 's/%..//g'

output:
/Users/imac1/Music/iTunes/iTunes 20Media/Music/TheRollingStones/FortyLicksDisc2/2-13KeysToYourLove.mp3

replaced with eg. underscore:

Code:
echo /Users/imac1/Music/iTunes/iTunes  20Media/Music/The%20Rolling%20Stones/Forty%20Licks%20%5BDisc%202%5D/2-13%20Keys%20To%20Your%20Love.mp3  | sed 's/%../_/g'

output:
/Users/imac1/Music/iTunes/iTunes 20Media/Music/The_Rolling_Stones/Forty_Licks__Disc_2_/2-13_Keys_To_Your_Love.mp3
This User Gave Thanks to locoroco For This Post:
# 4  
Old 04-09-2011
Hey try with this

Code:
echo /Users/imac1/Music/iTunes/iTunes  20Media/Music/The%20Rolling%20Stones/Forty%20Licks%20%5BDisc%202%5D/2-13%20Keys%20To%20Your%20Love.mp3 | sed 's/%[0-9]*/_/g'


Last edited by posix; 04-09-2011 at 11:20 AM.. Reason: missed the _
This User Gave Thanks to posix For This Post:
# 5  
Old 04-09-2011
Code:
sed 's/%[25][0D]/_/g' infile

---------- Post updated at 04:26 PM ---------- Previous update was at 04:25 PM ----------

sed 's/%[25][0B]/_/g' infile
This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 04-09-2011
sed is not really suitable for decoding an encoded URL string.

Scripting languages such as Perl, Python and Ruby have modules for encoding and decoding URLs.

For example
Code:
!/usr/bin/perl

use URI::Escape;

my $string = "/Users/imac1/Music/iTunes/iTunes%20Media/Music/The%20Rolling%20Stones/Forty%20Licks%20%5BDisc%202%5D/2-13%20Keys%20To%20Your%20Love.mp3";
my $decode = uri_unescape($string);

print "Original string: $string\n";
print "Decoded  string: $decode\n";

Code:
$ ./demo.pl
Original string: /Users/imac1/Music/iTunes/iTunes%20Media/Music/The%20Rolling%20Stones/Forty%20Licks%20%5BDisc%202%5D/2-13%20Keys%20To%20Your%20Love.mp3
Decoded string: /Users/imac1/Music/iTunes/iTunes Media/Music/The Rolling Stones/Forty Licks [Disc 2]/2-13 Keys To Your Love.mp3

This User Gave Thanks to fpmurphy For This Post:
# 7  
Old 04-09-2011
python has urllib(2) modules to handle url en/decoding too:

Code:
#!/usr/bin/python
import sys
import urllib2
print "\nResult:"
print urllib2.unquote(sys.argv[1])


Code:
kent$ python p.py /Users/imac1/Music/iTunes/iTunes%20Media/Music/The%20Rolling%20Stones/Forty%20Licks%20%5BDisc%202%5D/2-13%20Keys%20To%20Your%20Love.mp3

Result:
/Users/imac1/Music/iTunes/iTunes Media/Music/The Rolling Stones/Forty Licks [Disc 2]/2-13 Keys To Your Love.mp3

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string in a xml file

Hello, I have below xml file, I want to find line default-value and replace the string within quotes followed by default-value "moni/Websphere/". Replace moni/Websphere/ with monitor/AMQ/ <monitor> <name>WebsphereMqMonitor</name> <type>managed</type> <argument... (4 Replies)
Discussion started by: prince1987
4 Replies

2. Red Hat

How to replace Ip address in .xml file through shell script?

I have one .xml file. which contains the following line. <ParamString StringId="PortAddress" StringValue="172.27.166.170" /> <ParamString StringId="PortAddress" StringValue="172.27.166.171" /> <ParamString StringId="PortAddress" StringValue="172.27.166.202" /> <ParamString... (9 Replies)
Discussion started by: Anjan Ganguly
9 Replies

3. Shell Programming and Scripting

Replace the .txt file between two strings in XML file

Hi i am having XML file with many number of lines,I need to replace between two strings with .txt file using awk. For ex <PersonInfoShipTo ------------------------------ /> My requirement is to replace the content between <PersonInfoShipTo ------------------------------ /> help me. Thanks... (9 Replies)
Discussion started by: Padmanabhan
9 Replies

4. Shell Programming and Scripting

Need to replace particular content in a xml file

Hi, My requirement is to find a text and replace it with another in a XML file. I am new to Unix,Please provide some suggestion to achieve. Find: <Style ss:ID="ColumnHeader1"> Replace with: <Style ss:ID="ColumnHeader1"> <Borders> <Border ss:Position="Bottom"... (4 Replies)
Discussion started by: cnraja
4 Replies

5. Shell Programming and Scripting

Find/Replace in XML file

I am not sure how to approach this find/replace using a shell script. Any help or guidance appreciated. I want to find this: <objects/> <thumb>thumb_0001.jpg</thumb> <preview>preview_0001.jpg</preview> And replace with something like this: <objects>... (1 Reply)
Discussion started by: jimraynor
1 Replies

6. Shell Programming and Scripting

How can i use sed to replace a keyword in an xml file?

Hello Unix gurus! I'm a unix newbie. Can I use sed to replace a keyword in an xml file and convert this keyword with an output of a unix cat command? for example: <test>keyword</test> and temp.txt = hello! I would like to replace "keyword" with the output of "cat temp.txt". I think... (6 Replies)
Discussion started by: 4dirk1
6 Replies

7. Shell Programming and Scripting

Search and replace in xml file using awk..

Hi All, I have xml file,i am tring to use awk to search pattern as: <Password>x</Password> and Replace with: <Password>y</Password> please any one can help to solve this using awk and awk only. (4 Replies)
Discussion started by: islam2666
4 Replies

8. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . scriptName.sh wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

9. Shell Programming and Scripting

replace nth value in xml file

Hi all, I have application.xml file with following content <dependency> <groupId>fr.orange.portail.ear</groupId> <artifactId>_AdminServicesEAR</artifactId> <version>1.0.0-20080521.085352-1</version> <type>ear</type> </dependency> <dependency> ... (4 Replies)
Discussion started by: subin_bala
4 Replies

10. Shell Programming and Scripting

serach and replace file name in the path in a remote xml file

hi every one , here is my problem !! i have to run my script from an account and update the result in a xml file located on a different account. i use existing ssh keys to do it remotely for example the tags looks like this <PropertyValueList... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies
Login or Register to Ask a Question