Searching for escape characters

Hi all

I have been trying to write a script to look for a set of specific escape characters in a file. On viewing the file via vi it shows this :

^[p^@^E^_^@^@^@^@^@^@^@

Vi also reports at the bottom of the screen [noeol] I understand this means no end of line.

I have tried a vary of grep parameters such as grep ^\^. filename grep --binary-file=binary without any luck.

I hope someone has a genius idea! :smiley:

Most versions of awk allow you to represent non-printing characters as hex values.
\x01B is ASCII 27, the escape character.

awk '{ if (index($0, "\x01B") ) { print $0 }}' myfile

will find the escape character anywhere on a line in a file, like grep. You will need to construct a whole string of hex chars to find the exact sequence you want.

Is this what you want? : (example binary file was xxd)

root@sundude /usr/local/bin# file xxd
xxd: ELF 32-bit LSB executable 80386 Version 1, dynamically linked, stripped
root@sundude /usr/local/bin# cat -vte xxd > xxd.out
root@sundude /usr/local/bin# grep -c "@" xxd.out
57
root@sundude /usr/local/bin#

NOTE: cat -vte {file_name} will show all characters including carriage returns and such...
NOTE2: Use 'vim' (freeware) and or link vim to vi... vim can handle longer lines and larger pages...

HTH

Hi

Thanks to both of you for your replies, I assume these will work in conjunction with checking for the occurrence of the text and return a result value. They both look as though they do.

I will try these tomorrow and let you know. :slight_smile:

Hi.

A perl script:

#!/usr/bin/perl

# @(#) p1       Demonstrate search for escape.

use warnings;
use strict;

my($lines) = 0;

while ( <> ) {
        $lines++;
        print "$. $_" if /\e/xms;       # always use xms on matches
}

print STDERR " ( Lines read: $lines )\n";

exit(0);

Run on the data file data1, showing special characters:

% cat -vet data1
Now is the time to see an escape :^[:$
for all good men$
to come to the aid for escape ^[ from jail$
of their country.$

Produces:

% ./p1 data1
1 Now is the time to see an escape :
3 to come to the aid for escape from jail
 ( Lines read: 4 )

with the line number to help locate the lines ... cheers, drl

Drl

Thanks for your input, I haven't yet had chance to try the other suggestions. I think your code could be a little too much though for what I need. Also I don't know if the system I am working with has perl installed.

SunDude - tried your suggestion, the only draw back to this is that I need to look for more than just the @ sign to make the condition unique. I tried this with the other characters (excluding the [ as grep does not like looking for this character) and still didn't get a result (just got 0).

Actually scrap that I think I need to add the backslash to each character so that it is interpreted as a character and not an escape code. I will be back!

You can use grep to detect files containing control characters :

grep -l '[[:cntrl:]]' *

If you want to test if a specific file contains control characters :

if grep -q '[[:cntrl:]]" inputfile
then
   echo "File contains control character(s)"
fi

Jean-Pierre.

aigles yeah I tried this but there are draw backs to this method as well. Cheers for the help anyway