Reading a certain number of bytes from standard input and then closing the pipe

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question























    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?







    share|improve this question





















      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?







      share|improve this question











      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?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 5 at 19:19









      bdesham

      279210




      279210




















          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.






          share|improve this answer




























            up vote
            0
            down vote













            One simple way to make anything into readable characters:



            $ head -c30 /dev/urandom | base64
            nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW





            share|improve this answer





















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              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






























              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer













                  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.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 5 at 20:22









                  ilkkachu

                  47.7k668131




                  47.7k668131






















                      up vote
                      0
                      down vote













                      One simple way to make anything into readable characters:



                      $ head -c30 /dev/urandom | base64
                      nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        One simple way to make anything into readable characters:



                        $ head -c30 /dev/urandom | base64
                        nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW





                        share|improve this answer























                          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





                          share|improve this answer













                          One simple way to make anything into readable characters:



                          $ head -c30 /dev/urandom | base64
                          nkGmuXgY/1OfNz8i/t3MsVe/5Q0z18AKotV3oJRW






                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Jun 5 at 19:23









                          DopeGhoti

                          39.8k54779




                          39.8k54779






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              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













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?