Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am running this:

cat /dev/urandom|hexdump| grep -i "ffff f" > random

and I get nothing in file random, it remains zero length after the command is interrupted.

How to make it writing output to a file?

I need to write a result to a file, which should contains output data like this:

021bc40 7724 d4f5 59ec bcbb ffff fd26 ab3c 9b7c

03a9100 b3a5 8601 fa33 ffff f23c 4326 2e7f 0c8a

0449810 e459 d5af 4e11 ffff fc55 8660 9efb 3c9b

enter image description here

share|improve this question

4 Answers 4

up vote 7 down vote accepted

Use the --line-buffered option for grep (and also get rid of the useless cat):

hexdump /dev/urandom | grep --line-buffered -i "ffff f" > random

This way the output is not buffered but every line put into random immediately. I would also recommend to use tee in your pipe to see how many lines have been produced:

hexdump /dev/urandom | grep --line-buffered -i "ffff f" | tee random
share|improve this answer
    
Perfectly does the trick. Thank you very much. –  Ruslan Gerasimov Jun 26 at 9:06

Your file is empty because the process is interrupted before the file is written to disk. That is how redirection works. As a workaround, try this:

script -c 'cat /dev/urandom|hexdump|grep -i "ffff f"' -f random

This will basically write all screen output to the file.

share|improve this answer
    
It involved one more command, but delivered. Thank you. –  Ruslan Gerasimov Jun 26 at 9:05

cat /dev/urandom|hexdump or hexdump /dev/urandom writes continuously to the stdout and if you press Ctrl+C nothing will be executed after this. But you can limit the output with head

hexdump /dev/urandom | head -n1000000 | grep "ffff f" > random  

this will grep in the first 1000000 lines of the output and writes the result to a file.

share|improve this answer
    
Involved one more command, but gave a good result. Thank you. –  Ruslan Gerasimov Jun 26 at 9:07

You will need to write the output of cat /dev/urandom | hexdump to a file before you execute it the next time. The script below should accomplish what you are trying:

cat /dev/urandom | hexdump |
  while IFS= read -r line; do
    printf '%s\n' "$line" >> random;
  done

IFS is used to split the output into lines here.


(Source)

share|improve this answer
    
grep "ffff f" missed –  Ruslan Gerasimov Jun 26 at 8:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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