Searching for escape characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for escape characters
# 1  
Old 05-24-2007
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! Smilie
# 2  
Old 05-24-2007
Most versions of awk allow you to represent non-printing characters as hex values.
\x01B is ASCII 27, the escape character.

Code:
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.
# 3  
Old 05-24-2007
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
# 4  
Old 05-24-2007
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. Smilie
# 5  
Old 05-24-2007
Hi.

A perl script:
Code:
#!/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:
Code:
% 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:
Code:
% ./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
# 6  
Old 05-25-2007
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.
# 7  
Old 05-25-2007
Quote:
Originally Posted by SunDude
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

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!

Last edited by timcs; 05-25-2007 at 04:26 AM.. Reason: Mistake
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escape characters in a variable

Debian 9 64x - LXDE How can i disable escape sequences in a variable? #!/bin/bash #mainscript . "./links.bash" echo "$red_start This text should be red $color_end"#!/bin/bash #links.bash #colors red_start="\eOutput that i get: \e Output expected: This text should be... (5 Replies)
Discussion started by: int3g3r
5 Replies

2. Shell Programming and Scripting

How to escape all special characters?

I have an application which I am integrating with that accepts the password via a CLI. I am running in to issues with passwords that contain special characters. I tried to escape them all, but I ran in to an issue where I cannot escape the characters ' ] My attempt is as follows: $... (2 Replies)
Discussion started by: AMG1978
2 Replies

3. Shell Programming and Scripting

Escape characters

i am executing script from A server which will execute the script in B server , as below. ssh A 'ssh B echo 'select * from testing where name ='test'' i am getting the below output. select * from testing where name=test but i need the output where clause with quotes , tried with... (3 Replies)
Discussion started by: expert
3 Replies

4. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

5. Shell Programming and Scripting

Removing Escape Sequence Characters

Hi All, I have added the script command to user profile so that to record the on-screen data.But when i i checked the O/P i could see lot of escape sequence is there way to remove it. (2 Replies)
Discussion started by: cutechaps
2 Replies

6. UNIX for Advanced & Expert Users

Remove escape characters from string

Hello all, I have a string var which contains formatting characters at the end, it is a string with EScape sequences at the end of it. How can I remove them so that I only keep the 'real' text? I tried : var1=${var1%%\033[0m} does not seem to do the job .... Please help Thanks (2 Replies)
Discussion started by: gio001
2 Replies

7. UNIX for Dummies Questions & Answers

Escape Characters on various shells

Hi, I want to know if escape charaters work on all the popular UNIX shells. More specifically I want to know if echo "\c" will work on most of the UNIX shells and are there any specific shells on which \c won't work. Please help. Thanks, Vineet (2 Replies)
Discussion started by: vineetd
2 Replies

8. Shell Programming and Scripting

escape characters..

hey i want to know the unix commands to replace all the character escape sequences with their "C" values in a string... thanks in advance..! Regards, Sharanya (9 Replies)
Discussion started by: sharsin2001
9 Replies

9. Shell Programming and Scripting

number of escape characters?

Hi, I am trying to execute the following command from a batch script, but no matter how many escape characters I put in it doesn't execute properly. It works fine from the command line with quotes around the -exec part. #!/bin/sh /usr/local/bin/sudo /usr/atria/bin/cleartool setview -exec... (0 Replies)
Discussion started by: Sebarry
0 Replies

10. UNIX for Advanced & Expert Users

lp FormFeed Escape characters

I'm trying to modify the /usr/lib/lp/model/netstandard file to generate a header for all the print jobs that are sent, but there is no formfeed defined so the the job prints right after the header with no page break. What is the sequence I need in order to generate a formfeed? Or, do you have... (4 Replies)
Discussion started by: jgordon
4 Replies
Login or Register to Ask a Question