Reputation: 11
So I have a string at the beginning of a line and can find all of them. I am using ^$string to match and I have thousands of these and an error occurs on a specific line. Let's say I was trying to get to the 100th occurrence of this pattern how would I do so?
For example, I can grep ^$string and list all but I would like to find a specific one.
Upvotes: 0
Views: 925
Reputation: 24802
You can use sed
to print only a single line of your grep
's output :
grep "^$string" inputFile | sed -n '100p'
-n
has output disabled by default, 100p
prints the input to the output stream for the 100th line only.
Or as @dan mentions in the comments :
grep "^$string" inputFile | sed '100!d;q'
Upvotes: 0
Reputation: 195039
grep has -m / --max-count
option:
grep -m100 '^String' | tail -1
will give you the 100th matched line.
Note:
-m100
will make grep stop reading the input file if 100 matches are hit. It's pretty useful if you are reading a huge filetail
command is very fast since it doesn't read the content.Upvotes: 1