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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TintinConnect With a Mentor Commented:
Try doing

dos2unix BooksToCopy.csv

and running the scripts again.
0
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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:
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
All Courses

From novice to tech pro — start learning today.