Omri Omri - 1 year ago 127
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

Answer Source

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}'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download