Reading control characters into variables

Hi,

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.

Hi,

You can use the following line of code;

cat -v -t afile.txt | paste -s | sed 's/  //g'

Note to get the character that looks like a space between the two slashes, you need to use "ctrl+v ctrl+i"

Regards

Gull04

I maybe missing something, but it returns nothing.

Hi,

What is your OS?

Check the following;

Can you see the original file if you just run the,

cat -v -t afile.txt

Let me know the above.

Regards

Gull04

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:

while read line
do
...
done < afile.txt
cat text.txt | awk '{ORS=""}1'