How to grep two lines from lshw?
You want the first two lines that match either
serial:. If so, you can try:
$ sudo lshw | grep -Em2 'serial:|product:' product: 20FWCTO1WW (LENOVO_MT_20FW_BU_Think_FM_ThinkPad T460p) serial: PF0P1EUH
grep all lines that match either of the target strings and then use
head to only print the 1st two:
$ sudo lshw | grep -E 'serial:|product:' | head -n2 product: 20FWCTO1WW (LENOVO_MT_20FW_BU_Think_FM_ThinkPad T460p) serial: PF0P1EUH
Of course, both of these approaches assume that you will never have a second
product: before the first
serial: and vice versa.
lshw with the
-c switch to show only the
system class and you don't need to worry about extracting only the 1st occurrence:
lshw -c system | grep -E 'product:|serial:'
If you don't want to use
-c system you could use
quit on 1st occurrence of
lshw | sed '/serial/q;/product/!d'
If you need only the values you could use
lshw -json -c system | jq '.product,.serial'
Assuming you're running those commands as root.
sudo lshw -class system | grep 'product\|serial'