Omri Omri - 8 months ago 56
Linux Question

Count lines of CLI output in linux

Hi have the following command:

lsscsi | grep HITACHI | awk '{print $6}'

I want that the output will be the number of lines of the original output.
For example, if the original output is:


The final output will be


Basically the command wc -l can be used to count the lines in a file or pipe. However, since you want to count the number of lines after a filter has been applied I would recommend to use grep for that:

lsscsi | grep -c 'HITACHI'

-c just prints the number of matching lines.

Another thing. In your example you are using grep .. | awk. That's a useless use of grep. It should be

lsscsi | awk '/HITACHI/{print $6}'