Next script demostrates the use of gawk, which can ignore case as opposed to awk.
First install gawk,ufetch,neofetch,fastfetch,cpufetch,nvidia-drivers.
Use self explaining script below (note i used ai).
First install gawk,ufetch,neofetch,fastfetch,cpufetch,nvidia-drivers.
Use self explaining script below (note i used ai).
Code:
#!/bin/sh
# Define the function to print dashes
myminus() {
# Runs gawk to print a specific number of dashes
gawk 'BEGIN { while(i++<85) printf "-"; print "" }'
}
# Define the shared gawk functions ONCE - Includes fmt and your new my_printf function
GAWK_FUNC='
# General layout function to print formatted and aligned output fields
function my_printf(p1, p2) {
printf "%-18s : %s\n", p1, p2
}
# Shared function to split string text, trim whitespace, and call my_printf
function fmt(str, arr) {
# Isolate the field label from the trailing information using the colon delimiter
split(str, arr, ":")
# Delete leading spaces from the extracted label name in the first array slot
gsub(/^[ \t]*/, "", arr[1])
# Delete everything up to the first colon character from the full text string
gsub(/^[^:]*:/, "", str)
# Delete any remaining leading empty spaces from the text value string
gsub(/^[ \t]*/, "", str)
my_printf(arr[1], str)
}
'
myminus
# 1. pfetch
PF_INFO="os kernel" pfetch | gawk "$GAWK_FUNC"'
BEGIN { IGNORECASE=1 }
{
# Delete all ANSI color escape codes from the current line
gsub(/\x1b\[[0-9;]*m/, "")
# Delete all hidden, non-printable characters from the current line
gsub(/[^[:print:]]/, "")
}
# Only process the line if it contains the word "os"
/os/ {
# Delete the word "os" and any spacing from the very start of the text
gsub(/^.*os[ \t]*/, "", $0)
# Call your new general my_printf function
my_printf("Os", $0)
}
# Only process the line if it contains "kernel" but does NOT contain "host"
/kernel/ && !/host/ {
# Delete the word "kernel" and any spacing from the very start of the text
gsub(/^.*kernel[ \t]*/, "", $0)
# Call your new general my_printf function
my_printf("Kernel", $0)
}'
# 2. ufetch
ufetch | gawk "$GAWK_FUNC"'
BEGIN { IGNORECASE=1 }
{
# Delete all colors and special VT100 graphics drawing characters
gsub(/\x1b\[[0-9;]*m|\x1b\(B/, "")
# Delete all remaining non-printable hidden characters from the line
gsub(/[^[:print:]]/, "")
}
# Only process the line if it contains the word "kernel"
/kernel/ {
# Find the character position where the text "KERNEL:" begins
match($0, /KERNEL:.*/);
# Extract everything from that found position to the end of the line
line = substr($0, RSTART);
# Separate the extracted ufetch kernel data using the colon delimiter
split(line, a, ":");
# Delete any spaces at the beginning of the text inside the second array slot
gsub(/^[ \t]*/, "", a[2]);
# Call your new general my_printf function
my_printf("KERNEL", a[2])
}'
myminus
# 3. neofetch
neofetch \
--off \
--title_fqdn on --package_managers on --os_arch on --speed_shorthand on \
--cpu_brand on --cpu_speed on --distro_shorthand on --kernel_shorthand on \
--uptime_shorthand on --gpu_brand on --de_version on --gtk_shorthand on \
--gtk2 on --gtk3 on --shell_path on --shell_version on --disk_percent on \
--memory_percent on --color_blocks off --bar_border on | \
gawk "$GAWK_FUNC"'
# Skip lines that contain Resolution, Kernel, Theme, Icons, FreeBSD, or multiple dashes
!/Resolution|Kernel|Theme|Icons|FreeBSD|\-\-\-\-/ {
# Delete colors and raw non-printable control characters from the line
gsub(/\x1b\[[0-9;]*m|[^[:print:]]/, "")
# Check if the text line contains a colon divider before executing fmt (which performs internal gsubs)
if ($0 ~ /:/) {
# Call the short formatting function which internalizes my_printf
fmt($0)
}
}'
myminus
# 4. fastfetch
fastfetch --detect-version --logo none | gawk "$GAWK_FUNC"'
BEGIN { IGNORECASE=1 }
NR > 25 { exit }
# Skip lines that match any of these system or hardware labels
!/kernel|packages|shell|terminal|memory|uptime|cpu|lxqt|freebsd|\-\-\-\-/ {
# Check if the text line contains a colon divider before executing fmt (which performs internal gsubs)
if ($0 ~ /:/) {
# Call the short formatting function which internalizes my_printf
fmt($0)
}
}'
myminus
# 5. cpufetch
cpufetch --logo-long -F | gawk "$GAWK_FUNC"'
# Skip any lines that contain the word "size" or "SIZE"
!/size/ && !/SIZE/ {
# Delete color strings and unprintable characters from the current line
gsub(/\x1b\[[0-9;]*m|[^[:print:]]/, "")
# Extract only the hardware info text by cutting off the first 57 logo characters
line = substr($0, 58)
# Check if the extracted text line has content before passing it to fmt (which performs internal gsubs)
if (line ~ /[^[:space:]]/) {
# Call the short formatting function which internalizes my_printf
fmt(line)
}
}'
myminus
# 6. nvidia-smi metrics
nvidia-smi --query-gpu=name,driver_version,fan.speed,temperature.gpu,pstate,power.draw,power.limit,memory.used,memory.total,utilization.gpu --format=csv,noheader,nounits 2>/dev/null | \
gawk "$GAWK_FUNC"'
BEGIN {
# Define display names for each metric position index
labels[1] = "NVIDIA GPU"
labels[2] = "Driver Version"
labels[3] = "NVIDIA Fan"
labels[4] = "NVIDIA Temp"
labels[5] = "Perf State"
labels[6] = "NVIDIA Power"
labels[7] = "NVIDIA VRAM"
labels[8] = "GPU Util"
}
{
# Verify the line does not contain driver errors or blank metrics before processing
if ($0 ~ /invalid field/ || $0 ~ /Not Supported/ || $0 == "") next;
# Parse out individual csv elements using a comma and space sequence as the field separator
split($0, val, ", ")
# Construct combined data fields for power and memory indexes
pwr = (val[6] != "" && val[7] != "") ? val[6] "W / " val[7] "W" : ""
vram = (val[8] != "" && val[9] != "") ? val[8] "MiB / " val[9] "MiB" : ""
# Map raw value data and custom text combinations into a clean loop array
data[1] = val[1]
data[2] = val[2]
data[3] = (val[3] != "") ? val[3] "%" : ""
data[4] = (val[4] != "") ? val[4] "°C" : ""
data[5] = val[5]
data[6] = pwr
data[7] = vram
data[8] = (val[10] != "") ? val[10] "%" : ""
# Loop sequentially from 1 to 8 across all populated monitoring properties
for (i = 1; i <= 8; i++) {
# Verify the target index contains text data before passing it to the aligned layout function
if (data[i] != "") {
my_printf(labels[i], data[i])
}
}
}'
# 7. Extract both Graphics (G) and Compute (C) processes from the native output layout
nvidia-smi 2>/dev/null | \
gawk "$GAWK_FUNC"'
BEGIN { in_proc = 0 }
/Processes:/ { in_proc = 1; next }
in_proc && /^\|[[:space:]]+[0-9]+/ {
# Strip out the exterior layout boundary pipe characters before extracting process text
gsub(/^\||\|$/, "", $0)
pid = $4
type = $5
mem = $NF
pname = ""
# Loop across the middle fields to reassemble the process name even if it contains space characters
for(i=6; i<NF; i++) {
pname = (pname == "" ? $i : pname " " $i)
}
# Verify that the system PID index was collected successfully before sending to standard display
if (pid != "") {
my_printf("Process " pid, pname " (" mem ")")
}
}'
myminus