1

I was reading Count number of lines of output from previous program and found it helpful. However, while

$ grep -i [pattern, file] | tee >(wc -l) 

gives me a nice output of the grep lines and the number of matches, I want to ask if anyone knows how to save the number of matches to a variable in my script. I want to output it within a string with more readable formatting.

1
somevar=$(wc -l <(grep -i [pattern, file]) | awk '{print $1}')

Then later on

echo "This was the numbers reported: $somevar"

If you want to keep the output of grep in a file and still do the count:

$ wc -l <(grep -i [pattern, file] | tee somefile) | awk '{print $1}'

$ wc -l somefile
 364 somefile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.