How to find the ^M(control M) character in unix file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find the ^M(control M) character in unix file?
# 1  
Old 12-04-2008
How to find the ^M(control M) character in unix file?

can any one say about command to find "^M" (Control M)characters in a unix text file.

^M comes when a file ftped from windows to unix without using bin mode.

I need the command to find lik this,

ex.txt:
------------------------------
...,name,time^M
go^M
...file,end^M
------------------------------

i want to grep and find it. i want the output as,
- "^M" characters present in the file (if its there).
if not
- "No ^M characters present in file.
# 2  
Old 12-04-2008
Why not use the '/usr/bin/dos2unix' utility?

-OR-

Code:
tr '\n' ' '

HTH
# 3  
Old 12-04-2008
i should not remove the ^M characters by putting dos2unix command.
Actaully i want to find whether ^M character is present in the file or not by NOT opening the file.
i want some command like grep, i.e i want to grep and find for ^M characters int he file by not opening that text file.

any one say the unix (grep or cut or any)command?
# 4  
Old 12-04-2008
^M is a carriage return.

You should understand a few things first:

CR = \r = Carriage Return
LF = \n = Line Feed

In DOS, all lines end with a CR/LF combination or \r\n.
In UNIX, all lines end with a single LF or \n.

The ^M that you are seeing is actually a CR or \r. If you want to test for carraige returns in a file, you want to look for \r. Try this on the file:

Code:
od -c filename.txt

You'll see tabs, vertical tabs, carriage returns, linefeeds and whatnot using the slash notation. I find this to be the best method for determining what actual characters are in a file.

Or, if you just want to see the ^M notation, you can use cat, like so:

Code:
cat -v filename.txt

If you want to remove the ^M characters, you can use dos2unix as suggested above, or the correct tr syntax:

Code:
tr -d '\r' < infile.txt > outfile.txt

These 4 Users Gave Thanks to stanleypane For This Post:
# 5  
Old 12-04-2008
To find whether a file has a CR or not you can use grep, this should print the lines with a CR:

Code:
grep '^M' file1

Type <Ctr-v><Ctr-m> to get ^M and not a ^ and an M.

Regards
These 2 Users Gave Thanks to Franklin52 For This Post:
# 6  
Old 12-04-2008
Thnx Frankln and stanley for ur reply...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script for creating Control file in UNIX

Delete ---- Original post, restored by mod after being deleted by abhilashnair ---- I have a requirement where, I need to create a control file which will have 3 columns in the header row as below: Filename Count Checksum This above control file has to contain metadata as above... (2 Replies)
Discussion started by: abhilashnair
2 Replies

2. Shell Programming and Scripting

Formatting file data to another file (control character related)

I have to write a program to read data from files and then format into another file. However, I face a strange problem related to control character that I can't understand and solve. The source file is compose of many lines with such format: T_NAME|P_NAME|P_CODE|DOCUMENT_PATH|REG_DATE ... (3 Replies)
Discussion started by: hk6279
3 Replies

3. Shell Programming and Scripting

Character Find and replace in Unix or Perl

Hi, I am stuck with an problem and want some help, what i want to do is There is a directory name temp which include file named t1.txt, t2,txt, t3.txt and so on. These files contains data, but there are some bad character also that is % present in the files , I want to write the script... (13 Replies)
Discussion started by: parthmittal2007
13 Replies

4. Shell Programming and Scripting

control M character in unix file

in a file we are getting control character in a file , is there any way that they can be removed once we have the file for eg. BEGIN-PROCEDURE INITIALIZE ^M LET #row_count = 0^M ^M ^M (2 Replies)
Discussion started by: lalitpct
2 Replies

5. Shell Programming and Scripting

perl cmd to remove the control-Z character at end of 10GB file

In a 10-50GB file , at end of file there is Control-z character tried the below options, 1. perl -p -i -e 's/^Z//g' new.txt 2. perl -0777lwi -032e0 new.txt and Sed command, dos2unix etc it takes more time to remove the control-z. need a command or perl program to GO TO LAST LINE OF FILE ... (7 Replies)
Discussion started by: prsam
7 Replies

6. Programming

Problem with control file and special character

I am getting error when loading data file using ctl file. I get this error only when there is special character. Below is some data. DataFile=> company_id|ciu_id|english_name|iso_country_code|active|partner_name 1-2JT-122||Expert Järvenpää|FI|A|Expert Järvenpää Control File=> LOAD DATA... (1 Reply)
Discussion started by: rshivarkar
1 Replies

7. Shell Programming and Scripting

display all possible control characters from .xml file in unix

Hi, I have a .xml file in unix. We are passing this file through a xml parser. But we are getting some control characters from input file and XML parser is failing for the control character in file.Now I am getting following error, Error at byte 243206625 of file filename_$.xml: Error... (1 Reply)
Discussion started by: fantushmayu
1 Replies

8. Shell Programming and Scripting

Hidden control characters in a Unix Text File!

Can anyone seem to know how to find out whether a UNIX text file has 'hidden' control characters? Can I view them using 'vi' by some command line options? If there are control characters in a text file which are invisible/hidden.. then how do I get rid of them? Your intelletual answers are... (6 Replies)
Discussion started by: kewl_guy
6 Replies

9. UNIX for Dummies Questions & Answers

Control character in a file

Hi All, I am looking for a solution to capture any ASCII control character in a file ( where the ASCII control character is in decimal value from 0 to 31 and 127 ( Hex value from 00 to 1F and 7F ) ) by returning any affected lines. The intended good file should contain "ASCII printable... (5 Replies)
Discussion started by: cursive
5 Replies

10. UNIX for Dummies Questions & Answers

create control file in UNIX

UNIX gurus: Following is what I am trying to do: I need to create a control file for another file that I am creating. The information needed in the control file is the date in YYYYMMDD format and then the number of records in the other file right justified and lpadded with spaces of 20. So... (5 Replies)
Discussion started by: alfredo123
5 Replies
Login or Register to Ask a Question