I'm trying to find the number of lines in a file that match a certain pattern and find the number of lines that matched. For example, if my file were
test1 type1 random1
test2 type2 bird
dog cat random
I want to find the lines that have "random" and the number of lines. Ideally, the output would be something like
test1 type1 random1
dog cat random
2
I know how to use grep to do either of these tasks individually, but if I'm working with a large file, I'd prefer to not read the file twice. I'd also like to stay away from making an additional temp file to store the results of grep.
Is there a command and/or a simple function I can write to achieve these results?