Redirect an output to a file from command piping grep

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

0

4 Answers

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
0

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.

1

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.

1

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)

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like