How do I grep file paths out of a text file?

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











up vote
1
down vote

favorite
2












I have a file that has a list of paths like so:



"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"


Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?










share|improve this question























  • you can use sed -r 's/.*"(.*)"$/1/' as answered to your newly duplicated question.
    – Î±Ò“sнιη
    Sep 7 at 4:10















up vote
1
down vote

favorite
2












I have a file that has a list of paths like so:



"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"


Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?










share|improve this question























  • you can use sed -r 's/.*"(.*)"$/1/' as answered to your newly duplicated question.
    – Î±Ò“sнιη
    Sep 7 at 4:10













up vote
1
down vote

favorite
2









up vote
1
down vote

favorite
2






2





I have a file that has a list of paths like so:



"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"


Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?










share|improve this question















I have a file that has a list of paths like so:



"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"


Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?







text-processing sed grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 7 at 4:12









αғsнιη

15.8k92563




15.8k92563










asked Sep 7 at 0:10









James Heald

126115




126115











  • you can use sed -r 's/.*"(.*)"$/1/' as answered to your newly duplicated question.
    – Î±Ò“sнιη
    Sep 7 at 4:10

















  • you can use sed -r 's/.*"(.*)"$/1/' as answered to your newly duplicated question.
    – Î±Ò“sнιη
    Sep 7 at 4:10
















you can use sed -r 's/.*"(.*)"$/1/' as answered to your newly duplicated question.
– Î±Ò“sнιη
Sep 7 at 4:10





you can use sed -r 's/.*"(.*)"$/1/' as answered to your newly duplicated question.
– Î±Ò“sнιη
Sep 7 at 4:10











4 Answers
4






active

oldest

votes

















up vote
6
down vote



accepted










If all the paths start with /, you could just match / followed by a sequence of non-" characters:



$ grep -o '/[^"]*' file
/user/bin/share
/home/user/.local
/root/


Alternatively, for a more structured approach, use awk to strip quotes from and print just the second field:



awk 'gsub(/"/,"",$2); print $2' file





share|improve this answer




















  • awk solution would not work if there was a whitespaces in path
    – Î±Ò“sнιη
    Sep 7 at 4:08

















up vote
1
down vote













Assuming that the paths do not contain newline characters,



$ sed 's/^.*[[:blank:]]"//; s/"$//' <file
/user/bin/share
/home/user/.local
/root/


The sed code first removes everything on each line up to and including the first " character preceded by a blank (space or tab). It then removes the " at the end.



This allows the paths to contain spaces and embedded " characters, but not the combination blank+".






