Operating System - HP-UX
1832861 Members
2778 Online
110048 Solutions
New Discussion

Re: Need a little search script to find ^L and ^M

 
SOLVED
Go to solution

Need a little search script to find ^L and ^M

Hi Folks,

I need a little help again. Can anyone suggest a way to run through a directory and search the files to see if they have any carriage return / line feeds in them. We get them in some of our text files when when transfer them in binary mode from our unix boxes to our LAN. The dos2unix utility can strip them off if I can identify them 1st. Any suggestions? Thank you in advance.
7 REPLIES 7
Cheryl Griffin
Honored Contributor

Re: Need a little search script to find ^L and ^M

# ll | cat -vet

Cheryl
"Downtime is a Crime."
Cheryl Griffin
Honored Contributor

Re: Need a little search script to find ^L and ^M

Here's a test:
I created a file with vi:
# vi ^l^y^t (by hitting the control L, etc.)
this is a test
:wq!

# ll <--- does not show the file
# ll | cat -vet
total 1124$
-rw-rw-rw- 1 root sys 16 Oct 17 14:52 ^L^Y^T$ <-- here is my file

Here's what a "regular" file looks like:
-rw------- 1 root sys 297 May 13 09:18 .TTauthority$

The $ marks the end of the file name.
The cat -vet helps print those unseen characters.

Cheryl
"Downtime is a Crime."
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Need a little search script to find ^L and ^M

Here's one method off the top of my head:

find . -type f -print | while read FNAME
do
awk '/.*\015/ { exit 99 }; /.*\014/ { exit 98 }' ${FNAME}
STAT=${?}
if [[ ${STAT} -eq 99 || ${STAT} -eq 98 ]]
then
echo "File ${FNAME} needs some work."
fi
done
If it ain't broke, I can fix that.
John Poff
Honored Contributor

Re: Need a little search script to find ^L and ^M

Hi,

If I understand your question, you just want to find the files that have carriage return characters in them and run the dos2ux utility on them. Here is a little script I came up with to do it. To get the ^M character in the grep line, I had to hit Ctrl-X and then Ctrl-M. I also had to do it in sh as I couldn't get a ^M entered in ksh, but that is another problem. Take a look at this:


#!/bin/sh

# find_dos_files

for f in $(ls *.txt)
do
CR=$(grep -c '^M' $f)
if [[ CR -gt 0 ]]
then
echo "The $f file has CR/LF and needs to be converted."
mv $f $f.tmp
dos2ux $f.tmp >$f
rm $f.tmp
fi
done




JP
H.Merijn Brand (procura
Honored Contributor

Re: Need a little search script to find ^L and ^M

*untested*
: find all files down from the current folder that contain any characters from ^A to ^
# find . -type d | perl -nle'opendir D,$_;for$f(grep/[\001-\037],readdir D){$f=~s/([\001-\037])/sprintf"^%03o",ord$1/ge;print "$_/$f"}'
Enjoy, Have FUN! H.Merijn
Steve Lewis
Honored Contributor

Re: Need a little search script to find ^L and ^M

To list the filenames containing the sequence under /dir, this worked for me:

echo '^M' > x
find /dir -type f | xargs grep -lf x {}

To get the '^M' use ctrl-V ctrl-M

Re: Need a little search script to find ^L and ^M

Thank you everyone. I found my problem files and got them fixed. Good job!