Visit Our UNIX and Linux User Community


Unable to generate Ctrl M at end of rec


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to generate Ctrl M at end of rec
# 1  
Old 10-16-2013
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:
Code:
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:
Code:
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:
Code:
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?
# 2  
Old 10-16-2013
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".
# 3  
Old 10-16-2013
Quote:
Originally Posted by app1mvf
Does anyone have any ideas?
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:
Code:
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
# 4  
Old 10-16-2013
Quote:
Originally Posted by app1mvf
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:
Code:
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:
Code:
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:
Code:
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?
There are a few problems with the command:
Code:
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:
Code:
syntax error at line 2: `)' unexpected

or:
Code:
 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:
Code:
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.)

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation of keyboard inputs..like Ctrl+d and Ctrl+a

Hi..! I'm stuck with my automation of starting a process and keeping it running even after the current ssh session has exited.. So i'm trying to use command 'screen'. which is doing exactly what i wanted, But the problem is automation of the same. i will have to press Ctrl+a and Ctrl+d for... (2 Replies)
Discussion started by: chandana hs
2 Replies

2. UNIX for Dummies Questions & Answers

Ctrl-V + Ctrl-J for newline character does not work inside vi editor

Hi friends, I am trying to add a newline char ('\n') between the query and the commit statement in the following shell script. #! /bin/sh echo "select * from tab; commit;" > data.sql I have tried typing in "Ctrl-V + Ctrl-J" combination which has inserted ^@ (NUL) character but the commit... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. Shell Programming and Scripting

Unable to set end of line in mail command

Hi , Am trying to send mail using the mail command, but the mail command is working but its not sending automatically after pressing .(dot) in the command prompt it sends . How to achieve that. Also it showing the below line after pressing the dot . /home/abc1/dead.letter... Saved message in... (5 Replies)
Discussion started by: rogerben
5 Replies

4. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

5. Shell Programming and Scripting

Generate Codes based on start and End values of numbers in a column

Hello All, Could you please help with this. This is what I have: 506234.222 2 506234.222 2 506234.222 2 506234.222 2 508212.200 2 508212.200 2 333456.111 2 333456.111 2 333456.111 2 333456.111 2 But this is what I want: 506234.222 1 506234.222 2 506234.222 2 506234.222 3 (5 Replies)
Discussion started by: canimba
5 Replies

6. Shell Programming and Scripting

Ctrl-C or Ctrl-Z causing exit the session

H! I have written script where it need to invoke the perl script in background, then write the pid in temp file then bring back the job to foreground. whenever the Ctrl-C or Ctrl-Z is pressed in the script has to exit and prompt should be dispalyed. but this script causing exit from shell session... (2 Replies)
Discussion started by: jramesh1
2 Replies

7. Shell Programming and Scripting

Generate quarter dates with begin date and end date

Hi All, I am trying to generate quarter dates with user giving input as begin date and end date. Example: Input by user: begin_date = "2009-01-01" end_date = 2010-04-30" required output: 2009-01-01 2009-03-31 09Q01 2009-04-01 2009-06-30 09Q02 . . till 2010-01-01 2010-03-31 10Q01 ... (9 Replies)
Discussion started by: sol_nov
9 Replies

8. Programming

unable to get end of file while reading HTTP data from socket

I am trying to read HTTP data from a socket. However, for the final set of data being read using read(), read blocks and the control doesnt come back for further processing. I tried using select, but it didn't work... Any help would be greatly acknowledged.:) (2 Replies)
Discussion started by: Harish.joshi
2 Replies

9. Shell Programming and Scripting

Ctrl-M at end of each line

I have a write a script in VI editor.whenever i open the Vi script ,it is showing ctrl-M at the end of each line. Please tell me how to remove this Ctrl-M from each line. Thanks in Advance (3 Replies)
Discussion started by: navi
3 Replies

10. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies

Featured Tech Videos