Unable to generate Ctrl M at end of rec

Hello, I was wondering if someone can assist me. I have a file that I download from a mainframe to a RedHat Linux 5.5 server, the file is delimited by control underscore characters and the record is terminated by a control M (carriage return). The problem I am having is with the control M character; in my shell script I read the file in and write out a new tweaked file. The new file that I create I am able to create the control underscore but I am not able create the control M on the file.... It just disappears from the file. (Just so people are aware.... I am aware that a control M is created in vi with a Ctrl-V Ctrl-M)
So far I have tried the following:
1) I set a variable to control M
Example:

DLMT1='^M'
echo �${Value1}${Value2)${DLMT1} >> ${filepath}${linuxfile}.tmp

The above writes out the values of Value1 and Value2 but the Control M isn't written out
2) I set a variable to control K
Example:

DLMT1='^K'
echo �${Value1}${Value2)${DLMT1} >> /tmp/tempfile.txt

The above writes out the values of Value1, Value2 and the Control K - That Works
3) I set a variable to control K then tried to replace the control K with control m using tr
Example:

DLMT1='^K'
echo �${Value1}${Value2)${DLMT1} >> /tmp/tempfile.txt
tr '\013-\015' ' ' <${filepath}${linuxfile}.tmp >${filepath}${linuxfile}.tmp2

Didn't work.... Control M disappears....
Now I have tried a bunch of other things but I am still not able to get a control M at the end of the record.... Let me be very clear the issue is getting the control M at the end of the record.... In my testing I am able to get a control M to appear in the middle of the record but that's not what I want. Does anyone have any ideas?

How are you checking that ^M is present? cat may just display it as a newline, since it's a CR.

An alterntive might be to use echo -e "${Value1}${Value2}\r" .

Your post is vague where it matters most.

I suggest that you share your script's code (along with your operating system and shell version), a sample of the input data, a sample of the erroneous output that your script generates, and, finally, the desired output. Since non-graphic characters are crucial here, make sure to post octal/hex dumps of the input/output, so that there is no ambiguity about what we're seeing.

To generate a ^M in a shell script is trivial:

printf '\r'

If that doesn't work, I'm inclined to believe that the problem lies elsewhere.

On an unrelated note, your tr attempt does not convert from one control character to another. It selects a range of them and converts them to spaces, so, naturally, they "disappear".

Regards,
Alister

There are a few problems with the command:

echo �${Value1}${Value2)${DLMT1} >> /tmp/tempfile.txt

that appears in all three of your scripts.

First, it looks like you want a double qouted string, but the closing regular double quote at the end of the string and you have specified the opening double quote character rather than a regular double quote character that would be recognized by the shells to surround a string.

Second, you have ${...) instead of ${...} . You have to match the opening brace character with a closing brace character, not a closing parenthesis character.

Both of these will cause all three of the shell scripts you showed us to produce diagnostics like:

syntax error at line 2: `)' unexpected

or:

 unexpected EOF while looking for matching `}'
unexpected end of file

depending on what shell you're using.

If you change the above line in all three of your scripts to:

echo "${Value1}${Value2}${DLMT1}" >> /tmp/tempfile.txt

you will probably get what you wanted. (Although the in your third script you also need to change the control-K to control-M when you define DLMT1.)