4

I need to grep specific luns from the lsscsi command.

For example:

[root@e15l1 ~]# lsscsi 4 0 1 | awk '{print $1,$6}' | head
[4:0:1:0] -
[4:0:1:1] /dev/sdab
[4:0:1:2] /dev/sdj
[4:0:1:3] /dev/sdz
[4:0:1:4] /dev/sdk
[4:0:1:12] /dev/sdo
[4:0:1:13] /dev/sdp
[4:0:1:38] /dev/sdad

How can I grep only luns 1, 12 and 13?

I am using:lsscsi | awk '{ print $1,$6 }' | grep -w 4:0:1 | egrep -w '1|1[2-3]'

The problem is when I am searching for a lun with same number as other scsi entries. For example in my case searching lun 1 will give the whole output because my ID is also 1. Same with lun 4(due Host adapter)...

Output should be as in the example, scsi entries and /dev/...

igor
  • 141
  • 1
  • 1
    As I understand I need to use `-F` option to parse the number between ":" and "]"(`lun` number) and then do `grep`. But couldn't find the right command – igor Oct 24 '17 at 12:12

2 Answers2

3

Try this:

lsscsi 4 0 1 | sed -r '/\[4:0:1:(1|12|13)\].*/!d'

Output will be:

[4:0:1:1] /dev/sdab
[4:0:1:12] /dev/sdo
[4:0:1:13] /dev/sdp
Egor Vasilyev
  • 270
  • 1
  • 5
  • Thanks, worked as expected. Can I give a range this way? For example: if I want to search 20-28 `luns`, so I will not write each number? – igor Oct 24 '17 at 12:30
  • @igor, yes. It will be something like this: `sed -r '/\[4:0:1:2[0-8]\].*/!d'` – Egor Vasilyev Oct 24 '17 at 12:38
2

This should work I irrespective of the other numbers anywhere in the line :

egrep '^\[\d+:\d+:\d+:(1|12|13)\]'

Might need also to backslash colons, not at the computer atm to check.

For doing it for a range substitute (1|2|13) for eg (2[0-8])

Gnudiff
  • 533
  • 6
  • 21