I am trying to write a shell script to help with some digital signature work currently being undertaken where we have a file that contains a number of rows ending with ^M.
What I need to do is concatenate this using shell scripting and retain the control character. E.G.
abc^M
def^M
ghi^M
becomes
abc^Mdef^Mghi^M
I have a file called afile.txt
with contents
abc^M
def^M
ghi^M
And a script that looks like
for line in `cat afile.txt`
do
line2=${line2}${line}
done
echo $line2
This returns:
ghi
So where am I going wrong? Any help gratefully appreciated.
Thanks.
P.S.
If I remove the ^M from each line it works, but I need to keep ^M in the string.
You do not go wrong anywhere, but if $line2 contains abc^Mdef^Mghi^M then if you print the variable on your terminal the only thing visible is ghi , because of the CR characters, which move the cursor to the beginning of the line..
Try using set -x or redirecting the variable to a file and you'll see they are there...
--
It is better not to use for line in `cat afile.txt` . Consider using: