What is the difference between “grep -c ^b example” and “grep ^b example| cat –n”?

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











up vote
-2
down vote

favorite












I'm trying to learn command line better and wondering what the difference between these two commands would be.



grep -c ^b example 


and



grep ^b example| cat –n






share|improve this question


















  • 2




    Welcome to Unix & Linux. If you want to learn the command line, the best way is to experiment by running the commands yourself using different input files. Questions should show some research effort so I'd advise checking the relevant man pages to find out the purpose of the different options. Man pages aren't the most readable if you're new to Unix, so if you have any issues understanding it, you could edit this question to make it more specific (and useful). See How to Ask and feel free to take the tour.
    – Anthony Geoghegan
    Jan 12 at 21:35











  • (-1) Not even the same commands. Two unrelated commands.
    – Isaac
    Jan 12 at 22:04










  • Did you try running the commands?
    – Sparhawk
    Jan 13 at 2:12














up vote
-2
down vote

favorite












I'm trying to learn command line better and wondering what the difference between these two commands would be.



grep -c ^b example 


and



grep ^b example| cat –n






share|improve this question


















  • 2




    Welcome to Unix & Linux. If you want to learn the command line, the best way is to experiment by running the commands yourself using different input files. Questions should show some research effort so I'd advise checking the relevant man pages to find out the purpose of the different options. Man pages aren't the most readable if you're new to Unix, so if you have any issues understanding it, you could edit this question to make it more specific (and useful). See How to Ask and feel free to take the tour.
    – Anthony Geoghegan
    Jan 12 at 21:35











  • (-1) Not even the same commands. Two unrelated commands.
    – Isaac
    Jan 12 at 22:04










  • Did you try running the commands?
    – Sparhawk
    Jan 13 at 2:12












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I'm trying to learn command line better and wondering what the difference between these two commands would be.



grep -c ^b example 


and



grep ^b example| cat –n






share|improve this question














I'm trying to learn command line better and wondering what the difference between these two commands would be.



grep -c ^b example 


and



grep ^b example| cat –n








share|improve this question













share|improve this question




share|improve this question








edited Jan 12 at 23:47









ilkkachu

49.8k674137




49.8k674137










asked Jan 12 at 20:53









learner1992

1




1







  • 2




    Welcome to Unix & Linux. If you want to learn the command line, the best way is to experiment by running the commands yourself using different input files. Questions should show some research effort so I'd advise checking the relevant man pages to find out the purpose of the different options. Man pages aren't the most readable if you're new to Unix, so if you have any issues understanding it, you could edit this question to make it more specific (and useful). See How to Ask and feel free to take the tour.
    – Anthony Geoghegan
    Jan 12 at 21:35











  • (-1) Not even the same commands. Two unrelated commands.
    – Isaac
    Jan 12 at 22:04










  • Did you try running the commands?
    – Sparhawk
    Jan 13 at 2:12












  • 2




    Welcome to Unix & Linux. If you want to learn the command line, the best way is to experiment by running the commands yourself using different input files. Questions should show some research effort so I'd advise checking the relevant man pages to find out the purpose of the different options. Man pages aren't the most readable if you're new to Unix, so if you have any issues understanding it, you could edit this question to make it more specific (and useful). See How to Ask and feel free to take the tour.
    – Anthony Geoghegan
    Jan 12 at 21:35











  • (-1) Not even the same commands. Two unrelated commands.
    – Isaac
    Jan 12 at 22:04










  • Did you try running the commands?
    – Sparhawk
    Jan 13 at 2:12







2




2




Welcome to Unix & Linux. If you want to learn the command line, the best way is to experiment by running the commands yourself using different input files. Questions should show some research effort so I'd advise checking the relevant man pages to find out the purpose of the different options. Man pages aren't the most readable if you're new to Unix, so if you have any issues understanding it, you could edit this question to make it more specific (and useful). See How to Ask and feel free to take the tour.
– Anthony Geoghegan
Jan 12 at 21:35





Welcome to Unix & Linux. If you want to learn the command line, the best way is to experiment by running the commands yourself using different input files. Questions should show some research effort so I'd advise checking the relevant man pages to find out the purpose of the different options. Man pages aren't the most readable if you're new to Unix, so if you have any issues understanding it, you could edit this question to make it more specific (and useful). See How to Ask and feel free to take the tour.
– Anthony Geoghegan
Jan 12 at 21:35













(-1) Not even the same commands. Two unrelated commands.
– Isaac
Jan 12 at 22:04




(-1) Not even the same commands. Two unrelated commands.
– Isaac
Jan 12 at 22:04












Did you try running the commands?
– Sparhawk
Jan 13 at 2:12




Did you try running the commands?
– Sparhawk
Jan 13 at 2:12










2 Answers
2






active

oldest

votes

















up vote
0
down vote













Well, according to the manual, grep:




searches for PATTERN in each FILE. By
default, grep prints the matching lines.




and with the -c flag:




-c, --count


Suppress normal output; instead print a count of matching
lines for each input file.




