1826103 Members
4398 Online
109690 Solutions
New Discussion

Re: control M characters

 
SOLVED
Go to solution
Rinky
Advisor

control M characters

Hi,
I wanna check if a file contains control M [^M]
characters in it.
Not a perl script, but a shell script solution.
Can I check for it
20 REPLIES 20
Dennis Handly
Acclaimed Contributor

Re: control M characters

If you want to just convert them, you can use dos2ux(1).

Otherwise you can use grep but you'll have problems getting a control M into the shell.

I was able to use control V, then control M in vi to get:
$ fgrep -q ^M file
Yogeeraj_1
Honored Contributor

Re: control M characters

hi

try:
find . -type f |xargs grep -l ^M

Where the ^M character should be typed as:
ctrl+v ctrl+m

hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Rinky
Advisor

Re: control M characters

fgrep -q ^M file
dint work..
I have attached the file.
You can test this file
Dennis Handly
Acclaimed Contributor

Re: control M characters

>fgrep -q ^M file
dint work.

Works fine for me using a real shell. As I said, you have to create a script with control M. You can't enter it directly to the shell.
Dennis Handly
Acclaimed Contributor

Re: control M characters

Here is my script.
James R. Ferguson
Acclaimed Contributor

Re: control M characters

Hi Rinky:

What's (?) so hard with:

# perl -ne 'print if /\015/' file

THe \015 is the octal code for a Carriage-Return which is graphically seens as ^M. The Perl snippet reads every line of a 'file' passed printing any line that has a carriage-return in it.

Regards!

...JRF...
OldSchool
Honored Contributor

Re: control M characters

...or you can stick the ^M in a "pattern file" and use it. for example, file "P1" contains the Ctrl-M, then you can do:

grep -f P1

or for all Control characters, try

grep [:ctrl:]

Sandman!
Honored Contributor

Re: control M characters

Simplest would be to grep for the Ctrl-M character which is inserted into the cmd line by holding down the "Ctrl", "V" and "M" keys simultaneously on your keyboard i.e.

# grep -q ^M infile && echo "Ctrl M chars found" || echo "No Ctrl M chars found"
OldSchool
Honored Contributor

Re: control M characters

FYI:

Sandman, your solution occurred to me also, but when i tried it in ksh on 11.0, entering that key sequence generated a Ctrl-J on the command line. Suspect its an issue w/ "set -o vi" / .exrc / .kshrc, but didn't check further...just proceeded on in a different direction
Sandman!
Honored Contributor

Re: control M characters

You're right OldSchool I am unable to duplicate the command line I created under the sh-posix(1) shell. Hmmm...needs to be looked into.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: control M characters

Here's a sneaky way to do it leveraging echo.

If testing a shell variable then:

echo "${MYVAR}" | grep -q $(echo "\r\c")
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "CR Found"
fi

If testing a file:

grep -q $(echo "\r\c") infile
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "CR Found"
fi


If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: control M characters

>Sandman: I am unable to duplicate the command line I created

I kept saying that. But vi allows ^M.
Arturo Galbiati
Esteemed Contributor

Re: control M characters

Hi,
grep [\015] file

it's ok on my Hp-UX 11i

HTH,
Art
Dennis Handly
Acclaimed Contributor

Re: control M characters

>Art: grep [\015] file

I almost took you at your word since I didn't check this. (I forgot what Ronnie said. ;-)
But now I did and this does not work in a real shell. What shell did you use?

A similar (but quoted) syntax works in tr(1): "[\015]"
freddy_21
Respected Contributor

Re: control M characters

try to run
#dos2ux test1 > test2

your ^M will be remove at file test2.


thanks
freddy
Rinky
Advisor

Re: control M characters

grep -q $(echo "\r\c") infile
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "CR Found"
fi

worked fine..
thanks..
Rinky
Advisor

Re: control M characters

WOW!!
dos2ux test1 > test2
perfect command..
Thanks a lotttttt...
Hein van den Heuvel
Honored Contributor

Re: control M characters

Well duh!
Just like you were informed in the very first reply a week ago!

Cheers,
Hein.


Dennis Handly
Acclaimed Contributor

Re: control M characters

>Hein: Just like you were informed in the very first reply a week ago!

Exactly.

Other than Clay's echo trick, OldSchool's file of CR and JRF's perl script, nothing was added since my first reply.
(And I only got 4 points. Though Rinky can add some more. ;-)
Yogeeraj_1
Honored Contributor

Re: control M characters

... and not to complain that the initial question was:

"I wanna check if a file contains control M [^M] characters in it."


Na Zdrave
(Bulgarian Cheers!)

yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)