Search code examples
linuxcountgrepwc

Count the number of lines in a text file that contain a specific character (Linux)?


I have a .csv (utf-8 text file). It has millions of lines. I can count the lines with wc -l. I want to count only lines with specific characters in them. So if 2M of 10M lines had a "1" in them, I'd like to return 2M.

Is this possible? Will this be horrendously slow? What are some ways to do this?


Solution

  • This writes all lines from file.csv with a "1" to stdout:

    grep "1" file.csv
    

    With a pipe you can connect stdout (of grep) and stdin (of wc):

    grep "1" file.csv | wc -l