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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Script 1

Status
Not open for further replies.

kritlop

MIS
Jun 13, 2000
2
US
I need to write some kind of script that will process a text file, and convert any spaces to %20

Is there a way to do this using awk, or grep, or something?

Thanks.
 
The sed syntax would be sed -e 's/ /%20/g', somebody correct me if I'm wrong, I'm not much of a sed user, but in vi I'd use 1,$s/ /%20/g. There is a space after the "s/" and before the next "/".
 
Could be a problem in the interpretation of space. / / will replace every single space, i.e. if there are three spaces between two charecters then it will replace them with %20%20.
If this is required ok but if you need to replace all white space between two charecters with one occurance of %20

i.e.
222 222 222 becomes 222%20%20222%20%20222

I am thinking on this as I suspect I may encounter the problem myself.
 
Well, he said, "any spaces" so I figured that's what he was looking for.
I had run a script called "machine_info" that I copied from the "Essential Systems Administration" manual and edited to my needs on my RS6000's. When the script finished I had to break down spaces and do other formatting to it to make a readable Excel spreadsheet. To do this I had to redefine my spaces. For instance I used that command and put in however many spaces I needed and replaced them with a character that did not exist in the output (i.e., s/{2spaceshere}/;/g) would put a ";" in the place of 2 spaces, I know it will work, it's kind of cumbersome, but it did the trick. There's probably a perl or maybe a troff solution, but I don't do troff, and the box didn't have perl so I went with what I had.
 
try sed 's/[ ]*/ /g' <InputFile >OutputFile

afaik,
shail
 
Just to expand on shail's regexp, between the &quot;[&quot; and &quot;]&quot; you need to enter a space and a tab. For example, using ^I to represent tab:
[tt]
s/[^I ]*/%20/g
[/tt]
Don't forget that the * in a regexp means &quot;zero or more of the preceding character&quot;. As the preceding character is a tab or a space, this replaces any amount of whitespace with a single &quot;%20&quot;. Remove the &quot;*&quot; if you want to replace every tab or space with &quot;%20&quot;.
 
Shail,
&quot;sed 's/[ ]*/ /g' <InputFile >OutputFile&quot; will put a space at the begining of each line.

Try this: -

sed -e 's/ [0-z]/%20 &/g' -e 's/ //g' < input > output


 
Guys,

Lets keep sed programming simple, try this!!!

#cat [inputfile]
#sed 's/ /%20?g' [inputfile] >[outputfile]

Regards,
knightwing
 
Knightwing,
this is virtualy what Borg proposed. Also I think you missed a / before the g

That solution still does not replace each instance of white space with one and only one %20. It also adds a ? after each 20%.
 
Try
sed -e 's/<two spaces here>*/%20/g' <infile >outfile

This replaces 1 space followed by zero-to-many spaces with %20

HTH

cardy [sig][/sig]
 
Spot on Cardy. [sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
sed -e 's/<one space>\+/%20/g' <infile >outfile
Isn't it better solution?
IMHO you'll replace one-or-more spaces with %20's.
 
Did you actually try this because it doesn't work on my system. I'll stick with my original suggestion. [sig][/sig]
 
Cardy's solution is spot on, DGM's does not work on my Solaris instalation. [sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
Putting the &quot;\&quot; before the &quot;+&quot; is escaping the special regex meaning of &quot;+&quot; and turning it into a normal &quot;+&quot; character. So, &quot;<space>\+&quot; is looking for a literal &quot;<space>+&quot; character combination. Take the &quot;\&quot; out, and you've got a very similar solution to the &quot;<two spaces>*&quot; one. [sig]<p> Andy Bold<br><a href=mailto: > </a><br><a href= > </a><br>"I've probably made most of the mistakes already, so hopefully you won't have to..." Me, most days.[/sig]
 
'Fraid not Andy. I thought the same as you so I took the \ out but it still didn't work. I don't think + means 1-or-more of the previous character. I certainly haven't come across it (yet) in the excellent book 'sed and awk' by Dougherty & Robbins from O'Reilly. [sig][/sig]
 
Heck! This is what you get for doing too much perl! *grin* Looks like the &quot;+&quot; operator is a perlism, and I was getting confused. [sig]<p> Andy Bold<br><a href=mailto: > </a><br><a href= > </a><br>"I've probably made most of the mistakes already, so hopefully you won't have to..." Me, most days.[/sig]
 
Did anybody think about the Command &quot;tr&quot; which translates characters in a Stream?
[sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
I had a go with it bt could not stop tr from replacing the new lines with new string, also had problems using %20 as a string. [sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top