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



Nasif Abdur Razzaque said...

Your blog is helpful to the code geek people


Alan said...

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

grep -P "\x02" myfile.txt

djhayman said...

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...


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?


Jadu Saikia said...

@djhayman, something like:

printf "\x01\n" | less

which is ^A (control A)

thanks for the question.

djhayman said...


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

The command is

Can you help?


Jadu Saikia 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:

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 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.


© Jadu Saikia