How to grep two lines from lshw?

You want the first two lines that match either product: or serial:. If so, you can try:

$ sudo lshw | grep -Em2 'serial:|product:'
    product: 20FWCTO1WW (LENOVO_MT_20FW_BU_Think_FM_ThinkPad T460p)
    serial: PF0P1EUH

Alternatively, 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.


Use lshw with the --class or -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 sed and quit on 1st occurrence of serial:

lshw | sed '/serial/q;/product/!d'

If you need only the values you could use jq:

lshw -json -c system | jq '.product,.serial'

Assuming you're running those commands as root.


Using grep and -class option:

sudo lshw -class system | grep 'product\|serial'