#!/bin/bash nvidia-smi -q | awk ' # When a new GPU section starts /^GPU / { # Print the previous GPU’s info if all fields are collected if (product_name != "" && architecture != "" && uuid != "" && bus_id != "" && pcie_gen != "" && link_width != "") { print product_name, architecture, uuid, bus_id, "Gen" pcie_gen, "x" link_width } # Reset all variables for the new GPU product_name = ""; architecture = ""; uuid = ""; bus_id = ""; pcie_gen = ""; link_width = ""; in_pcie_gen = 0; in_link_width = 0 } # Extract Product Name (4 spaces indent) /^ Product Name / { split($0, a, / : /); product_name = a[2] } # Extract Product Architecture (4 spaces indent) /^ Product Architecture / { split($0, a, / : /); architecture = a[2] } # Extract GPU UUID (4 spaces indent) /^ GPU UUID / { split($0, a, / : /); uuid = a[2] } # Extract Bus Id (8 spaces indent, under PCI) /^ Bus Id / { split($0, a, / : /); bus_id = a[2] } # Enter PCIe Generation section (12 spaces indent) /^ PCIe Generation/ { in_pcie_gen = 1 } # Enter Link Width section (12 spaces indent) /^ Link Width/ { in_link_width = 1 } # Extract Current values (16 spaces indent) # Note: Only the first "Current" under each section is taken /^ Current/ { if (in_pcie_gen) { split($0, a, / : /); pcie_gen = a[2]; in_pcie_gen = 0 } else if (in_link_width) { split($0, a, / : /); # Remove "x" from link_width if present, as we’ll add it manually split(a[2], w, /x/); link_width = w[1]; in_link_width = 0 } } # Print the last GPU’s info after the file ends END { if (product_name != "" && architecture != "" && uuid != "" && bus_id != "" && pcie_gen != "" && link_width != "") { print product_name, architecture, uuid, bus_id, "Gen" pcie_gen, "x" link_width } }'