1754379 Members
4750 Online
108813 Solutions
New Discussion юеВ

output of a variable

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

output of a variable

Hi all,

I am having difficutly reading a variable in a while / for loop. I'm not sure whether it's because I have forgot or something else:

COLORS=#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099"

for data in `echo $COLORS`^Jdo^Jecho $COLORS^Jdone
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099
#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099


I dont want this - I want each $VAR to be printed on a seperate line.

Am i being dumb?

thanks

Chris.
hello
5 REPLIES 5
Pete Randall
Outstanding Contributor
Solution

Re: output of a variable

for data in `echo $COLORS`^Jdo^Jecho $COLORS^Jdone

should be

for data in `echo $COLORS`^Jdo^Jecho $data^Jdone


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: output of a variable

Hi CHris:

COLORS="#CC9999 #9999FF #FFCC88 #FF99FF #FFFF66 #FF9933 #CC9966 #990099"

for VAR in ${COLORS}
do
echo ${VAR}
done

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: output of a variable

you know when you look at something for so long your mind turns to jelly and all you get is "wibble and wobble"

Thats how I feel - a dumb question which I should have seen but thanks guys I think I will go home for the day!!

Chris.
hello
Hein van den Heuvel
Honored Contributor

Re: output of a variable

[I know the topic just closed]
Before you go home, please consider what you are going to do with the individual words once you have them.
I find that all too often folks then fo into awk or perl or further process.
In that case, use such tool to do both the splitting and the next processing step(s).

For sake of completeness here are two frames:

awk:

# echo $COLORS | awk '{while (i
Perl:

# $ echo $COLORS | perl -lne 'print foreach (split)'

# $ export COLORS
$ perl -le 'foreach (split /\s+/, $ENV{COLORS}) { print }'

# echo $COLORS | perl -pe '$_ = join "\n",split'

adding that last newline....

echo $COLORS | perl -pe '$_ = (join "\n",split) . "\n"'


hein.




lawrenzo_1
Super Advisor

Re: output of a variable

thank you
hello