Operating System - HP-UX
1822552 Members
2941 Online
109642 Solutions
New Discussion юеВ

Re: When I cat a binary file, I get garbled text...

 
SOLVED
Go to solution
jmckinzie
Super Advisor

When I cat a binary file, I get garbled text...

When I cat a binary file, i get garbled text, then I have to logofff and back on again for it to go away.

Also, it takes my terminal emulator (exceed) and makes my quick-keys not work.

IE, i have a quick key called nsu that runs a script i run al lthe time....

After I cat the bianry, I push the quickey and get this

nsu^[

It is supposed to be only nsu.

Any ideas?

Thanks,
12 REPLIES 12
Pete Randall
Outstanding Contributor
Solution

Re: When I cat a binary file, I get garbled text...

Use the "strings" command against binary files.


Pete

Pete
Enrico P.
Honored Contributor

Re: When I cat a binary file, I get garbled text...

Hi,
you can use the strings command to view the printable strings in a binary file.

Enrico
Phillip Thayer
Esteemed Contributor

Re: When I cat a binary file, I get garbled text...

I dont't think you can use cat on binaries. You will need to use something else or reasses whether you really want to display the contects of a binary file.

Phil
Once it's in production it's all bugs after that.
James R. Ferguson
Acclaimed Contributor

Re: When I cat a binary file, I get garbled text...

Hi Jody:

Listing binary files with 'cat' or 'more' will often lead to the results you see!

You need to know the internal structure of your binary data in order to read it sensibly.

# 'strings' filename

...will attempt to present the best graphic representation of four or more "printing" characters but will *not* guarantee that you "see" everything.

# od filename
# xd filename

...provide you octal and hexadecimal renditions of your file, respectively.

Should you inadvertantly 'cat' or 'more' a binary file and corrupt your terminal, try:

# stty sane

...This may restore your terminal configuration and avoid the logoff you have been doing.

Lastly, test your files beforehand with:

# file filename

...If you see "ascii", then the file is a "text" file and is safe to 'cat' or 'more'.

Regards!

...JRF...
Doug O'Leary
Honored Contributor

Re: When I cat a binary file, I get garbled text...

>>Any ideas?

Yea; don't cat binary files. cat is for text files. If you want to see the text (and only the text) in a binary file, use strings...

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Deoncia Grayson_1
Honored Contributor

Re: When I cat a binary file, I get garbled text...

cat is better used on non-binary files, you should use strings on binary files in order to read it...
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Torsten.
Acclaimed Contributor

Re: When I cat a binary file, I get garbled text...

What did you expect to see?

Some binary data will be interpreted as control characters - this will confuse your emulation. If you want to "see" your binary file, use "strings" or a hex editor.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
spex
Honored Contributor

Re: When I cat a binary file, I get garbled text...

Jody,

Performing a cat of a binary file is one of the more common mistakes. The 'reset' command will reset your terminal to its defaults.

PCS
Patrick Wallek
Honored Contributor

Re: When I cat a binary file, I get garbled text...

Ideas -- Yes, don't cat a binary file. The behavior you see is not unexpected. Since the file is binary, you are going to see all kinds of strange behavior since there will be escape sequences, at least that's how they'll be interpreted, and other things going to your terminal.

If you really want to look at a binary file use the strings command.

# strings filename | more

This will pull out any "human readable" text. 'man strings' for more information.
A. Clay Stephenson
Acclaimed Contributor

Re: When I cat a binary file, I get garbled text...

Actually cat can be (and often is) used on binary files to, for example, conCATentate several binary files into one. However, when redirected to stdout and stdout is a terminal the the file is simply displayed intact and the terminal emulator does its best -- which is typically garbage to human eyes. The terminal emulator looks for specific sequences of characters to clear the screen, move the curson, display reverse video, blink, and change character sets. By the luck of the draw, your file contains some sequence of non-ASCII characters that your terminal emulator is doing it's best to obey.

One of the best tools to display these files is hexedit. http://hpux.cs.utah.edu/hppd/hpux/Development/Tools/hexedit-1.2.10/

The standard tools are od and xd. Man od for details.

If it ain't broke, I can fix that.
jmckinzie
Super Advisor

Re: When I cat a binary file, I get garbled text...

Thanks for al lthe responses...
The answer I was looking for was the reset command....

It stops the garbled text on the quick keys.

Thanks,
jmckinzie
Super Advisor

Re: When I cat a binary file, I get garbled text...

the reset command worked.