25

Can I tell xxd to not print any line breaks in its output and have my dump as one continuous line?

[user@localhost] : ~ $ echo -n "this is a long line and xxd is going to take multiple lines to print it" | xxd -p
746869732069732061206c6f6e67206c696e6520616e6420787864206973
20676f696e6720746f2074616b65206d756c7469706c65206c696e657320
746f207072696e74206974
dr_
  • 28,763
  • 21
  • 89
  • 133
Juicy
  • 3,685
  • 11
  • 31
  • 44
  • 10
    You could simply use `tr` to delete the newlines, e.g. `... | xxd -p | tr -d \\n` – don_crissti Jul 26 '15 at 23:12
  • 1
    It depends on what you need it for, but one handy option of `xxd` is that it ignores whitespace for the reverse `-r` of its postcript/plain `-p` dump (or any plain hexdump for that matter). eg. The following line wraps with `\n`, but the reversed output is exactly what was input: `echo {1..14} | xxd -p | xxd -p -r` produces output: `1 2 3 4 5 6 7 8 9 10 11 12 13 14\n` – the `\n` is from the `echo` – Peter.O Jul 27 '15 at 00:20
  • 3
    You can also use `hexdump -v -e '/1 "%02X"'` instead of `xxd`. – dirkt Oct 10 '16 at 16:29
  • I'd stick with piping to `tr -d '[[:blank:][:space:]]'`. – Yan Foto Nov 17 '22 at 12:20

2 Answers2

28

What you need is the -c option.

# echo -n "this is a long line and xxd will print it as one line" | xxd -p -c 1000000

746869732069732061206c6f6e67206c696e6520616e64207878642077696c6c207072696e74206974206173206f6e65206c696e65

Here is some info from the documentation:

-c cols | -cols cols format octets per line. Default 16 (-i: 12, -ps: 30, -b: 6). Max 256.

Documentation says that the max value for "c" parameter is 256, but I tried greater values and it worked. Check it out:

# xxd -c 1000000 -p -l 1000000 /dev/urandom | wc -c
2000001

Here I dump one million bytes from /dev/random and I get a string of 2 million + 1 characters. Each byte from /dev/random is represented by 2 characters and additional byte is the final newline.

Graf
  • 381
  • 3
  • 3
4

With xxd version 2022-01-14 (coming with vim 8.2.4088) or newer, you can now use '-p' and '-c' with value 0 as said in the man page:

-c cols | -cols cols

Format cols octets per line. Default 16 (-i: 12, -ps: 30, -b: 6). Max 256. No maxmimum [sic] for -ps. With -ps, 0 results in one long line of output.

for example:

$ echo "Lorem Ipsum is simply dummy text of the printing and typesetting industry." | xxd -p -c0 
4c6f72656d20497073756d2069732073696d706c792064756d6d792074657874206f6620746865207072696e74696e6720616e64207479706573657474696e6720696e6475737472792e0a
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501