简体   繁体   中英

xxd output only the binary

Using the command :

xxd -b some_text

will output the binary representation and the original text:

000054c: 11101111 10100001 10110110 00010000 01011011 10000110  ....[.
0000552: 01111000 11001000 01010101 11000101 11101111 10101111  x.U...
0000558: 10010111 11101010 00011001 01010100 10101010 10001110  ...T..

How can I have the utility output only the binary without anything else?

That combination is not listed in its manual page . However, you could filter the result using awk/sed/perl (your choice). For the given example, sed suffices:

xxd -b some_text | sed -e 's/^[^:]*:[[:space:]][[:space:]]*//' -e 's/[[:space:]][[:space:]]*.\{6,6\}$//'

That is:

  • In the first "-e" option, remove everything through the first ":", followed by whitespace.

  • In the second "-e" option, starting with at least one space

  • followed by 6 characters (it's unclear if those can be spaces, but testing seems to confirm they are not)
  • strip off the entire matched string

You could replace the "[[:space:]][[:space:]]*" with exactly the number of spaces which you want to match, but though more verbose, the character class is also more powerful.

Alternatively (since whitespace is a default field separator in awk), one could pipe through awk:

xxd -b some_text | awk '{ printf "%s %s %s %s %s %s\n", $2, $3, $4, $5, $6, $7 }'

though as a rule I use the simplest tool which works.

如果您只想要二进制文件,而不是偏移量,则可以扩展Thomas Dickey的sed衬套来去除偏移量:

xxd -b some_text | sed -e 's/^.*: //; s/[[:space:]][[:space:]]*.\{6,6\}$//'

"xxd" do not take "some_text" as input but "infile".
Shorter answer using 'cut' command printing 6 fields (2 to 7) :

xxd -b some_file | cut -d\  -f2-7

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