Quick Tip
Tame Your Processes: Focused `ps` Output with `awk`
Challenge: The output of the ps command can be overwhelming, especially when you need to quickly find specific process information like a process ID (PID) or its resource usage. Piping ps to grep is common, but it’s not always the most efficient or elegant solution for structured data.
The Solution: Use awk to filter and format the output of ps for precise data extraction.
ps aux | awk '$11 ~ /my_process_name/ { print $2, $11 }'
Why it works: awk is a powerful text-processing tool that excels at working with delimited data. Here, we’re using it to examine the output of ps aux (which provides a comprehensive snapshot of running processes). awk '$11 ~ /my_process_name/' filters lines where the 11th field (typically the command name) contains “my_process_name”. Then, { print $2, $11 } prints the PID (2nd field) and the command name (11th field) for those matching processes.
Pro-Tip: To get even more focused output, you can use awk‘s field manipulation capabilities. For example, ps aux | awk 'NR>1 { printf "%-10s %s\n", $2, $11 }' will print the PID and command name with left-aligned padding for better readability, skipping the header row (NR>1).
Linux Tips & Tricks | © ngelinux.com | 5/26/2026
