count number of lines in terminal output

后端 未结 4 1065
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 19:46

couldn\'t find this on SO. I ran the following command in the terminal:

>> grep -Rl \"curl\" ./

and this displays the list of files wher

4条回答
  •  [愿得一人]
    2021-01-29 20:34

    Piping to 'wc' could be better IF the last line ends with a newline (I know that in this case, it will)
    However, if the last line does not end with a newline 'wc -l' gives back a false result.

    For example:

    $ echo "asd" | wc -l
    

    Will return 1 and

    $ echo -n "asd" | wc -l
    

    Will return 0


    So what I often use is grep -c

    $ echo "asd" | grep "^.*$" -c
    1
    
    $ echo -n "asd" | grep "^.*$" -c
    1
    

    This is closer to reality than what wc -l will return.

提交回复
热议问题