Re-print an array in a certain format?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1














I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with



ind=$(shuf -i 1-100 -n 10 | sort -n)


Then, I wish to use sed to extract out those lines (indicated by ind). So I need to re-print the array ind to generate



<ind(1)>p;<ind(2)>p;...;<ind(10)>p


as in



sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt


How may I do this?










share|improve this question




























    1














    I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with



    ind=$(shuf -i 1-100 -n 10 | sort -n)


    Then, I wish to use sed to extract out those lines (indicated by ind). So I need to re-print the array ind to generate



    <ind(1)>p;<ind(2)>p;...;<ind(10)>p


    as in



    sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt


    How may I do this?










    share|improve this question


























      1












      1








      1







      I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with



      ind=$(shuf -i 1-100 -n 10 | sort -n)


      Then, I wish to use sed to extract out those lines (indicated by ind). So I need to re-print the array ind to generate



      <ind(1)>p;<ind(2)>p;...;<ind(10)>p


      as in



      sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt


      How may I do this?










      share|improve this question















      I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with



      ind=$(shuf -i 1-100 -n 10 | sort -n)


      Then, I wish to use sed to extract out those lines (indicated by ind). So I need to re-print the array ind to generate



      <ind(1)>p;<ind(2)>p;...;<ind(10)>p


      as in



      sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt


      How may I do this?







      shell-script sed array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 27 '15 at 22:57









      Gilles

      528k12810581583




      528k12810581583










      asked May 27 '15 at 16:38









      Sibbs Gambling

      5252922




      5252922




















          4 Answers
          4






          active

          oldest

          votes


















          3














          In your example, ind is a string, not an array. You must use:



          ind=($(shuf -i 1-100 -n 10 | sort -n))


          to make ind to be array (in shell support array like bash, zsh, ksh).



          Simply, you can try:



          $ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
          $ sed -n -f /tmp/short.sed < orig.txt > short.txt





          share|improve this answer






























            2














            An awk based solution (without need of sorting):



            awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt


            And a pure GNU awk variant (without need of other external processes):



            awk '
            BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
            NR in a
            ' ~/orig.txt > ~/short.txt





            share|improve this answer






























              0














              Instead of trying to go the long way, creating a sed script to extract the lines from your file, just use shuf directly on the file:



              shuf -n 10 ~/short.txt


              If you want the lines in the order that they are found in the original file:



              cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-


              This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.






              share|improve this answer






























                0














                U could...



                shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt


                or, under bash



                sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt





                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',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  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%2f205919%2fre-print-an-array-in-a-certain-format%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  3














                  In your example, ind is a string, not an array. You must use:



                  ind=($(shuf -i 1-100 -n 10 | sort -n))


                  to make ind to be array (in shell support array like bash, zsh, ksh).



                  Simply, you can try:



                  $ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
                  $ sed -n -f /tmp/short.sed < orig.txt > short.txt





                  share|improve this answer



























                    3














                    In your example, ind is a string, not an array. You must use:



                    ind=($(shuf -i 1-100 -n 10 | sort -n))


                    to make ind to be array (in shell support array like bash, zsh, ksh).



                    Simply, you can try:



                    $ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
                    $ sed -n -f /tmp/short.sed < orig.txt > short.txt





                    share|improve this answer

























                      3












                      3








                      3






                      In your example, ind is a string, not an array. You must use:



                      ind=($(shuf -i 1-100 -n 10 | sort -n))


                      to make ind to be array (in shell support array like bash, zsh, ksh).



                      Simply, you can try:



                      $ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
                      $ sed -n -f /tmp/short.sed < orig.txt > short.txt





                      share|improve this answer














                      In your example, ind is a string, not an array. You must use:



                      ind=($(shuf -i 1-100 -n 10 | sort -n))


                      to make ind to be array (in shell support array like bash, zsh, ksh).



                      Simply, you can try:



                      $ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
                      $ sed -n -f /tmp/short.sed < orig.txt > short.txt






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 28 '15 at 3:23

























                      answered May 27 '15 at 16:50









                      cuonglm

                      102k23200301




                      102k23200301























                          2














                          An awk based solution (without need of sorting):



                          awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt


                          And a pure GNU awk variant (without need of other external processes):



                          awk '
                          BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
                          NR in a
                          ' ~/orig.txt > ~/short.txt





                          share|improve this answer



























                            2














                            An awk based solution (without need of sorting):



                            awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt


                            And a pure GNU awk variant (without need of other external processes):



                            awk '
                            BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
                            NR in a
                            ' ~/orig.txt > ~/short.txt





                            share|improve this answer

























                              2












                              2








                              2






                              An awk based solution (without need of sorting):



                              awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt


                              And a pure GNU awk variant (without need of other external processes):



                              awk '
                              BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
                              NR in a
                              ' ~/orig.txt > ~/short.txt





                              share|improve this answer














                              An awk based solution (without need of sorting):



                              awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt


                              And a pure GNU awk variant (without need of other external processes):



                              awk '
                              BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
                              NR in a
                              ' ~/orig.txt > ~/short.txt






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 27 '15 at 17:20

























                              answered May 27 '15 at 17:07









                              Janis

                              10.1k21437




                              10.1k21437





















                                  0














                                  Instead of trying to go the long way, creating a sed script to extract the lines from your file, just use shuf directly on the file:



                                  shuf -n 10 ~/short.txt


                                  If you want the lines in the order that they are found in the original file:



                                  cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-


                                  This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.






                                  share|improve this answer



























                                    0














                                    Instead of trying to go the long way, creating a sed script to extract the lines from your file, just use shuf directly on the file:



                                    shuf -n 10 ~/short.txt


                                    If you want the lines in the order that they are found in the original file:



                                    cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-


                                    This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.






                                    share|improve this answer

























                                      0












                                      0








                                      0






                                      Instead of trying to go the long way, creating a sed script to extract the lines from your file, just use shuf directly on the file:



                                      shuf -n 10 ~/short.txt


                                      If you want the lines in the order that they are found in the original file:



                                      cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-


                                      This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.






                                      share|improve this answer














                                      Instead of trying to go the long way, creating a sed script to extract the lines from your file, just use shuf directly on the file:



                                      shuf -n 10 ~/short.txt


                                      If you want the lines in the order that they are found in the original file:



                                      cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-


                                      This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Dec 18 at 19:34

























                                      answered Aug 1 at 11:20









                                      Kusalananda

                                      121k16229372




                                      121k16229372





















                                          0














                                          U could...



                                          shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt


                                          or, under bash



                                          sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt





                                          share|improve this answer

























                                            0














                                            U could...



                                            shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt


                                            or, under bash



                                            sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt





                                            share|improve this answer























                                              0












                                              0








                                              0






                                              U could...



                                              shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt


                                              or, under bash



                                              sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt





                                              share|improve this answer












                                              U could...



                                              shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt


                                              or, under bash



                                              sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Dec 18 at 20:59









                                              F. Hauri

                                              2,6191327




                                              2,6191327



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f205919%2fre-print-an-array-in-a-certain-format%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown






                                                  Popular posts from this blog

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

                                                  Bahrain

                                                  Postfix configuration issue with fips on centos 7; mailgun relay