ketchukf
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/stuf f.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
#!/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/stuf
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
ASKER
Tintin, that gives me the same results. Five lines consisting of the word 'copies':
copies
copies
copies
copies
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
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
ASKER
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
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!
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?
Are you copying/pasting the *exact* solutions or re-typing them?
ASKER
Here's an exact copy-and-paste from vi on my CentOS 5.4 VM:
#!/bin/bash
INPUTFILE="/home/keith/Des ktop/Books ToCopy.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/sample 2.sh
copies
copies
copies
copies
copies
[root@localhost tmp]#
#!/bin/bash
INPUTFILE="/home/keith/Des
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/sample
copies
copies
copies
copies
copies
[root@localhost tmp]#
ASKER
Here's a copy-and-paste of the input file:
DB61448,4
DB66554,2
DB55517,2
DB59397,2
DB61789,1
DB61448,4
DB66554,2
DB55517,2
DB59397,2
DB61789,1
ASKER
My test file is attached.
BooksToCopy.csv
BooksToCopy.csv
ASKER
The result of this:
#!/bin/bash
INPUTFILE="/home/keith/Des ktop/Books ToCopy.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/sample 3.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.
#!/bin/bash
INPUTFILE="/home/keith/Des
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/sample
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Jackpot. Thank you both for your help.
Open in new window