Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Regex: Adding %20 to filenames in html tag to replace spaces

Status
Not open for further replies.

d3funct

MIS
Jul 13, 2000
313
US
I am creating an html document which is a documentation inventory of all server/project docs. To get this info I ran 'ls -ltRa /foo/documents'. I then pruned the output to provide only filenames and ran that through sed to get something like the following:

<a href=&quot; stats.doc&quot;> mailserver stats.doc</a>

This is a 6300 line file, and I need to replace the spaces with a %20 for the file to be recognized in the browser. I'm not a Perl or Regex GURU (yet), and would like to know of a regex which would replace the spaces found in the filenames (which is the only place there are spaces in this file) with a %20. I know &quot;there's more than one way to do it&quot; and would just like to be provided with one of them.

Thanks for any and all help. d3funct
zimmer.jon@cfwy.com
The software required `Windows 95 or better', so I installed Linux.
 
s| |%20|g;

will replace all instances of a space with %20. I only suggests this because you stated that in the entire cocument of the file name had a space. Becareful and back up file first. Hope this helps...
 
I'm sorry I was a wrong about the spaces, spaces DO exist elsewhere in the file mainly they are spaces at &quot;<a href=&quot; which I do not want a %20 entered in that space, which is what prompted my post. I had already tried s/ /%20/g but noticed that it made this output:
<a href= file.doc> foo file.doc</a>

look like this

<a%20href= foo%20file.doc</a>

but I want it to look like this:
<a href= foo file.doc</a>
d3funct
zimmer.jon@cfwy.com
The software required `Windows 95 or better', so I installed Linux.
 
I understand what you are saying now, and hoped it was not that way. hahaha.....donno, will have to think about it some more. Leaving work now...
 
there are many ways to do things, one way is to replace the space between <A and HREF with something UNIQUE and then replace all remaining spaces with %20, and then replace the unique thing with a space.

s/\<A HREF/<A*replaceme*HREF/g

s/\s/%20/g

s/*replaceme*/ /g

There are more effecient ways than this, but it's all I could think of this late at night:) Celia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top