Skip to content

Instantly share code, notes, and snippets.

@crap0101
Created February 3, 2024 11:10
Show Gist options
  • Save crap0101/f7f8d6d4760b2397dea6da9a30d842cf to your computer and use it in GitHub Desktop.
Save crap0101/f7f8d6d4760b2397dea6da9a30d842cf to your computer and use it in GitHub Desktop.
## !!! gawk, non posix
## run: gawk -v cmp=[fst|snd] -f thisfile
## prints half `lspci -k` output, cutting sensibly ;)
## see: https://forum.ubuntu-it.org/viewtopic.php?f=33&t=654734&sid=8194abf8e927662044c7c8f3ab902872
function fst(val, cmp) {
return (val < cmp) ? 1 : 0
}
function snd(val, cmp) {
return (val >= cmp) ? 1 : 0
}
BEGIN {
if (cmp != "fst" && cmp != "snd") {
print "ERROR: cmp: " cmp
exit(1)
}
PROCINFO["sorted_in"] = "@ind_num_asc"
current = ""
cmd = "lspci -k"
lines = 0
while (0 < (cmd | getline)) {
lines++
if (match($1, /^([0-9]+:[0-9]+).[0-9]+$/, arr)) {
if (! (arr[1] in ports)) {
ports[arr[1]] = $0 "\n"
current = arr[1]
}
else
ports[arr[1]] = sprintf("%s%s\n", ports[arr[1]], $0)
} else
ports[current] = sprintf("%s%s\n", ports[current], $0)
}
close(cmd)
div = lines / 2
tot_lines = 0
for (port in ports) {
tot_lines += nl = split(ports[port], arr, "\n")
if (@cmp(tot_lines, div))
printf("%s", ports[port])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment