Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to concatenate strings into one? 1

Status
Not open for further replies.

volcano

Programmer
Aug 29, 2000
136
HK
Hello, I have a question here which may be very simple for all of you (but I do not know...)

Suppose there are 2 variables named $ABC and $DEF whose values are "hello, " and "john" respectively. How can I concatenate these 2 variables into one so that I can output "hello, john"?

Thanks!
 
print "$ABC \c$DEF"

or

NEWLINE="$ABC $DEF"
print "${NEWLINE}"
 
Thank you for your help!

I am writing a Bourne shell script. Is PRINT command not allowed in it?

Let me specify my case: I have a text file containing many email addresses. In the file each line contains only one email address. Now I have to read this text file and concatenate all of these email addresses into a string so that I can have a list of email addresses separated by commas. It looks like: abc@abc.com, def@def,com, 123@123.com

So how could I do this?

Thanks again!
 
For sh, print is not a builtin, so you either have to call it from /usr/bin or use echo.

For your added question do this:

for EMAIL in `cat YOUR_FILE_NAME_HERE`
do
echo "${EMAIL}, \c" >> NEW_FILE
done
 
Thanks pmcmicha. Your script works well. Now if I want to concatenate all email addresses into a variable, but not a physical file (in your code, NEW_FILE), what should I edit in the code? If I replace NEW_FILE with $LIST_OF_ADDR, I can only get an empty variable when I access $LIST_OF_ADDR next time!!
 
This would do it, but you need the Korn shell for this one to work. There's a subtle difference between Korn shell and Bourne shell at work here...
[tt]
#!/bin/ksh

ADDRFILE=youraddressfile.txt
SEPARATOR=""
ADDRLIST=""

while read ADDRESS
do
ADDRLIST="${ADDRLIST}${SEPARATOR}${ADDRESS}"
SEPARATOR=","
done < ${ADDRFILE}

print &quot;Address list: ${ADDRLIST}&quot;
[/tt]
This ends up with the comma separated list of addresses in the variable ADDRLIST.

Hope this helps.

 
Here is a bsh solution with no looping required!
[tt]
LIST=`cat file1`
LIST=`echo $LIST|tr ' ' ','`
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top