I want to grep the motherboard serial number and the product model of a computer. I used

sudo lshw | grep -m1  serial:

to grep the serial number (since there are multiple occurrences of "serial:" and the one I want is the first one. How can I do this AND simultaneously grep for "product:" as well? There are also multiple occurrences of product, and the first one is again the one I want.

lshw returns this:

user@ubuntu:~$ sudo lshw
    description: Notebook
    product: 23252DG (LENOVO_MT_2325)
    vendor: LENOVO
    version: ThinkPad X230
    serial: R9TWZVR
    width: 64 bits
    capabilities: smbios-2.7 dmi-2.7 vsyscall32
    configuration: administrator_password=disabled chassis=notebook family=ThinkPad X230 power-on_password=disabled sku=LENOVO_MT_2325 uuid=01ECC0B1-8251-CB11-8538-B7D9EC435D9B
       description: Motherboard
       product: 23252DG
       vendor: LENOVO
       physical id: 0
       version: Not Defined
       serial: 1ZPAB2AC2C1
       slot: Not Available
          description: CPU
          product: Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
          vendor: Intel Corp.
          physical id: 1
          bus info: cpu@0
          version: Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
          serial: None

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.

  • Nice! Out of curiosity, how did you know to use system? I don't see it mentioned in man lshw. Can we be confident that -c system will always show the motherboard details and nothing else? – terdon May 4 '17 at 11:00
  • 1
    @terdon - it's in the manual: if you run lshw -short (to see the hardware classes) the output is self-explanatory – don_crissti May 4 '17 at 11:04
  • 1
    Ah, so it is, thanks. Apparently, I only read the first sentence of the -c section in the manual and missed the second which explained precisely that. Sigh. – terdon May 4 '17 at 11:09

Using grep and -class option:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.