Thursday, November 18, 2021

[SOLVED] Counting num of lines containing substring within a given range of lines

Issue

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.


Solution

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


Answered By - Allan Wind