1752781 Members
6135 Online
108789 Solutions
New Discussion юеВ

script need help

 
kholikt
Super Advisor

script need help

Hi,

I am writing some script to construct a new variable called CG_GRP

for CG in `/bin/cat /opt/bin/mylist.txt`
do
echo "$CG"
CG_GRP=$CG
done

But the result will return the item line by line instead of one line.

For Example in mylist.txt I have the following:
Apple
Orange

I want the output to be in single line and separate by just one blank space.

Apple Orange

Instead of

Apple
Orange
abc
5 REPLIES 5
Steven Schweda
Honored Contributor

Re: script need help

> I am writing some script [...]

What kind of (shell) script? "#!/???/??"?

On what?

uname -a

As usual, many things are possible, and "cat"
may not be needed.

dyi # cat mylist.txt
Apple
Orange
dyi # cat mylist.sh
#!/bin/sh

cg_grp=''
while read line ; do
cg_grp="${cg_grp} ${line}"
done
echo ">${cg_grp}<"

dyi # ./mylist.sh < mylist.txt
> Apple Orange<
dyi #
Dennis Handly
Acclaimed Contributor

Re: script need help

You can do this in several ways as Steven said:
# This embeds a newline in the variable:
CG_GRP=$(< /opt/bin/mylist.txt)
echo quoted: "$CG_GRP"
echo not quoted: $CG_GRP

# This uses echo to replace newlines by space:
CG_GRP=$(echo $(< /opt/bin/mylist.txt))
echo quoted: "$CG_GRP"
echo not quoted: $CG_GRP
Jose Mosquera
Honored Contributor

Re: script need help

Hi,

When you do an echo of a variable the behavior could vary depend of you set the variable among double quotes, when this hapen the output will be a list, in your case:
Apple
Orange

Try the variable echo without double quotes.

Rgds.
Rita C Workman
Honored Contributor

Re: script need help

How about:

for CG in `/bin/cat /opt/bin/mylist.txt`
do
echo $CG "\c"
CG_GRP=$CG
done

I think you could put quotes around $CG and it would come out the same.

Just a thought,
Rita

James R. Ferguson
Acclaimed Contributor

Re: script need help

Hi:

TMTOWTDI :

# cat ./showme
#!/usr/bin/sh
while read LINE
do
echo "$LINE \c"
done < /opt/bin/mylist.txt
echo
exit 0

...Note that we let the shell perform the read and eliminate the use of the extra process (and I/O) when the 'cat' was used.

Regards!

...JRF...