blacktrance
blacktrance

Reputation: 1052

from Linux command line, find number of lines in which a string occurs

I have a file in the location /home/someuser/sometext.txt . I want to count the number of lines in which a particular string occurs. What's the way to do that from Linux command line?

Upvotes: 1

Views: 1562

Answers (2)

anubhava
anubhava

Reputation: 785108

grep with -c switch is what you need:

grep -c "pattern" /home/someuser/sometext.txt

Alternate solution using awk:

awk '/regex/{c++}END{print c+0}' /home/someuser/sometext.txt

Upvotes: 2

Ryan Endacott
Ryan Endacott

Reputation: 9172

You're looking for the grep command. Here's a basic tutorial. It's extremely useful for string searching in files. It also has support for regular expressions.

It looks like you'll do something like this:

grep -c "mystring" /home/someuser/sometext.txt

The -c argument is short for --count and tells grep to print out the number of lines that contain the string.

Upvotes: 1

Related Questions