3

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:

/dev/sda
/dev/sdb
/dev/sdc

The final output will be 3.

Omri
  • 1,436
  • 7
  • 31
  • 61

1 Answers1

6

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}'
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • What is meant here is that `lsscsi | grep -c 'HITACHI'` and `lsscsi | grep 'HITACHI'|wc -l` will have the same console output: just the number of lines. – questionto42 Jan 16 '23 at 01:53