Number of intergers in a file using Command Line Interface

后端 未结 3 1935
感动是毒
感动是毒 2021-01-14 06:14

How to count number of integers in a file using egrep?

I tried to solve it as a pattern finding problem. Actually, I am facing problem of how to represent range of c

3条回答
  •  忘掉有多难
    2021-01-14 06:31

    Based on the observation that you want 66.88 excluded, I'm guessing

    grep -Ec '[0-9]\.?( |$)' file
    

    which finds a digit, optionally followed by a dot, followed by either a space or end of line.

    The -c option says to report the number of lines which contain a match (so not strictly the number of matches, if there are lines which contain multiple matches) and the -E option enables extended regular expression syntax, i.e. what was traditionally calned egrep (though the command name is now obsolescent).

    If you need to count matches, the -o option prints each match on a separate line, which you can then pass to wc -l (or in lucky cases combine with grep -c, but check first; this doesn't work e.g. with GNU grep currently).

提交回复
热议问题