Link to home
Start Free TrialLog in
Avatar of ketchukf
ketchukfFlag for United States of America

asked on

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
Avatar of Tintin
Tintin

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

Avatar of ketchukf

ASKER

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

copies
copies
copies
copies
copies
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

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
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!




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?
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]#

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

DB61448,4
DB66554,2
DB55517,2
DB59397,2
DB61789,1
My test file is attached.
BooksToCopy.csv
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.
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Jackpot. Thank you both for your help.