syntax for IF test or AWK for octal

Using korn shell. I am reading a file line by line. If a record has a carriage return (octal 015) then I append the second record to the first record. Not all records have a carriage return. I have the unix shell script working with grep, but when my file has +100,000 records it runs slow. I would like to see if the shell script would run faster with an IF Test or AWK. I can't get the syntax right for the IF test. I don't know AWK, if you think this would run faster I would appreciate know how to code it.

1) I was doing some time testing to see where my shell script was slow. This runs fast.

#!/bin/ksh
file_in=file.txt
file_out=file.txt_out
cnt_in=0
while read LINE
do
   cnt_in=$(( cnt_in + 1 ))
   echo $LINE >> $file_out
done < $file_in

2) This runs slow
#!/bin/ksh
dir1=/tdcexports/db2/stage
file_in=file.txt
file_out=file.txt_out
cnt_in=0
while read LINE
do
   cnt_in=$(( cnt_in + 1 ))
   ck_carriage=`echo $LINE | grep -c ^M`
   echo $LINE >> $file_out
done < $file_in

3) I've tried various IF but didn't find the right syntax yet. This doesn't work properly:

if [ $LINE  = '\015' ]
     then echo equal
     else echo no equal
   fi

Thank you for your help.

Just post a sample of your input file and an example of the desired/expected output.

You can test with case:

case $line in 
  *^M) echo "$line ends in a carriage return"
esac

^M is entered as CTRL-V <RETURN>

in ksh93 / bash

case $line in 
  *$'\015') echo "$line ends in a carriage return"
esac

You can probably also use ksh/bash style conditionals:

if [[ "$line" = *^M ]]; then

echo "$line ends in a carriage return"
fi

---------- Post updated at 11:01 ---------- Previous update was at 10:36 ----------

Try this awk to glue together:

awk '{ while (sub(/\r$/,x) && getline p) $0=$0p }1' infile

Please explain this process in minute detail and include before and after sample data which shows where the carriage-return character(s) may be found in a record and whether you need to RETAIN the carriage-return characters in the output or REMOVE them.

Is this one of those cases where carriage-return characters are valid in a string within a database record?