Search a Keyword and get its count occurrence











up vote
0
down vote

favorite












I want to find out the occurrence of a keyword group by filename inside a directory.




Example: Keyword - TEST (means TEST1, TEST2 etc can be valid results) Files - a.txt, b.txt etc. (inside a directory .. /tmp). Output will be as follows.




a.txt - 4
b.txt - 5
c.txt - 0


I tried with grep -o "TEST" a.txt and this gives only TEST and its occurrence like if a file is having 3 occurrence (like TEST1, TEST_XXX, TESTXYES) the output will be



TEST
TEST
TEST


OR If multiple files search is not possible than OUTPUT should provide all the similar string starting TEST
example



TEST
TEST_1
TEST_2
TEST4


I tried following too.



grep -c "TEST" a.txt


this gives on the count, it does not gives what was the entire text starting with TEST










share|improve this question
























  • For keyword TEST does it count if it's found in RETESTS? What about in Testament?
    – roaima
    8 hours ago








  • 1




    I modified your question, tried making it readable, but you should really edit the question by yourself because it is still hard to understand what exactly you are asking for.
    – jimmij
    8 hours ago

















up vote
0
down vote

favorite












I want to find out the occurrence of a keyword group by filename inside a directory.




Example: Keyword - TEST (means TEST1, TEST2 etc can be valid results) Files - a.txt, b.txt etc. (inside a directory .. /tmp). Output will be as follows.




a.txt - 4
b.txt - 5
c.txt - 0


I tried with grep -o "TEST" a.txt and this gives only TEST and its occurrence like if a file is having 3 occurrence (like TEST1, TEST_XXX, TESTXYES) the output will be



TEST
TEST
TEST


OR If multiple files search is not possible than OUTPUT should provide all the similar string starting TEST
example



TEST
TEST_1
TEST_2
TEST4


I tried following too.



grep -c "TEST" a.txt


this gives on the count, it does not gives what was the entire text starting with TEST










share|improve this question
























  • For keyword TEST does it count if it's found in RETESTS? What about in Testament?
    – roaima
    8 hours ago








  • 1




    I modified your question, tried making it readable, but you should really edit the question by yourself because it is still hard to understand what exactly you are asking for.
    – jimmij
    8 hours ago















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to find out the occurrence of a keyword group by filename inside a directory.




Example: Keyword - TEST (means TEST1, TEST2 etc can be valid results) Files - a.txt, b.txt etc. (inside a directory .. /tmp). Output will be as follows.




a.txt - 4
b.txt - 5
c.txt - 0


I tried with grep -o "TEST" a.txt and this gives only TEST and its occurrence like if a file is having 3 occurrence (like TEST1, TEST_XXX, TESTXYES) the output will be



TEST
TEST
TEST


OR If multiple files search is not possible than OUTPUT should provide all the similar string starting TEST
example



TEST
TEST_1
TEST_2
TEST4


I tried following too.



grep -c "TEST" a.txt


this gives on the count, it does not gives what was the entire text starting with TEST










share|improve this question















I want to find out the occurrence of a keyword group by filename inside a directory.




Example: Keyword - TEST (means TEST1, TEST2 etc can be valid results) Files - a.txt, b.txt etc. (inside a directory .. /tmp). Output will be as follows.




a.txt - 4
b.txt - 5
c.txt - 0


I tried with grep -o "TEST" a.txt and this gives only TEST and its occurrence like if a file is having 3 occurrence (like TEST1, TEST_XXX, TESTXYES) the output will be



TEST
TEST
TEST


OR If multiple files search is not possible than OUTPUT should provide all the similar string starting TEST
example



TEST
TEST_1
TEST_2
TEST4


I tried following too.



grep -c "TEST" a.txt


this gives on the count, it does not gives what was the entire text starting with TEST







text-processing grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago









GAD3R

24.2k1748102




24.2k1748102










asked 8 hours ago









user249634

6




6












  • For keyword TEST does it count if it's found in RETESTS? What about in Testament?
    – roaima
    8 hours ago








  • 1




    I modified your question, tried making it readable, but you should really edit the question by yourself because it is still hard to understand what exactly you are asking for.
    – jimmij
    8 hours ago




















  • For keyword TEST does it count if it's found in RETESTS? What about in Testament?
    – roaima
    8 hours ago








  • 1




    I modified your question, tried making it readable, but you should really edit the question by yourself because it is still hard to understand what exactly you are asking for.
    – jimmij
    8 hours ago


















For keyword TEST does it count if it's found in RETESTS? What about in Testament?
– roaima
8 hours ago






For keyword TEST does it count if it's found in RETESTS? What about in Testament?
– roaima
8 hours ago






1




1




I modified your question, tried making it readable, but you should really edit the question by yourself because it is still hard to understand what exactly you are asking for.
– jimmij
8 hours ago






I modified your question, tried making it readable, but you should really edit the question by yourself because it is still hard to understand what exactly you are asking for.
– jimmij
8 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The grep -c prints number of matching lines, so you won't get right result in combination with -o option. I would pipe the output of grep -o to uniq -c:



$ echo 'test1 test2 test3' >/tmp/file1
$ echo 'test1 test2 test3 test4' >/tmp/file2
$ grep -o test /tmp/file* | uniq -c
3 /tmp/file1:test
4 /tmp/file2:test


You can pipe it further to modify the output with some text-processing tools.






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: 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%2f481627%2fsearch-a-keyword-and-get-its-count-occurrence%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The grep -c prints number of matching lines, so you won't get right result in combination with -o option. I would pipe the output of grep -o to uniq -c:



    $ echo 'test1 test2 test3' >/tmp/file1
    $ echo 'test1 test2 test3 test4' >/tmp/file2
    $ grep -o test /tmp/file* | uniq -c
    3 /tmp/file1:test
    4 /tmp/file2:test


    You can pipe it further to modify the output with some text-processing tools.






    share|improve this answer



























      up vote
      0
      down vote













      The grep -c prints number of matching lines, so you won't get right result in combination with -o option. I would pipe the output of grep -o to uniq -c:



      $ echo 'test1 test2 test3' >/tmp/file1
      $ echo 'test1 test2 test3 test4' >/tmp/file2
      $ grep -o test /tmp/file* | uniq -c
      3 /tmp/file1:test
      4 /tmp/file2:test


      You can pipe it further to modify the output with some text-processing tools.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        The grep -c prints number of matching lines, so you won't get right result in combination with -o option. I would pipe the output of grep -o to uniq -c:



        $ echo 'test1 test2 test3' >/tmp/file1
        $ echo 'test1 test2 test3 test4' >/tmp/file2
        $ grep -o test /tmp/file* | uniq -c
        3 /tmp/file1:test
        4 /tmp/file2:test


        You can pipe it further to modify the output with some text-processing tools.






        share|improve this answer














        The grep -c prints number of matching lines, so you won't get right result in combination with -o option. I would pipe the output of grep -o to uniq -c:



        $ echo 'test1 test2 test3' >/tmp/file1
        $ echo 'test1 test2 test3 test4' >/tmp/file2
        $ grep -o test /tmp/file* | uniq -c
        3 /tmp/file1:test
        4 /tmp/file2:test


        You can pipe it further to modify the output with some text-processing tools.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 8 hours ago

























        answered 8 hours ago









        jimmij

        30.2k867102




        30.2k867102






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481627%2fsearch-a-keyword-and-get-its-count-occurrence%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre