How can I concatenate a string with a number in bash?

When I run this script:

#!/bin/bash

declare A="make"
declare B="copies"
declare C
C=$A"5"$B
printf "%s\n" "$C"

I get this result:

make5copies

That's the output I want from this:

#!/bin/bash

declare INPUTFILE="/home/test/stuff.csv"
declare TMP
declare A="make"
declare B="copies"
declare C

cut -d, -f2 $INPUTFILE | {
        while read TMP ; do
                C=$A"${TMP}"$B
                printf "%s\n" "$C"
        done
}

No matter how I format the line: C=$A"${TMP}"$B the output is either an error or just 'copies'. The problem seems to be that I'm concatenating a string, a number, and another string. What's the trick?

Here's the input file:

DB61448,4
DB66554,2
DB55517,2
DB59397,2
DB61789,1

Thanks for your help.

Keith
ketchukfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
I just do it like this
#!/bin/bash
INPUTFILE=/home/test/stuff.csv
A=make
B=copies

for i in $(cut -f2 -d, $INPUTFILE)
do
  echo "$A$i$B"
done

Open in new window

0
ketchukfAuthor Commented:
Tintin, that gives me the same results. Five lines consisting of the word 'copies':

copies
copies
copies
copies
copies
0
woolmilkporcCommented:
Hi,

your script should work as posted, and Tintin's version should work fine, too.

Anyway, you can try this:

awk -F, '{printf "make%scopies\n", $2}' /home/test/stuff.csv

wmp

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ketchukfAuthor Commented:
Thanks for the reply, both of you.

wmp, same results with awk. Five lines of 'copies'. This is my first attempt at a shell script, so I thought the problem would be something simple. Evidently I was wrong.

Why would all three methods return the same unexpected result?

Keith
0
woolmilkporcCommented:
The only thing I could imagine is that your inputfile doesn't look as posted.

And even then you should at least see "makecopies" and not "copies" alone!




0
TintinCommented:
If you used either my solution of woolmilkporc's solution it is impossible to get the output you say unless there is something very, very strange with your system or input file.

Are you copying/pasting the *exact* solutions or re-typing them?
0
ketchukfAuthor Commented:
Here's an exact copy-and-paste from vi on my CentOS 5.4 VM:

#!/bin/bash

INPUTFILE="/home/keith/Desktop/BooksToCopy.csv"
A=make
B=copies

for i in $(cut -f2 -d, $INPUTFILE)
do
  echo "$A$i$B"
done

Here's a copy-and-paste of the output:

[root@localhost tmp]# /home/keith/Desktop/sample2.sh
copies
copies
copies
copies
copies
[root@localhost tmp]#

0
ketchukfAuthor Commented:
Here's a copy-and-paste of the input file:

DB61448,4
DB66554,2
DB55517,2
DB59397,2
DB61789,1
0
ketchukfAuthor Commented:
My test file is attached.
BooksToCopy.csv
0
ketchukfAuthor Commented:
The result of this:

#!/bin/bash

INPUTFILE="/home/keith/Desktop/BooksToCopy.csv"
A=make
B=copies

for i in $(cut -f2 -d, $INPUTFILE)
do
  echo $A
  echo $i
  echo $B
done

Is this:
[root@localhost tmp]# /home/keith/Desktop/sample3.sh
make
4
copies
make
2
copies
make
2
copies
make
2
copies
make
1
copies

The problem seems to be concatenating a number and a string. The CentOS VM is a fresh install. I don't know what would be strange about it.
0
TintinCommented:
Try doing

dos2unix BooksToCopy.csv

and running the scripts again.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TintinCommented:
I've just verified that the issue is caused by your csv file being in Windows format.  Once you run the dos2unix, it will be fine.
0
ketchukfAuthor Commented:
Jackpot. Thank you both for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.