Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacing spaces characters with % character

4 views
Skip to first unread message

contracer

unread,
Feb 8, 2012, 7:54:29 AM2/8/12
to
Hi,
How could I replace all spaces in a file with % character, usind "for"
loop ?
Thanks

pk

unread,
Feb 8, 2012, 8:04:27 AM2/8/12
to
On Wed, 8 Feb 2012 04:54:29 -0800 (PST), contracer <contr...@gmail.com>
wrote:

> Hi,
> How could I replace all spaces in a file with % character, usind "for"
> loop ?

Obviously not, the for loop itself isn't used to replace characters. Why do
you need to use a for loop?

contracer

unread,
Feb 8, 2012, 8:45:06 AM2/8/12
to
On 8 fev, 11:04, pk <p...@pk.invalid> wrote:
> On Wed, 8 Feb 2012 04:54:29 -0800 (PST), contracer <contrace...@gmail.com>
> wrote:
>
> > Hi,
> > How could I replace all spaces in a file with % character, usind "for"
> > loop ?
>
> Obviously not, the for loop itself isn't used to replace characters. Why do
> you need to use a for loop?

Which Unix command could I use to verify character by character in a
file ?

Janis Papanagnou

unread,
Feb 8, 2012, 9:00:58 AM2/8/12
to
tr(1)

Janis

Aragorn

unread,
Feb 8, 2012, 9:01:20 AM2/8/12
to
On Wednesday 08 February 2012 14:45, contracer conveyed the following to
comp.unix.shell...

> On 8 fev, 11:04, pk <p...@pk.invalid> wrote:
>
>> On Wed, 8 Feb 2012 04:54:29 -0800 (PST), contracer wrote:
>>
>> > How could I replace all spaces in a file with % character, usind
>> > "for" loop ?
>>
>> Obviously not, the for loop itself isn't used to replace characters.
>> Why do you need to use a for loop?
>
> Which Unix command could I use to verify character by character in a
> file ?

man sed


--
= Aragorn =
(registered GNU/Linux user #223157)

Ed Morton

unread,
Feb 8, 2012, 9:08:03 AM2/8/12
to
It's not clear if you're talking about replacing spaces in a file name or in a
files contents, if you're issue is how to do the replacement in 1 file or if you
know how to do it for 1 file but can't figure out how to do it for many files.

You could really stand to post some sample input and expected output so we're
not all guessing.

Ed.

Janis Papanagnou

unread,
Feb 8, 2012, 9:15:34 AM2/8/12
to
Am 08.02.2012 15:08, schrieb Ed Morton:
> On 2/8/2012 7:45 AM, contracer wrote:
>> [...]
> [...]>
> You could really stand to post some sample input and expected output so
> we're not all guessing.

As long as we're able to post just 5 or 6 character as response the
wasted time is not that that significant. ;-)

Janis

>
> Ed.

bsh

unread,
Feb 14, 2012, 6:48:20 PM2/14/12
to
On Feb 8, 4:54 am, contracer <contrace...@gmail.com> wrote:
> How could I replace all spaces in a file with % character, usind "for" loop ?

Why does it necessarily have to use the "for" loop; isn't the looping
flow-of-control code up to you and your particular implementation?
(Actually, the "while" or "until" loop should be more pertainent....)

Are you talking about percent encoding? (Probably not). This
code fragment should show more light on the technique:

From the Kornshell FAQ at http://kornshell.com/doc/faq.html:

Q25. How can I convert %XX values to ascii?
A25. You can convert this to a sequence of ANSI C strings and
then eval that string, for example suppose the variable 'foo' contains
%XX strings, then:

eval print -r -- "\$'${foo//'%'@(??)/'\x\1"'\$'"}'"

... will print out the string in ascii.

The above uses ksh version 1993 or newer features. If you want
a CGI / URL encoding/unencoding function in shell, there are many
to be found, including one from the author of kornshell:

http://www2.research.att.com/~gsf/download/ksh/scripts/cgi-lib.ksh
http://www2.research.att.com/~gsf/download/ksh/scripts/dump-cgi.ksh

... and others:

ftp://ftp.armory.com/pub/lib/ksh/cgi
http://cgd.sdf-eu.org/a/libs/sh/cgi.sh
http://www.mtxia.com/fancyIndex/Tools/Scripts/Korn/Functions/cgiparse.{html,shtml,txt}

=Brian
0 new messages