Operating System - HP-UX
1834088 Members
3178 Online
110063 Solutions
New Discussion

how to catch DOS carriage return by egrep

 
SOLVED
Go to solution
Gary Yu
Super Advisor

how to catch DOS carriage return by egrep

Dear all,

we have some files initially generated in some editors in windows environment and uploaded, which have DOS carriage return, it has a trailing '^M' if opened in vi, how can I identify those files by grep or egrep?

thanks,
Gary
6 REPLIES 6
RAC_1
Honored Contributor

Re: how to catch DOS carriage return by egrep

You can do as grep "\^M" file.

I think you need to look at dos2ux command.
man dos2ux

Anil
There is no substitute to HARDWORK
Hein van den Heuvel
Honored Contributor

Re: how to catch DOS carriage return by egrep



- check out 'dos2ux', a standard tool


- using 'man ed' you'll see you can express the dos line ends ( CR - LF ) with \r$

Hein.


Steve Steel
Honored Contributor
Solution

Re: how to catch DOS carriage return by egrep

Hi


grep -il CntrlVcntrlM filelist


Note controlV followed by control M


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Chris Wilshaw
Honored Contributor

Re: how to catch DOS carriage return by egrep

try

grep ^V^M$ file

The ^V should allow you enter the ^M as a single character. The $ specifies that you're checking at the end of the line.

To remove them, use

cat file | sed -e s/^V^M$//g > new_file
Gary Yu
Super Advisor

Re: how to catch DOS carriage return by egrep

thank you guys, Steve's and Chris' way works fine. I know I can use dos2ux to convert them, I just need to identify those files for some other stuff.

thanks again,
Gary
A. Clay Stephenson
Acclaimed Contributor

Re: how to catch DOS carriage return by egrep

Here is one technique leveraging awk and looking for a terminal CR on a line. It exits with a status of 1 if a CR is detected and 0 otherwise.

#!/usr/bin/sh

INFILE=/mydir/myfile
awk 'BEGIN { X=0 } /.*\015$/ {X = 1; exit(X)} END {exit(X)}' < ${INFILE}
STAT=${?}
if [[ ${STAT} -eq 1 ]]
then
echo "File ${INFILE} has CR's"
else
echo "No CR's"
fi
exit ${STAT}


If it ain't broke, I can fix that.