Reading a certain number of bytes from standard input and then closing the pipe
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
IâÂÂm trying to read some bytes out of /dev/urandom, keep only the ones I can type easily, and trim the result to 30 characters. I canâÂÂt figure out how to get the âÂÂonly 30 charactersâ behavior when the data is coming from a pipe, though. I tried
cat /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
and
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
but these hang without displaying anything. On the other hand,
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9'
outputs data endlessly (although itâÂÂs clearly been processed by tr
because only the specified characters appear in the output).
In general, how can I read a certain number of characters (or bytes) from a pipe and then close the pipe?
command-line pipe
add a comment |Â
up vote
0
down vote
favorite
IâÂÂm trying to read some bytes out of /dev/urandom, keep only the ones I can type easily, and trim the result to 30 characters. I canâÂÂt figure out how to get the âÂÂonly 30 charactersâ behavior when the data is coming from a pipe, though. I tried
cat /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
and
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
but these hang without displaying anything. On the other hand,
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9'
outputs data endlessly (although itâÂÂs clearly been processed by tr
because only the specified characters appear in the output).
In general, how can I read a certain number of characters (or bytes) from a pipe and then close the pipe?
command-line pipe
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
IâÂÂm trying to read some bytes out of /dev/urandom, keep only the ones I can type easily, and trim the result to 30 characters. I canâÂÂt figure out how to get the âÂÂonly 30 charactersâ behavior when the data is coming from a pipe, though. I tried
cat /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
and
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
but these hang without displaying anything. On the other hand,
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9'
outputs data endlessly (although itâÂÂs clearly been processed by tr
because only the specified characters appear in the output).
In general, how can I read a certain number of characters (or bytes) from a pipe and then close the pipe?
command-line pipe
IâÂÂm trying to read some bytes out of /dev/urandom, keep only the ones I can type easily, and trim the result to 30 characters. I canâÂÂt figure out how to get the âÂÂonly 30 charactersâ behavior when the data is coming from a pipe, though. I tried
cat /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
and
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
but these hang without displaying anything. On the other hand,
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9'
outputs data endlessly (although itâÂÂs clearly been processed by tr
because only the specified characters appear in the output).
In general, how can I read a certain number of characters (or bytes) from a pipe and then close the pipe?
command-line pipe
asked Jun 5 at 19:19
bdesham
279210
279210
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
cut
will read the input until end-of-file, and cut a part out of each line. With the two first commands, you don't see anything, since tr
removes any newlines, and so cut
never sees a full line to process. In the last, the tr
again removes newlines so you don't see how nicely cut
kept the lines to a certain length.
Assuming your head
utility supports the -c <characters>
option, you could use it to get a fixed number of bytes. If it doesn't, use dd
. (dd bs=1 count=NNN < /dev/urandom
)
So, this would produce 32 alphanumerics (and a trailing newline):
tr -cd 'A-Za-z0-9' < /dev/urandom | head -c32 ; echo
Though, just out of principle, that's a bit of a waste since the tr
tosses away about 3/4 or the raw input bytes. It might be better to pass the raw data from urandom
to something like base64
or anything that produces a hex dump.
add a comment |Â
up vote
0
down vote
One simple way to make anything into readable characters:
$ head -c30 /dev/urandom | base64
nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
cut
will read the input until end-of-file, and cut a part out of each line. With the two first commands, you don't see anything, since tr
removes any newlines, and so cut
never sees a full line to process. In the last, the tr
again removes newlines so you don't see how nicely cut
kept the lines to a certain length.
Assuming your head
utility supports the -c <characters>
option, you could use it to get a fixed number of bytes. If it doesn't, use dd
. (dd bs=1 count=NNN < /dev/urandom
)
So, this would produce 32 alphanumerics (and a trailing newline):
tr -cd 'A-Za-z0-9' < /dev/urandom | head -c32 ; echo
Though, just out of principle, that's a bit of a waste since the tr
tosses away about 3/4 or the raw input bytes. It might be better to pass the raw data from urandom
to something like base64
or anything that produces a hex dump.
add a comment |Â
up vote
1
down vote
accepted
cut
will read the input until end-of-file, and cut a part out of each line. With the two first commands, you don't see anything, since tr
removes any newlines, and so cut
never sees a full line to process. In the last, the tr
again removes newlines so you don't see how nicely cut
kept the lines to a certain length.
Assuming your head
utility supports the -c <characters>
option, you could use it to get a fixed number of bytes. If it doesn't, use dd
. (dd bs=1 count=NNN < /dev/urandom
)
So, this would produce 32 alphanumerics (and a trailing newline):
tr -cd 'A-Za-z0-9' < /dev/urandom | head -c32 ; echo
Though, just out of principle, that's a bit of a waste since the tr
tosses away about 3/4 or the raw input bytes. It might be better to pass the raw data from urandom
to something like base64
or anything that produces a hex dump.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
cut
will read the input until end-of-file, and cut a part out of each line. With the two first commands, you don't see anything, since tr
removes any newlines, and so cut
never sees a full line to process. In the last, the tr
again removes newlines so you don't see how nicely cut
kept the lines to a certain length.
Assuming your head
utility supports the -c <characters>
option, you could use it to get a fixed number of bytes. If it doesn't, use dd
. (dd bs=1 count=NNN < /dev/urandom
)
So, this would produce 32 alphanumerics (and a trailing newline):
tr -cd 'A-Za-z0-9' < /dev/urandom | head -c32 ; echo
Though, just out of principle, that's a bit of a waste since the tr
tosses away about 3/4 or the raw input bytes. It might be better to pass the raw data from urandom
to something like base64
or anything that produces a hex dump.
cut
will read the input until end-of-file, and cut a part out of each line. With the two first commands, you don't see anything, since tr
removes any newlines, and so cut
never sees a full line to process. In the last, the tr
again removes newlines so you don't see how nicely cut
kept the lines to a certain length.
Assuming your head
utility supports the -c <characters>
option, you could use it to get a fixed number of bytes. If it doesn't, use dd
. (dd bs=1 count=NNN < /dev/urandom
)
So, this would produce 32 alphanumerics (and a trailing newline):
tr -cd 'A-Za-z0-9' < /dev/urandom | head -c32 ; echo
Though, just out of principle, that's a bit of a waste since the tr
tosses away about 3/4 or the raw input bytes. It might be better to pass the raw data from urandom
to something like base64
or anything that produces a hex dump.
answered Jun 5 at 20:22
ilkkachu
47.7k668131
47.7k668131
add a comment |Â
add a comment |Â
up vote
0
down vote
One simple way to make anything into readable characters:
$ head -c30 /dev/urandom | base64
nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW
add a comment |Â
up vote
0
down vote
One simple way to make anything into readable characters:
$ head -c30 /dev/urandom | base64
nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW
add a comment |Â
up vote
0
down vote
up vote
0
down vote
One simple way to make anything into readable characters:
$ head -c30 /dev/urandom | base64
nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW
One simple way to make anything into readable characters:
$ head -c30 /dev/urandom | base64
nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW
answered Jun 5 at 19:23
DopeGhoti
39.8k54779
39.8k54779
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448060%2freading-a-certain-number-of-bytes-from-standard-input-and-then-closing-the-pipe%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password