简体   繁体   中英

How can I extract the text portion of a binary file in Linux/Bash?

I have a binary file. If I open it with vi , it shows sequences of human-readable text and binary characters. What is the best way to extract the human-readable portion only using Bash?

I was thinking, maybe we can do this over a grep or sed pattern?

cat file1.bin | grep '????'  > newfile.txt

使用strings实用程序 - 这正是它的设计目的。

If you're on a Debian distro, you can probably get radare2 (r2) with just sudo apt install radare2 .

After you've installed r2, either with apt , some other installer on some other distro, or by following an online guide, you can use rabin2 to extract just the text part of a binary:

$ rabin2 -z your_binary

This is often "better" than just strings because it outputs just the useful .data section of the binary. Stuff outside that section isn't always very useful.

Here's what I used in a system that didn't have the "strings" utility installed

cat yourfilename | tr -cd "[:print:]"

This prints the text and removes unprintable characters in one fell swoop, unlike "cat -v filename" which prints only text but requires some postprocessing to remove unwanted stuff. Note that some of the binary data may be printable so you'll still get some gibberish between the good stuff. I think strings removes this gibberish too if you can use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM