## How to convert "A+B+C" into "A B C" in C shell (sh)?

### How to convert "A+B+C" into "A B C" in C shell (sh)?

My script is passed a variable (let's call it VAR) which contains the
following:

"A+B+C+D+E"

I'd like to be able to do a for in loop on "A B C D E".  Is there an
easy way to convert:

VAR="A+B+C+D+E" into

VAR="A B C D E"

from within the script?  Is there a way to do it in one line?

/--------------------------------------------------------------------\
|   A man said to the Universe: "Sir, I exist!"                      |
|   "However," replied the Universe,                                 |
|   "the fact has not created in me a sense of obligation."          |
|                                -- Stephen Crane                    |
\--------------------------------------------------------------------/

### How to convert "A+B+C" into "A B C" in C shell (sh)?

! I'd like to be able to do a for in loop on "A B C D E".  Is there an
! easy way to convert:
!
! VAR="A+B+C+D+E" into
!
! VAR="A B C D E"
!
! from within the script?  Is there a way to do it in one line?

Forget csh.  Here how to do it in sh:

_ifs=\$IFS
IFS=+\$IFS

eval VAR=\'\$VAR\'       # Now \$VAR == "A B C D E"

IFS=\$_ifs

or, if you're only interested in splitting for the sake of the for loop:

_ifs=\$IFS
IFS=+\$IFS

for i in \$VAR
do
# \$i loops through \$VAR elements
done

IFS=\$_ifs

~~h

### How to convert "A+B+C" into "A B C" in C shell (sh)?

> My script is passed a variable (let's call it VAR) which contains the
> following:
>    "A+B+C+D+E"
> I'd like to be able to do a for in loop on "A B C D E".  Is there an
> easy way to convert:
> VAR="A+B+C+D+E" into
> VAR="A B C D E"
> from within the script?  Is there a way to do it in one line?

The following will work with csh.  Change the setenv part for other
shells.

setenv VAR `echo \$VAR | tr "+" " "`

---

### How to convert "A+B+C" into "A B C" in C shell (sh)?

One quick way is using sed.

set aa='A+B+C+D+E'
set bb="`echo \$aa | sed 's/+/ /g'`"
echo \$aa \$bb

I test it and it works properly.

o
~~~~  /-/-/   ~~~~   >>--->  LIFE GOES ON
_/\
1
-----------------

-----------------

### How to convert "A+B+C" into "A B C" in C shell (sh)?

|> My script is passed a variable (let's call it VAR) which contains the
|> following:
|>
|>    "A+B+C+D+E"
|>
|> I'd like to be able to do a for in loop on "A B C D E".  Is there an
|> easy way to convert:
|>
|> VAR="A+B+C+D+E" into
|>
|> VAR="A B C D E"
|>
|> from within the script?  Is there a way to do it in one line?
|>
|>
|> /--------------------------------------------------------------------\
|> |   A man said to the Universe: "Sir, I exist!"                      |
|> |   "However," replied the Universe,                                 |
|> |   "the fact has not created in me a sense of obligation."          |
|> |                                -- Stephen Crane                    |
|> \--------------------------------------------------------------------/
|>

To replace +'s in VAR with spaces in C sh, use:

set VAR=`echo \$VAR | sed 's/+/ /g'`

--
Praveen Puri
Common Install Group
Motorola
"Have sh, will travel"

### How to convert "A+B+C" into "A B C" in C shell (sh)?

: My script is passed a variable (let's call it VAR) which contains the
: following:

:    "A+B+C+D+E"

: I'd like to be able to do a for in loop on "A B C D E".  Is there an
: easy way to convert:

: VAR="A+B+C+D+E" into

: VAR="A B C D E"

: from within the script?  Is there a way to do it in one line?

Here's a ksh way to do it.  It requires more than one line, but it is faster
than using tr or sed.  Change print to echo if using /bin/sh.

X='A+B+C+D+E'
OLDIFS=\$IFS
IFS=+
for Y in \$X
do
print \$Y
done
IFS=\$OLDIFS

Beirne

: /--------------------------------------------------------------------\
: |   A man said to the Universe: "Sir, I exist!"                      |
: |   "However," replied the Universe,                                 |
: |   "the fact has not created in me a sense of obligation."          |
: |                                -- Stephen Crane                    |
: \--------------------------------------------------------------------/
:

--
Beirne Konarski         | Subscribe to the Unicycling Mailing List

Kent State University   | Unicycling Web Page:
"Untouched by Scandal"        | http://nimitz.mcs.kent.edu/~bkonarsk/

### How to convert "A+B+C" into "A B C" in C shell (sh)?

>I'd like to be able to do a for in loop on "A B C D E".  Is there an
>easy way to convert:

>VAR="A+B+C+D+E" into
>VAR="A B C D E"

>from within the script?  Is there a way to do it in one line?

set VAR=`echo \$VAR | tr '+' ' '`

--

### How to convert "A+B+C" into "A B C" in C shell (sh)?

: I'd like to be able to do a for in loop on "A B C D E".  Is there an
: easy way to convert:

: VAR="A+B+C+D+E" into

: VAR="A B C D E"

Use:

set VAR=`echo \$VAR | sed 's/\+/ /g'`

--

Tel: RES (415) 964 0470
OFF (415) 926 1103

### How to convert "A+B+C" into "A B C" in C shell (sh)?

>: I'd like to be able to do a for in loop on "A B C D E".  Is there an
>: easy way to convert:
>: VAR="A+B+C+D+E" into
>: VAR="A B C D E"
>Use:
>  set VAR=`echo \$VAR | sed 's/\+/ /g'`

Or even:

set VAR=`echo \$var | tr '+' ' '`

Cheers
Tony
______                    , _
(_) |                     /|/ \
| __   _  _            |   |          __,  _   _  _  _|_
_ |/  \_/ |/ |  |   |    |   |  |   |  /  | |/  / |/ |  |
(_/ \__/   |  |_/ \_/|/   |   |_/ \_/|_/\_/|/|__/  |  |_/|_/
/|                    /|

'                     '

We are searching for professionals with 3 to 5 years experience with
UNIX or VAX "C" programming.

Our clients are NY and NJ based International  Financial Firms.

If you have what it takes, we will make the introduction for you for
the right fit.

Let us know your knowledge base so we can present the position that