So, grep -c ^b example prints the number (count) of lines matching the pattern, while grep ^b example prints the lines themselves.



As for cat, it's described to




concatenate files and print on the standard output




and what the -n flag does is to:




-n, --number 


number all output lines




Given the pipe and no file names, cat reads from the pipe, so the output is the output of grep, with line numbers added. Hence grep ^b example| cat –n prints all matching lines, numbered.




This is different from grep -n ^b example, where grep adds the line numbers of the matches. grep knows the line numbers of the original file, while cat only sees the output of grep and numbers the lines accordingly.



So, given the input file



$ cat example 
bar
foo
basf


We have:



$ grep -c ^b example 
2

$ grep ^b example |cat -n
1 bar
2 basf

$ grep -n ^b example
1:bar
3:basf





share|improve this answer



























    up vote
    0
    down vote













    Ok command:



    1. grep -c ^b example: Output the count for matching lines for lines that begin with letter b, and


    2. grep ^b example | cat -n: pass the result of the grep command to the cat command and list them with line numbers. The -n forces cat to list them along with lines numbers.


    Example:



    example.txt with content:



    media sound3
    media sound1
    media sound2
    find sound -type f -name sound[0-9] -printf 'media %fn' > file.txt
    find sound -type f -name sound[0-9] -exec bash -c 'echo media bash >> file.txt' ;
    find sound -type f -name sound[0-9] -exec bash -c "f=''; echo media $( basename $f) >> file.txt" ;


    Result from (1):



    3


    Result from (2):



    1 media sound3
    2 media sound1
    3 media sound2





    share|improve this answer






















    • Yes your right!
      – George Udosen
      Jan 12 at 23:56










    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%2f416674%2fwhat-is-the-difference-between-grep-c-b-example-and-grep-b-example-cat-n%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Well, according to the manual, grep:




    searches for PATTERN in each FILE. By
    default, grep prints the matching lines.




    and with the -c flag:




    -c, --count


    Suppress normal output; instead print a count of matching
    lines for each input file.




    So, grep -c ^b example prints the number (count) of lines matching the pattern, while grep ^b example prints the lines themselves.



    As for cat, it's described to




    concatenate files and print on the standard output




    and what the -n flag does is to:




    -n, --number 


    number all output lines




    Given the pipe and no file names, cat reads from the pipe, so the output is the output of grep, with line numbers added. Hence grep ^b example| cat –n prints all matching lines, numbered.




    This is different from grep -n ^b example, where grep adds the line numbers of the matches. grep knows the line numbers of the original file, while cat only sees the output of grep and numbers the lines accordingly.



    So, given the input file



    $ cat example 
    bar
    foo
    basf


    We have:



    $ grep -c ^b example 
    2

    $ grep ^b example |cat -n
    1 bar
    2 basf

    $ grep -n ^b example
    1:bar
    3:basf





    share|improve this answer
























      up vote
      0
      down vote













      Well, according to the manual, grep:




      searches for PATTERN in each FILE. By
      default, grep prints the matching lines.




      and with the -c flag:




      -c, --count


      Suppress normal output; instead print a count of matching
      lines for each input file.




      So, grep -c ^b example prints the number (count) of lines matching the pattern, while grep ^b example prints the lines themselves.



      As for cat, it's described to




      concatenate files and print on the standard output




      and what the -n flag does is to:




      -n, --number 


      number all output lines




      Given the pipe and no file names, cat reads from the pipe, so the output is the output of grep, with line numbers added. Hence grep ^b example| cat –n prints all matching lines, numbered.




      This is different from grep -n ^b example, where grep adds the line numbers of the matches. grep knows the line numbers of the original file, while cat only sees the output of grep and numbers the lines accordingly.



      So, given the input file



      $ cat example 
      bar
      foo
      basf


      We have:



      $ grep -c ^b example 
      2

      $ grep ^b example |cat -n
      1 bar
      2 basf

      $ grep -n ^b example
      1:bar
      3:basf





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Well, according to the manual, grep:




        searches for PATTERN in each FILE. By
        default, grep prints the matching lines.




        and with the -c flag:




        -c, --count


        Suppress normal output; instead print a count of matching
        lines for each input file.




        So, grep -c ^b example prints the number (count) of lines matching the pattern, while grep ^b example prints the lines themselves.



        As for cat, it's described to




        concatenate files and print on the standard output




        and what the -n flag does is to:




        -n, --number 


        number all output lines




        Given the pipe and no file names, cat reads from the pipe, so the output is the output of grep, with line numbers added. Hence grep ^b example| cat –n prints all matching lines, numbered.




        This is different from grep -n ^b example, where grep adds the line numbers of the matches. grep knows the line numbers of the original file, while cat only sees the output of grep and numbers the lines accordingly.



        So, given the input file



        $ cat example 
        bar
        foo
        basf


        We have:



        $ grep -c ^b example 
        2

        $ grep ^b example |cat -n
        1 bar
        2 basf

        $ grep -n ^b example
        1:bar
        3:basf





        share|improve this answer












        Well, according to the manual, grep:




        searches for PATTERN in each FILE. By
        default, grep prints the matching lines.




        and with the -c flag:




        -c, --count


        Suppress normal output; instead print a count of matching
        lines for each input file.




        So, grep -c ^b example prints the number (count) of lines matching the pattern, while grep ^b example prints the lines themselves.



        As for cat, it's described to




        concatenate files and print on the standard output




        and what the -n flag does is to:




        -n, --number 


        number all output lines




        Given the pipe and no file names, cat reads from the pipe, so the output is the output of grep, with line numbers added. Hence grep ^b example| cat –n prints all matching lines, numbered.




        This is different from grep -n ^b example, where grep adds the line numbers of the matches. grep knows the line numbers of the original file, while cat only sees the output of grep and numbers the lines accordingly.



        So, given the input file



        $ cat example 
        bar
        foo
        basf


        We have:



        $ grep -c ^b example 
        2

        $ grep ^b example |cat -n
        1 bar
        2 basf

        $ grep -n ^b example
        1:bar
        3:basf






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 12 at 23:54









        ilkkachu

        49.8k674137




        49.8k674137






















            up vote
            0
            down vote













            Ok command:



            1. grep -c ^b example: Output the count for matching lines for lines that begin with letter b, and


            2. grep ^b example | cat -n: pass the result of the grep command to the cat command and list them with line numbers. The -n forces cat to list them along with lines numbers.


            Example:



            example.txt with content:



            media sound3
            media sound1
            media sound2
            find sound -type f -name sound[0-9] -printf 'media %fn' > file.txt
            find sound -type f -name sound[0-9] -exec bash -c 'echo media bash >> file.txt' ;
            find sound -type f -name sound[0-9] -exec bash -c "f=''; echo media $( basename $f) >> file.txt" ;


            Result from (1):



            3


            Result from (2):



            1 media sound3
            2 media sound1
            3 media sound2





            share|improve this answer






















            • Yes your right!
              – George Udosen
              Jan 12 at 23:56














            up vote
            0
            down vote













            Ok command:



            1. grep -c ^b example: Output the count for matching lines for lines that begin with letter b, and


            2. grep ^b example | cat -n: pass the result of the grep command to the cat command and list them with line numbers. The -n forces cat to list them along with lines numbers.


            Example:



            example.txt with content:



            media sound3
            media sound1
            media sound2
            find sound -type f -name sound[0-9] -printf 'media %fn' > file.txt
            find sound -type f -name sound[0-9] -exec bash -c 'echo media bash >> file.txt' ;
            find sound -type f -name sound[0-9] -exec bash -c "f=''; echo media $( basename $f) >> file.txt" ;


            Result from (1):



            3


            Result from (2):



            1 media sound3
            2 media sound1
            3 media sound2





            share|improve this answer






















            • Yes your right!
              – George Udosen
              Jan 12 at 23:56












            up vote
            0
            down vote










            up vote
            0
            down vote









            Ok command:



            1. grep -c ^b example: Output the count for matching lines for lines that begin with letter b, and


            2. grep ^b example | cat -n: pass the result of the grep command to the cat command and list them with line numbers. The -n forces cat to list them along with lines numbers.


            Example:



            example.txt with content:



            media sound3
            media sound1
            media sound2
            find sound -type f -name sound[0-9] -printf 'media %fn' > file.txt
            find sound -type f -name sound[0-9] -exec bash -c 'echo media bash >> file.txt' ;
            find sound -type f -name sound[0-9] -exec bash -c "f=''; echo media $( basename $f) >> file.txt" ;


            Result from (1):



            3


            Result from (2):



            1 media sound3
            2 media sound1
            3 media sound2





            share|improve this answer














            Ok command:



            1. grep -c ^b example: Output the count for matching lines for lines that begin with letter b, and


            2. grep ^b example | cat -n: pass the result of the grep command to the cat command and list them with line numbers. The -n forces cat to list them along with lines numbers.


            Example:



            example.txt with content:



            media sound3
            media sound1
            media sound2
            find sound -type f -name sound[0-9] -printf 'media %fn' > file.txt
            find sound -type f -name sound[0-9] -exec bash -c 'echo media bash >> file.txt' ;
            find sound -type f -name sound[0-9] -exec bash -c "f=''; echo media $( basename $f) >> file.txt" ;


            Result from (1):



            3


            Result from (2):



            1 media sound3
            2 media sound1
            3 media sound2






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 12 at 23:56

























            answered Jan 12 at 21:14









            George Udosen

            1,112318




            1,112318











            • Yes your right!
              – George Udosen
              Jan 12 at 23:56
















            • Yes your right!
              – George Udosen
              Jan 12 at 23:56















            Yes your right!
            – George Udosen
            Jan 12 at 23:56




            Yes your right!
            – George Udosen
            Jan 12 at 23:56












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f416674%2fwhat-is-the-difference-between-grep-c-b-example-and-grep-b-example-cat-n%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?