Process a list of filenames to split directory and basename

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











up vote
0
down vote

favorite












I have a file containing a bunch of filenames:



$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt


For each filename, I want to print the directory name and the base filename, comma-separated. For example:



/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt


How can I do that?










share|improve this question



















  • 2




    You might want to check out the basename and dirname commands
    – hhoke1
    Aug 31 at 14:50














up vote
0
down vote

favorite












I have a file containing a bunch of filenames:



$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt


For each filename, I want to print the directory name and the base filename, comma-separated. For example:



/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt


How can I do that?










share|improve this question



















  • 2




    You might want to check out the basename and dirname commands
    – hhoke1
    Aug 31 at 14:50












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file containing a bunch of filenames:



$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt


For each filename, I want to print the directory name and the base filename, comma-separated. For example:



/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt


How can I do that?










share|improve this question















I have a file containing a bunch of filenames:



$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt


For each filename, I want to print the directory name and the base filename, comma-separated. For example:



/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt


How can I do that?







centos text-processing filenames






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 31 at 16:54









Michael Mrozek♦

58.7k27184207




58.7k27184207










asked Aug 31 at 14:41









user210912

12




12







  • 2




    You might want to check out the basename and dirname commands
    – hhoke1
    Aug 31 at 14:50












  • 2




    You might want to check out the basename and dirname commands
    – hhoke1
    Aug 31 at 14:50







2




2




You might want to check out the basename and dirname commands
– hhoke1
Aug 31 at 14:50




You might want to check out the basename and dirname commands
– hhoke1
Aug 31 at 14:50










3 Answers
3






active

oldest

votes

















up vote
1
down vote













Like @hhoke1 mentioned in a comment above, you can use the basename and dirname commands:



$ basename /var/incoming/new_items.txt
new_items.txt

$ dirname /var/incoming/new_items.txt
/var/incoming





share|improve this answer




















  • Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
    – user210912
    Aug 31 at 15:00






  • 2




    @user210912 Can you tell us what you're trying to do?
    – Andy Dalton
    Aug 31 at 15:05






  • 1




    Edited the question with what I believe user210912 is looking for based on a comment thread
    – Michael Mrozek♦
    Aug 31 at 16:56

















up vote
1
down vote













Do you mean something like this ?



sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt





share|improve this answer





























    up vote
    1
    down vote













    You could loop over the file and then print the parts before and after the last slash with parameter expansion:



    while IFS= read -r line; do
    printf '%s,%sn' "$line%/*" "$line##*/"
    done < test_as1


    "$line%/*" removes the shortest match of /* from the end of the line, and "$line##*/" removes the longest match of */ from the beginning of the line.






    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%2f466057%2fprocess-a-list-of-filenames-to-split-directory-and-basename%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Like @hhoke1 mentioned in a comment above, you can use the basename and dirname commands:



      $ basename /var/incoming/new_items.txt
      new_items.txt

      $ dirname /var/incoming/new_items.txt
      /var/incoming





      share|improve this answer




















      • Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
        – user210912
        Aug 31 at 15:00






      • 2




        @user210912 Can you tell us what you're trying to do?
        – Andy Dalton
        Aug 31 at 15:05






      • 1




        Edited the question with what I believe user210912 is looking for based on a comment thread
        – Michael Mrozek♦
        Aug 31 at 16:56














      up vote
      1
      down vote













      Like @hhoke1 mentioned in a comment above, you can use the basename and dirname commands:



      $ basename /var/incoming/new_items.txt
      new_items.txt

      $ dirname /var/incoming/new_items.txt
      /var/incoming





      share|improve this answer




















      • Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
        – user210912
        Aug 31 at 15:00






      • 2




        @user210912 Can you tell us what you're trying to do?
        – Andy Dalton
        Aug 31 at 15:05






      • 1




        Edited the question with what I believe user210912 is looking for based on a comment thread
        – Michael Mrozek♦
        Aug 31 at 16:56












      up vote
      1
      down vote










      up vote
      1
      down vote









      Like @hhoke1 mentioned in a comment above, you can use the basename and dirname commands:



      $ basename /var/incoming/new_items.txt
      new_items.txt

      $ dirname /var/incoming/new_items.txt
      /var/incoming





      share|improve this answer












      Like @hhoke1 mentioned in a comment above, you can use the basename and dirname commands:



      $ basename /var/incoming/new_items.txt
      new_items.txt

      $ dirname /var/incoming/new_items.txt
      /var/incoming






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Aug 31 at 14:54









      Andy Dalton

      4,8391520




      4,8391520











      • Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
        – user210912
        Aug 31 at 15:00






      • 2




        @user210912 Can you tell us what you're trying to do?
        – Andy Dalton
        Aug 31 at 15:05






      • 1




        Edited the question with what I believe user210912 is looking for based on a comment thread
        – Michael Mrozek♦
        Aug 31 at 16:56
















      • Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
        – user210912
        Aug 31 at 15:00






      • 2




        @user210912 Can you tell us what you're trying to do?
        – Andy Dalton
        Aug 31 at 15:05






      • 1




        Edited the question with what I believe user210912 is looking for based on a comment thread
        – Michael Mrozek♦
        Aug 31 at 16:56















      Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
      – user210912
      Aug 31 at 15:00




      Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
      – user210912
      Aug 31 at 15:00




      2




      2




      @user210912 Can you tell us what you're trying to do?
      – Andy Dalton
      Aug 31 at 15:05




      @user210912 Can you tell us what you're trying to do?
      – Andy Dalton
      Aug 31 at 15:05




      1




      1




      Edited the question with what I believe user210912 is looking for based on a comment thread
      – Michael Mrozek♦
      Aug 31 at 16:56




      Edited the question with what I believe user210912 is looking for based on a comment thread
      – Michael Mrozek♦
      Aug 31 at 16:56












      up vote
      1
      down vote













      Do you mean something like this ?



      sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt





      share|improve this answer


























        up vote
        1
        down vote













        Do you mean something like this ?



        sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt





        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          Do you mean something like this ?



          sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt





          share|improve this answer














          Do you mean something like this ?



          sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 31 at 17:14









          αғsнιη

          16k92563




          16k92563










          answered Aug 31 at 14:48









          Alexander

          80413




          80413




















              up vote
              1
              down vote













              You could loop over the file and then print the parts before and after the last slash with parameter expansion:



              while IFS= read -r line; do
              printf '%s,%sn' "$line%/*" "$line##*/"
              done < test_as1


              "$line%/*" removes the shortest match of /* from the end of the line, and "$line##*/" removes the longest match of */ from the beginning of the line.






              share|improve this answer
























                up vote
                1
                down vote













                You could loop over the file and then print the parts before and after the last slash with parameter expansion:



                while IFS= read -r line; do
                printf '%s,%sn' "$line%/*" "$line##*/"
                done < test_as1


                "$line%/*" removes the shortest match of /* from the end of the line, and "$line##*/" removes the longest match of */ from the beginning of the line.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You could loop over the file and then print the parts before and after the last slash with parameter expansion:



                  while IFS= read -r line; do
                  printf '%s,%sn' "$line%/*" "$line##*/"
                  done < test_as1


                  "$line%/*" removes the shortest match of /* from the end of the line, and "$line##*/" removes the longest match of */ from the beginning of the line.






                  share|improve this answer












                  You could loop over the file and then print the parts before and after the last slash with parameter expansion:



                  while IFS= read -r line; do
                  printf '%s,%sn' "$line%/*" "$line##*/"
                  done < test_as1


                  "$line%/*" removes the shortest match of /* from the end of the line, and "$line##*/" removes the longest match of */ from the beginning of the line.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 31 at 18:17









                  Benjamin W.

                  413211




                  413211



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466057%2fprocess-a-list-of-filenames-to-split-directory-and-basename%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?