How to extract filenames from a chunk of code












0














I want to extract all the filenames which are coming in a piece of code like



a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)


Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth










share|improve this question


















  • 1




    what have you tried so far?
    – oguzismail
    Nov 23 '18 at 5:38










  • There are many valid filenames in your sample data: a, cut, /etc/passwd, sort, uniq, awk, print in the first line alone. You will need to be more specific about what you want to extract.
    – Nick
    Nov 23 '18 at 5:48










  • Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
    – Cyrus
    Nov 23 '18 at 5:48










  • Please take a look at: What should I do when someone answers my question?
    – Cyrus
    Nov 23 '18 at 6:03
















0














I want to extract all the filenames which are coming in a piece of code like



a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)


Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth










share|improve this question


















  • 1




    what have you tried so far?
    – oguzismail
    Nov 23 '18 at 5:38










  • There are many valid filenames in your sample data: a, cut, /etc/passwd, sort, uniq, awk, print in the first line alone. You will need to be more specific about what you want to extract.
    – Nick
    Nov 23 '18 at 5:48










  • Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
    – Cyrus
    Nov 23 '18 at 5:48










  • Please take a look at: What should I do when someone answers my question?
    – Cyrus
    Nov 23 '18 at 6:03














0












0








0







I want to extract all the filenames which are coming in a piece of code like



a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)


Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth










share|improve this question













I want to extract all the filenames which are coming in a piece of code like



a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)


Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth







regex linux bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 5:32









Fullmetal

435




435








  • 1




    what have you tried so far?
    – oguzismail
    Nov 23 '18 at 5:38










  • There are many valid filenames in your sample data: a, cut, /etc/passwd, sort, uniq, awk, print in the first line alone. You will need to be more specific about what you want to extract.
    – Nick
    Nov 23 '18 at 5:48










  • Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
    – Cyrus
    Nov 23 '18 at 5:48










  • Please take a look at: What should I do when someone answers my question?
    – Cyrus
    Nov 23 '18 at 6:03














  • 1




    what have you tried so far?
    – oguzismail
    Nov 23 '18 at 5:38










  • There are many valid filenames in your sample data: a, cut, /etc/passwd, sort, uniq, awk, print in the first line alone. You will need to be more specific about what you want to extract.
    – Nick
    Nov 23 '18 at 5:48










  • Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
    – Cyrus
    Nov 23 '18 at 5:48










  • Please take a look at: What should I do when someone answers my question?
    – Cyrus
    Nov 23 '18 at 6:03








1




1




what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38




what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38












There are many valid filenames in your sample data: a, cut, /etc/passwd, sort, uniq, awk, print in the first line alone. You will need to be more specific about what you want to extract.
– Nick
Nov 23 '18 at 5:48




There are many valid filenames in your sample data: a, cut, /etc/passwd, sort, uniq, awk, print in the first line alone. You will need to be more specific about what you want to extract.
– Nick
Nov 23 '18 at 5:48












Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48




Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48












Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03




Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03












1 Answer
1






active

oldest

votes


















0














Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,



/[w+./-]+(?= |$)


Demo



You may put your text in a file say myfile and then run this command,



cat myfile|grep -oP '/[w+./-]+(?= |$)'


OR



grep -oP '/[w+./-]+(?= |$)' myfile


This prints following output as the way you want,



/etc/passwd
/etc/issue
/etc/pam.d/system-auth





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53441104%2fhow-to-extract-filenames-from-a-chunk-of-code%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,



    /[w+./-]+(?= |$)


    Demo



    You may put your text in a file say myfile and then run this command,



    cat myfile|grep -oP '/[w+./-]+(?= |$)'


    OR



    grep -oP '/[w+./-]+(?= |$)' myfile


    This prints following output as the way you want,



    /etc/passwd
    /etc/issue
    /etc/pam.d/system-auth





    share|improve this answer




























      0














      Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,



      /[w+./-]+(?= |$)


      Demo



      You may put your text in a file say myfile and then run this command,



      cat myfile|grep -oP '/[w+./-]+(?= |$)'


      OR



      grep -oP '/[w+./-]+(?= |$)' myfile


      This prints following output as the way you want,



      /etc/passwd
      /etc/issue
      /etc/pam.d/system-auth





      share|improve this answer


























        0












        0








        0






        Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,



        /[w+./-]+(?= |$)


        Demo



        You may put your text in a file say myfile and then run this command,



        cat myfile|grep -oP '/[w+./-]+(?= |$)'


        OR



        grep -oP '/[w+./-]+(?= |$)' myfile


        This prints following output as the way you want,



        /etc/passwd
        /etc/issue
        /etc/pam.d/system-auth





        share|improve this answer














        Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,



        /[w+./-]+(?= |$)


        Demo



        You may put your text in a file say myfile and then run this command,



        cat myfile|grep -oP '/[w+./-]+(?= |$)'


        OR



        grep -oP '/[w+./-]+(?= |$)' myfile


        This prints following output as the way you want,



        /etc/passwd
        /etc/issue
        /etc/pam.d/system-auth






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 10:17

























        answered Nov 23 '18 at 9:37









        Pushpesh Kumar Rajwanshi

        5,4322827




        5,4322827






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53441104%2fhow-to-extract-filenames-from-a-chunk-of-code%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

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...