How do I organize and cut this portion of a directory into a file?












1















My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command



This is what I thought would do the job but it doesn't



wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort


My output




1 /s/s/s/s/location/folder/folder/a.log
1 /s/s/s/s/location/folder/folder/b.log
1 /s/s/s/s/location/folder/folder/c.log
3 /s/s/s/s/location/folder/folder/d.log
2 /s/s/s/s/location/folder/folder/e.log



What I want it to be




1 a
1 b
1 c
2 e
3 d










share|improve this question





























    1















    My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command



    This is what I thought would do the job but it doesn't



    wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort


    My output




    1 /s/s/s/s/location/folder/folder/a.log
    1 /s/s/s/s/location/folder/folder/b.log
    1 /s/s/s/s/location/folder/folder/c.log
    3 /s/s/s/s/location/folder/folder/d.log
    2 /s/s/s/s/location/folder/folder/e.log



    What I want it to be




    1 a
    1 b
    1 c
    2 e
    3 d










    share|improve this question



























      1












      1








      1








      My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command



      This is what I thought would do the job but it doesn't



      wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort


      My output




      1 /s/s/s/s/location/folder/folder/a.log
      1 /s/s/s/s/location/folder/folder/b.log
      1 /s/s/s/s/location/folder/folder/c.log
      3 /s/s/s/s/location/folder/folder/d.log
      2 /s/s/s/s/location/folder/folder/e.log



      What I want it to be




      1 a
      1 b
      1 c
      2 e
      3 d










      share|improve this question
















      My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command



      This is what I thought would do the job but it doesn't



      wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort


      My output




      1 /s/s/s/s/location/folder/folder/a.log
      1 /s/s/s/s/location/folder/folder/b.log
      1 /s/s/s/s/location/folder/folder/c.log
      3 /s/s/s/s/location/folder/folder/d.log
      2 /s/s/s/s/location/folder/folder/e.log



      What I want it to be




      1 a
      1 b
      1 c
      2 e
      3 d







      shell text-processing pipe io-redirection






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 53 mins ago









      Rui F Ribeiro

      40.1k1479135




      40.1k1479135










      asked Feb 4 '16 at 18:23









      JoeJoe

      84




      84






















          3 Answers
          3






          active

          oldest

          votes


















          0














          You can strip out all of the bits you don't want by piping the output through sed:



          wc -l ~location/folder/folder/*.log > ~/log.info
          cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'





          share|improve this answer
























          • Doesn't this also remove the frequency of when they should up?

            – Joe
            Feb 4 '16 at 18:30













          • No, it preserves everything before the first / and after the last /, and then snips .log off of the end.

            – DopeGhoti
            Feb 4 '16 at 18:56



















          0














          How about:



          perl -e 'print sort map {s/(d).*(w)..*//r} <>' 


          (Needs perl 5.14+ - this is probably installed on your system already)



          Uses an inlined bit of perl code that:




          • reads <> which is STDIN or files specified on command line.

          • Uses a substitution regex which captures a digit (d) and a letter just before a . (w).

          • then returns the transformed string. r regex flag.

          • uses map to iterate STDIN.

          • and sort to ... well, sort.

          • and print to print it.


          Output:



          1 a
          1 b
          1 c
          2 e
          3 d


          Alternatively:



          perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log 





          share|improve this answer

































            0















            wc … > ~/log.info | cut ~/log.info | …



            The two sides of the pipe are executed in parallel. Unless wc is especially quick to finish and cut is especially slow to start, by the time cut reads ~/log.info, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort is also executed in parallel and also truncates the file.



            I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:



            wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info


            It would be simpler to switch to the target directory:



            { cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info





            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%2f259918%2fhow-do-i-organize-and-cut-this-portion-of-a-directory-into-a-file%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can strip out all of the bits you don't want by piping the output through sed:



              wc -l ~location/folder/folder/*.log > ~/log.info
              cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'





              share|improve this answer
























              • Doesn't this also remove the frequency of when they should up?

                – Joe
                Feb 4 '16 at 18:30













              • No, it preserves everything before the first / and after the last /, and then snips .log off of the end.

                – DopeGhoti
                Feb 4 '16 at 18:56
















              0














              You can strip out all of the bits you don't want by piping the output through sed:



              wc -l ~location/folder/folder/*.log > ~/log.info
              cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'





              share|improve this answer
























              • Doesn't this also remove the frequency of when they should up?

                – Joe
                Feb 4 '16 at 18:30













              • No, it preserves everything before the first / and after the last /, and then snips .log off of the end.

                – DopeGhoti
                Feb 4 '16 at 18:56














              0












              0








              0







              You can strip out all of the bits you don't want by piping the output through sed:



              wc -l ~location/folder/folder/*.log > ~/log.info
              cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'





              share|improve this answer













              You can strip out all of the bits you don't want by piping the output through sed:



              wc -l ~location/folder/folder/*.log > ~/log.info
              cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 4 '16 at 18:26









              DopeGhotiDopeGhoti

              45.5k55988




              45.5k55988













              • Doesn't this also remove the frequency of when they should up?

                – Joe
                Feb 4 '16 at 18:30













              • No, it preserves everything before the first / and after the last /, and then snips .log off of the end.

                – DopeGhoti
                Feb 4 '16 at 18:56



















              • Doesn't this also remove the frequency of when they should up?

                – Joe
                Feb 4 '16 at 18:30













              • No, it preserves everything before the first / and after the last /, and then snips .log off of the end.

                – DopeGhoti
                Feb 4 '16 at 18:56

















              Doesn't this also remove the frequency of when they should up?

              – Joe
              Feb 4 '16 at 18:30







              Doesn't this also remove the frequency of when they should up?

              – Joe
              Feb 4 '16 at 18:30















              No, it preserves everything before the first / and after the last /, and then snips .log off of the end.

              – DopeGhoti
              Feb 4 '16 at 18:56





              No, it preserves everything before the first / and after the last /, and then snips .log off of the end.

              – DopeGhoti
              Feb 4 '16 at 18:56













              0














              How about:



              perl -e 'print sort map {s/(d).*(w)..*//r} <>' 


              (Needs perl 5.14+ - this is probably installed on your system already)



              Uses an inlined bit of perl code that:




              • reads <> which is STDIN or files specified on command line.

              • Uses a substitution regex which captures a digit (d) and a letter just before a . (w).

              • then returns the transformed string. r regex flag.

              • uses map to iterate STDIN.

              • and sort to ... well, sort.

              • and print to print it.


              Output:



              1 a
              1 b
              1 c
              2 e
              3 d


              Alternatively:



              perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log 





              share|improve this answer






























                0














                How about:



                perl -e 'print sort map {s/(d).*(w)..*//r} <>' 


                (Needs perl 5.14+ - this is probably installed on your system already)



                Uses an inlined bit of perl code that:




                • reads <> which is STDIN or files specified on command line.

                • Uses a substitution regex which captures a digit (d) and a letter just before a . (w).

                • then returns the transformed string. r regex flag.

                • uses map to iterate STDIN.

                • and sort to ... well, sort.

                • and print to print it.


                Output:



                1 a
                1 b
                1 c
                2 e
                3 d


                Alternatively:



                perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log 





                share|improve this answer




























                  0












                  0








                  0







                  How about:



                  perl -e 'print sort map {s/(d).*(w)..*//r} <>' 


                  (Needs perl 5.14+ - this is probably installed on your system already)



                  Uses an inlined bit of perl code that:




                  • reads <> which is STDIN or files specified on command line.

                  • Uses a substitution regex which captures a digit (d) and a letter just before a . (w).

                  • then returns the transformed string. r regex flag.

                  • uses map to iterate STDIN.

                  • and sort to ... well, sort.

                  • and print to print it.


                  Output:



                  1 a
                  1 b
                  1 c
                  2 e
                  3 d


                  Alternatively:



                  perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log 





                  share|improve this answer















                  How about:



                  perl -e 'print sort map {s/(d).*(w)..*//r} <>' 


                  (Needs perl 5.14+ - this is probably installed on your system already)



                  Uses an inlined bit of perl code that:




                  • reads <> which is STDIN or files specified on command line.

                  • Uses a substitution regex which captures a digit (d) and a letter just before a . (w).

                  • then returns the transformed string. r regex flag.

                  • uses map to iterate STDIN.

                  • and sort to ... well, sort.

                  • and print to print it.


                  Output:



                  1 a
                  1 b
                  1 c
                  2 e
                  3 d


                  Alternatively:



                  perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log 






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 4 '16 at 19:46

























                  answered Feb 4 '16 at 19:31









                  SobriqueSobrique

                  3,819519




                  3,819519























                      0















                      wc … > ~/log.info | cut ~/log.info | …



                      The two sides of the pipe are executed in parallel. Unless wc is especially quick to finish and cut is especially slow to start, by the time cut reads ~/log.info, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort is also executed in parallel and also truncates the file.



                      I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:



                      wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info


                      It would be simpler to switch to the target directory:



                      { cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info





                      share|improve this answer




























                        0















                        wc … > ~/log.info | cut ~/log.info | …



                        The two sides of the pipe are executed in parallel. Unless wc is especially quick to finish and cut is especially slow to start, by the time cut reads ~/log.info, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort is also executed in parallel and also truncates the file.



                        I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:



                        wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info


                        It would be simpler to switch to the target directory:



                        { cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info





                        share|improve this answer


























                          0












                          0








                          0








                          wc … > ~/log.info | cut ~/log.info | …



                          The two sides of the pipe are executed in parallel. Unless wc is especially quick to finish and cut is especially slow to start, by the time cut reads ~/log.info, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort is also executed in parallel and also truncates the file.



                          I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:



                          wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info


                          It would be simpler to switch to the target directory:



                          { cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info





                          share|improve this answer














                          wc … > ~/log.info | cut ~/log.info | …



                          The two sides of the pipe are executed in parallel. Unless wc is especially quick to finish and cut is especially slow to start, by the time cut reads ~/log.info, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort is also executed in parallel and also truncates the file.



                          I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:



                          wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info


                          It would be simpler to switch to the target directory:



                          { cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 6 '16 at 0:57









                          GillesGilles

                          537k12810861603




                          537k12810861603






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f259918%2fhow-do-i-organize-and-cut-this-portion-of-a-directory-into-a-file%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

                              サソリ

                              広島県道265号伴広島線

                              Setup Asymptote in Texstudio