Wednesday, October 28, 2009

Grep and print control characters in file - unix


One of my input file had some control characters (^B i.e. hex \x02)



On my Ubuntu 8.04.3 and GNU grep version of

$ grep --version
GNU grep 2.5.3

I can grep for any control characters like this:

$ grep '[[:cntrl:]]' /tmp/file.txt
$ grep '[[:cntrl:]]' /tmp/file.txt | less




Also if you know what to grep for, say in above example the control character is ^B (hex \x02); then you can directly grep for it like this

$ grep ^B /tmp/file.txt

* ^B to be typed as ctrl V and ctrl B

And to match any non printable characters, here is another way using grep

$ grep '[^[:print:]]' /tmp/file.txt


To display non printable characters, here is a way using GNU cat command (My cat version : cat GNU coreutils 6.10)

$ cat -v -e -t /tmp/s

Output:

8 comments:

Anonymous said...

Your blog is helpful to the code geek people

Nasif

Unknown said...

I find it easier to use Perl regexps and the hex value...

grep -P "\x02" myfile.txt

djhayman said...

hello,
I am looking to include control characters in a script. Is there a way for a shell to translate asci into the control characters to then be piped in netcat?

djhayman said...

Hello,

I am looking to include control character in a bash script. It there any way for bash to translate asci into the special control characters?

Cheers
Dan

Unknown said...

@djhayman, something like:

printf "\x01\n" | less

which is ^A (control A)

thanks for the question.

djhayman said...

Hello,

Thanks very much for your reply Jadu. I am having a bit of trouble formatting the command.

The command is
0x02PON0x030x0d

Can you help?

Cheers
Dan

Unknown said...

@djhayman, could you please explain your last query again with an example, I would definitely help you here.

Do you mean something like ?
printf "0\x02PON0\x030\x0d" | less

Which is:
0^BPON0^C0

djhayman said...

Hi Jadu,

Sorry for such a late reply.

Basically, we have some TVs on a network which can be controlled over the LAN. We have some software that does this but I am exploring making a piece of software dedicated to control them.

I have been able to control them by inputting 0x02PON0x03 into this website http://chxo.com/scripts/hex2string.php then taking the special characters and saving them in a text file. Next I would pipe the text file into a net cat command.

I would like to create an application that has greater flexibilty rather than requring a pre made text file for each command.

I hope I have been clear.

Thanks,
Dan

© Jadu Saikia www.UNIXCL.com