1

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?

TryingHard
  • 11
  • 1
  • 2

3 Answers3

0

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
  • 51,086
  • 7
  • 70
  • 105
0

If you want just the binary, and not even the offset, you could extend Thomas Dickey's sed one-liner to strip the offset too:

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

"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
Tic-Tac
  • 1
  • 1