Take a look - Minix

This is a discussion on Take a look - Minix ; Hello everyone, Could I use cat and grep commands to do the following task? If it is possible, could anyone help to show the sample please? Task: Given a file, calculate the number of lines (in this file) which is ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Take a look

  1. Take a look

    Hello everyone,


    Could I use cat and grep commands to do the following task? If it is
    possible, could anyone help to show the sample please?

    Task:

    Given a file, calculate the number of lines (in this file) which is not
    started with '#'.


  2. Re: Take a look

    Hi,
    you only need grep to do your task:

    grep -vc '#'
    or
    grep -vce '#.*'

    the option used are:
    -c Print out the matching lines count only
    -v Negate the search pattern
    -e The pattern is an extended regular expression

    Goodbye
    --
    Gaetano Sferra


  3. Re: Take a look

    That will only count the lines that don't contain # at all. If you want
    to count all the lines that don't START with #, you will need to
    specify the beginning of the line (use the ^ character) in your grep
    pattern.

    For example, if the file looks like this:
    # line 1
    line 2 #
    # line 3

    Then:
    grep -vc "#" will be 0, since it excludes all three lines (they all
    contain #).
    grep -vc "^#" will be 2, since it only excludes the line 1 (only line 1
    has # as the first character of the line).
    grep -vc "^[[:blank:]]*#" will be 1, since it excludes both line 1 and
    line 3 (both lines have # as the first non-whitespace character on the
    line)

    gsferra@hotmail.com wrote:
    > Hi,
    > you only need grep to do your task:
    >
    > grep -vc '#'
    > or
    > grep -vce '#.*'
    >
    > the option used are:
    > -c Print out the matching lines count only
    > -v Negate the search pattern
    > -e The pattern is an extended regular expression
    >
    > Goodbye
    > --
    > Gaetano Sferra



  4. Re: Take a look

    I agree with Jeshua, I forgot the "^" in the pattern


+ Reply to Thread