[bash] remove some entries from output

Hello everyone,

I have the following script

sudo lshw -class disk | egrep -E "product|logical name|size:" | awk ' {print;} NR % 3 == 0 { print ""; }'

This is the current output on my machine

       product: BUP Slim SL 
       logical name: /dev/sdh
       size: 1863GiB (2TB)

       product: STORAGE DEVICE
       logical name: /dev/sdc
          logical name: /dev/sdc

       product: STORAGE DEVICE
       logical name: /dev/sdd
          logical name: /dev/sdd

       product: STORAGE DEVICE
       logical name: /dev/sde
          logical name: /dev/sde

       product: STORAGE DEVICE
       logical name: /dev/sdf
          logical name: /dev/sdf

       product: STORAGE DEVICE
       logical name: /dev/sdg
          logical name: /dev/sdg

       product: OCZ-VECTOR150
       logical name: /dev/sda
       size: 223GiB (240GB)

       product: Samsung SSD 860
       logical name: /dev/sdb
       size: 465GiB (500GB)

       product: BD-RE  BH12LS35
       logical name: /dev/cdrom
       logical name: /dev/sr0

       product: DRW-24B1ST   a
       logical name: /dev/cdrw
       logical name: /dev/dvd

       logical name: /dev/dvdrw
       logical name: /dev/sr1

But, I would like the following

       product: OCZ-VECTOR150
       logical name: /dev/sda
       size: 223GiB (240GB)

       product: Samsung SSD 860
       logical name: /dev/sdb
       size: 465GiB (500GB)

So my question is... How do I achieve the the above output and getting rid of everything else?

You need jq installed for this. The maintainers describe jq as a sed -like tool for json files.

$ sudo lshw -class disk -json | jq -c . | grep size | jq '{product,logicalname,size}'
{                         
  "product": "Backup+  Desk",
  "logicalname": "/dev/sda",
  "size": 3000592977920
}
{
  "product": "Micron_M510_2.5_",
  "logicalname": "/dev/sdb",
  "size": 256060514304
}

The first instance of jq minimises the output to one device per line. sed is then used to reduce the output to only devices with a size field. jq is used once again to pull out the product name, logical name and size for each remaining device. You can then use awk or some other tool to reformat the output into something more palatable, if you desire.

jq will probably be in your Linux repository and should be easy to install.

Andrew

Is there any feature in the unfiltered lshw output that allows us to discriminate between desired and undesired devices? In your above sample, that might be size, but that would also output /dev/sdh .

Try piping through

awk '/product/ {T1 = $0; T2 = ""} /logical name/ {T2 = $0}  /size:/ {print T1; print T2; print $0; print ""}'
2 Likes

Oops! Missed that! I agree that there should be something better to filter out on than size. It was simply the one that worked for my system.

Andrew

Thank you so much for your help guys!