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.

4
  • 3
    lsscsi | grep HITACHI | awk '{print $6}' | wc -l Commented Aug 3, 2016 at 15:17
  • Thanks. So easy :) Commented Aug 3, 2016 at 15:18
  • 1
    @JuanTomas Never use grep foo | awk '{print $1}'. It should be awk '/foo/{print $1}'. If you use awk, grep is not required meaning unnecessary overhead. Commented Aug 3, 2016 at 15:30
  • Possible duplicate of How to count lines in a document? Commented Aug 3, 2016 at 15:33

1 Answer 1

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}'
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.