Counting num of lines containing substring within a given range of lines

Viewed 77

I am trying to find the number of lines that contain a substring in a file over a given range of lines. E.g. last 100 lines, line 1 to line 5, etc.

Currently for a range I am trying to do:

sed -n '1,5p' $1 >> file.csv grep -c *"substring"* file.csv

Here $1 is the original file. I am creating a temp file and storing them in there. Is there a better way to do this? As I am not sure if this is working correctly.

As for the last n lines, I am doing:

(tail -n 100 $1) | grep -ic *"substring"* This does not seem to be working.

Wondering where I am going wrong? And how would I go about modifying this for case sensitive/insensitive scenarios.

3 Answers

You can use a pipe instead of the temporary file. The '*' outside the "" would match on files in the current directory, and that is probably not want you want, and as grep already would match within a string just do:

sed -n '1,5p' "$1" | grep -c "substring"

Alternatively, use sed to filter the data and wc -l to count lines:

sed -n '1,5 { /substring/p }' "$1" | wc-l

Regarding your first question, I don't think that you need to create a temp file and just pipe the results to grep:

sed -n '1,5p' $1 | grep -c "substring"

The second issue, which you can see in the answer above, is that you should not have the astericks in the grep substring specification. That is not necessary because grep always matches the substring anywhere in the line, and having them where you put them makes cause bash globbing.

Use this Perl one-liner to count the number of occurrences of pattern substring in file $1, lines 1-5:

perl -ne 'print if $. >= 1 && $. <= 5 && /substring/' "$1" | wc -l

For case-insensitive search, add the i modifier to the regex:

perl -ne 'print if $. >= 1 && $. <= 5 && /substring/i' "$1" | wc -l

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-n : Loop over the input one line at a time, assigning it to $_ by default.

$. : Current input line number.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)

Related