If I do something like:

openssl rand 16 > file.txt

I get a file that looks like this:

3e1c 37e2 96fc 7029 970c cc87 98fe 40f7
7c70 5a9a 30da 7b72 0064 9a54 ae9c ed91

Is there a way to do this without the spaces an newline characters?

  • You've got a weird version of openssl if that's the output you get. That is not the normal output. – Patrick May 12 '17 at 1:49
  • for ex. openssl rand 16 -hex gave me 716e2480fa82103b5b2e1ea52d12e03c . No spaces – RomanPerekhrest May 12 '17 at 6:53

You can use tr to delete the spaces and newlines like:

openssl rand 16 | tr -d '\n '  > file.txt

Or if your are really bad a remembering flags, you can use tr to convert the newlines to spaces, and then use sed to strip the spaces.

openssl rand 16 | tr '\n' ' ' | sed 's/ //g' > file.txt
  • Why use sed instead of tr -d? – Gilles May 13 '17 at 23:27
  • @Gilles, because I am terrible at remembering flags.... Thanks for the heads up... – Stephen Rauch May 13 '17 at 23:47

openssl rand 16 doesn't produce hexadecimal output, and openssl rand -hex 16 doesn't output whitespace, and it outputs 16 bytes (32 hex digits), not 32 bytes (64 digits). But anyway, if you want to remove spaces and newlines, you can use tr:

$ cat data
3e1c 37e2 96fc 7029 970c cc87 98fe 40f7
7c70 5a9a 30da 7b72 0064 9a54 ae9c ed91
$ cat data | tr -d '\n '; echo

To remove all characters that aren't hexadecimal digits, you can use tr -dc 0-9A-Fa-f or tr -dc '[:xdigit:]'.

If you want to produce random hexadecimal digits, you can get them from /dev/urandom (on Linux, might be /dev/random or /dev/arandom on other unix variants).

</dev/urandom od -An -tx1 | head -c 32 | tr -d ' \n' 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.