share|improve this answer



























    up vote
    1
    down vote













    Why wouldn't a simple



    awk -F" 'print $4' file
    /user/bin/share
    /home/user/.local
    /root/


    work?






    share|improve this answer



























      up vote
      1
      down vote













      Simple approach. Pluck out field #4, with quotes as delimiters, using cut.



      $ cut -f4 -d" file
      /user/bin/share
      /home/user/.local
      /root/
      $





      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%2f467412%2fhow-do-i-grep-file-paths-out-of-a-text-file%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        6
        down vote



        accepted










        If all the paths start with /, you could just match / followed by a sequence of non-" characters:



        $ grep -o '/[^"]*' file
        /user/bin/share
        /home/user/.local
        /root/


        Alternatively, for a more structured approach, use awk to strip quotes from and print just the second field:



        awk 'gsub(/"/,"",$2); print $2' file





        share|improve this answer




















        • awk solution would not work if there was a whitespaces in path
          – Î±Ò“sнιη
          Sep 7 at 4:08














        up vote
        6
        down vote



        accepted










        If all the paths start with /, you could just match / followed by a sequence of non-" characters:



        $ grep -o '/[^"]*' file
        /user/bin/share
        /home/user/.local
        /root/


        Alternatively, for a more structured approach, use awk to strip quotes from and print just the second field:



        awk 'gsub(/"/,"",$2); print $2' file





        share|improve this answer




















        • awk solution would not work if there was a whitespaces in path
          – Î±Ò“sнιη
          Sep 7 at 4:08












        up vote
        6
        down vote



        accepted







        up vote
        6
        down vote



        accepted






        If all the paths start with /, you could just match / followed by a sequence of non-" characters:



        $ grep -o '/[^"]*' file
        /user/bin/share
        /home/user/.local
        /root/


        Alternatively, for a more structured approach, use awk to strip quotes from and print just the second field:



        awk 'gsub(/"/,"",$2); print $2' file





        share|improve this answer












        If all the paths start with /, you could just match / followed by a sequence of non-" characters:



        $ grep -o '/[^"]*' file
        /user/bin/share
        /home/user/.local
        /root/


        Alternatively, for a more structured approach, use awk to strip quotes from and print just the second field:



        awk 'gsub(/"/,"",$2); print $2' file






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 7 at 0:21









        steeldriver

        32.2k34979




        32.2k34979











        • awk solution would not work if there was a whitespaces in path
          – Î±Ò“sнιη
          Sep 7 at 4:08
















        • awk solution would not work if there was a whitespaces in path
          – Î±Ò“sнιη
          Sep 7 at 4:08















        awk solution would not work if there was a whitespaces in path
        – Î±Ò“sнιη
        Sep 7 at 4:08




        awk solution would not work if there was a whitespaces in path
        – Î±Ò“sнιη
        Sep 7 at 4:08












        up vote
        1
        down vote













        Assuming that the paths do not contain newline characters,



        $ sed 's/^.*[[:blank:]]"//; s/"$//' <file
        /user/bin/share
        /home/user/.local
        /root/


        The sed code first removes everything on each line up to and including the first " character preceded by a blank (space or tab). It then removes the " at the end.



        This allows the paths to contain spaces and embedded " characters, but not the combination blank+".






        share|improve this answer
























          up vote
          1
          down vote













          Assuming that the paths do not contain newline characters,



          $ sed 's/^.*[[:blank:]]"//; s/"$//' <file
          /user/bin/share
          /home/user/.local
          /root/


          The sed code first removes everything on each line up to and including the first " character preceded by a blank (space or tab). It then removes the " at the end.



          This allows the paths to contain spaces and embedded " characters, but not the combination blank+".






          share|improve this answer






















            up vote
            1
            down vote










            up vote
            1
            down vote









            Assuming that the paths do not contain newline characters,



            $ sed 's/^.*[[:blank:]]"//; s/"$//' <file
            /user/bin/share
            /home/user/.local
            /root/


            The sed code first removes everything on each line up to and including the first " character preceded by a blank (space or tab). It then removes the " at the end.



            This allows the paths to contain spaces and embedded " characters, but not the combination blank+".






            share|improve this answer












            Assuming that the paths do not contain newline characters,



            $ sed 's/^.*[[:blank:]]"//; s/"$//' <file
            /user/bin/share
            /home/user/.local
            /root/


            The sed code first removes everything on each line up to and including the first " character preceded by a blank (space or tab). It then removes the " at the end.



            This allows the paths to contain spaces and embedded " characters, but not the combination blank+".







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 7 at 5:53









            Kusalananda

            106k14209329




            106k14209329




















                up vote
                1
                down vote













                Why wouldn't a simple



                awk -F" 'print $4' file
                /user/bin/share
                /home/user/.local
                /root/


                work?






                share|improve this answer
























                  up vote
                  1
                  down vote













                  Why wouldn't a simple



                  awk -F" 'print $4' file
                  /user/bin/share
                  /home/user/.local
                  /root/


                  work?






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Why wouldn't a simple



                    awk -F" 'print $4' file
                    /user/bin/share
                    /home/user/.local
                    /root/


                    work?






                    share|improve this answer












                    Why wouldn't a simple



                    awk -F" 'print $4' file
                    /user/bin/share
                    /home/user/.local
                    /root/


                    work?







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 7 at 8:27









                    RudiC

                    1,2168




                    1,2168




















                        up vote
                        1
                        down vote













                        Simple approach. Pluck out field #4, with quotes as delimiters, using cut.



                        $ cut -f4 -d" file
                        /user/bin/share
                        /home/user/.local
                        /root/
                        $





                        share|improve this answer
























                          up vote
                          1
                          down vote













                          Simple approach. Pluck out field #4, with quotes as delimiters, using cut.



                          $ cut -f4 -d" file
                          /user/bin/share
                          /home/user/.local
                          /root/
                          $





                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Simple approach. Pluck out field #4, with quotes as delimiters, using cut.



                            $ cut -f4 -d" file
                            /user/bin/share
                            /home/user/.local
                            /root/
                            $





                            share|improve this answer












                            Simple approach. Pluck out field #4, with quotes as delimiters, using cut.



                            $ cut -f4 -d" file
                            /user/bin/share
                            /home/user/.local
                            /root/
                            $






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 7 at 8:45









                            steve

                            12.9k22149




                            12.9k22149



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467412%2fhow-do-i-grep-file-paths-out-of-a-text-file%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                這個網誌中的熱門文章

                                How to combine Bézier curves to a surface?

                                Carbon dioxide

                                Why am i infinitely getting the same tweet with the Twitter Search API?