Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying a special character

Status
Not open for further replies.

pjb

Programmer
May 1, 2001
148
US
We receive files through email, and sometimes they show up with an extra control M (^M) character at the end of each line. Is there a way to identify this value in an if statement? I can use the alpha-numeric check I picked up on this forum yesterday, but is there a way of coding for the exact value of the '^M'?
 
You can try coding it as CTRL-V CTRL-M (the ^V is an escape for the next character)?

Or if you have already tried that, I believe that ^M is ASCII(13) so there should be a way to put that in an if statement.

Good luck - I'll watch this thread for a smarter response. I'm curious too.
Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 

Hi,
It sounds like you have a DOS2UNIX issue here. check to see if you UNI has a dos2unix command which will strip the ^M from the file.

another alternative is just run it through a SED script and do it yourself.

sed -e 's/<ctrl-v><Ctrl-m>//' file > file.new

The <ctrl-v> is the escape character which says the next character I type should not be a SPECIAL character.

the <ctrl-M> would normally be a return character but in this case since it is proceed by the <ctrl-v> it will amount to a ^M.



---
 
Hi:

Looks like you guys have it nailed. I don't care for tdatgod's solution because of the special characters. Good luck cutting and pasting that.

Here's my take in order or importance plus I'm taking possible control-z's into account:

1) The following replacement sed command works by deleting the last character of each line:

sed 's/.$//' $i.tmp > $i

Be careful! The above sed command clobbers the last character in the line, whether it's a control character or not.

2) A better solution is replacing the last character if the octal value is 15 or 32, Control-m and Control-z respectively:

sed 's/'&quot;$(printf '\015')&quot;'$//
s/'&quot;$(printf '\032')&quot;'$//' $i.tmp > $i


3) Perhaps the best solution is executing the tr command to delete all octal 15 and 32 characters. Replace the sed command with the following:

tr -d '\015\032' < $i.tmp > $i

The above command deletes all ^M and ^Z -- not just the ones at the end of the line.

Ed
Schaefer
 
Some versions of unix come with the dos2unix and unix2dos utilities. Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top