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)?

Post by Alan Wein » Thu, 22 Jun 1995 04:00:00



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                    |
\--------------------------------------------------------------------/    
 Visit my World Wide Web home page at:                              
       <A HREF="http://www.clark.net/pub/alweiner/cgi-bin/homepage.html">Alan's Home Page</A>


 
 
 

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

Post by Harald Eikr » Thu, 22 Jun 1995 04:00:00



! 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)?

Post by Frank San » Thu, 22 Jun 1995 04:00:00




> 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)?

Post by Peter Li » Thu, 22 Jun 1995 04:00:00


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)?

Post by Praveen C. Pu » Thu, 22 Jun 1995 04:00:00


|> 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                    |
|> \--------------------------------------------------------------------/    
|>  Visit my World Wide Web home page at:                              
|>        <A HREF="http://www.clark.net/pub/alweiner/cgi-bin/homepage.html">Alan's Home Page</A>
|>                                                                    

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)?

Post by Beirne Konars » Fri, 23 Jun 1995 04:00:00


: 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                    |
: \--------------------------------------------------------------------/    
:  Visit my World Wide Web home page at:                              
:        <A HREF="http://www.clark.net/pub/alweiner/cgi-bin/homepage.html">Alan's Home Page</A>
:                                                                    

--
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)?

Post by Mark Ra » Fri, 23 Jun 1995 04:00:00



>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?

how about
  set VAR=`echo $VAR | tr '+' ' '`

--

Decadence is its own reward.

 
 
 

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

Post by Prasad Deshpan » Sat, 24 Jun 1995 04:00:00


: 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'`

--
Prasad Deshpande

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

 
 
 

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

Post by Tony Nugen » Sun, 25 Jun 1995 04:00:00




>: 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
 ______                    , _                                  
 (_) |                     /|/ \                                
     | __   _  _            |   |          __,  _   _  _  _|_  
   _ |/  \_/ |/ |  |   |    |   |  |   |  /  | |/  / |/ |  |    
  (_/ \__/   |  |_/ \_/|/   |   |_/ \_/|_/\_/|/|__/  |  |_/|_/  
                      /|                    /|                  

                       '                     '                  

 
 
 

1. ***NYC***UNIX "C" and VAX "C" Programer/Analysts $75K-85K

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
fits your interests and needs.

Contact: Andre K. Papp


E. And T. Associates
Phone: (212) 780-9194
Fax:     (212) 979-6180

2. Need help with Broadcom homepna phoneline drivers

3. restricted shell

4. ***NYC***UNIX "C" and VAX "C" Programer/Analysts

5. Q:DiskCopy for Linux?

6. ***NYC***UNIX "C" and VAX "C" Programer/Analysts $75K-85K

7. RS6000 Gear to clear

8. Trap "^C" "^Z" and "^\"

9. newbie: "myexpect.c" or "use fscanf?"

10. What is a good way to learn GNU "C" and "C++" ?

11. mystery: "-c" option on "su -" does not work as advertised?

12. Catching "kill -9" and "^C"