Site icon New Generation Enterprise Linux

Master `ps` Output with Custom `awk` Formatting

Quick Tip

Master `ps` Output with Custom `awk` Formatting

Challenge: The standard `ps` command can often produce an overwhelming amount of information, making it difficult to extract the specific details you need about running processes. You might want to see only the process ID (PID), user, and command, or sort them by CPU usage.

The Solution: Use `ps` in conjunction with `awk` to precisely control the output. This allows you to select specific columns and format them as desired.

ps aux | awk '{printf "%-10s %-15s %s\n", $2, $1, $11}'

Why it works: The `ps aux` command lists all running processes with detailed information. `awk` then processes this output line by line, using `printf` to format specific fields (columns) into a more readable and concise output, in this case, PID, User, and the command itself.

Pro-Tip: To sort processes by CPU usage, you can pipe the output to `sort -k3nr` before the `awk` command. For example: `ps aux | sort -k3nr | awk ‘{printf “%-10s %-15s %s\n”, $2, $1, $11}’`

Linux Tips & Tricks | © ngelinux.com | 5/20/2026

0 0 votes
Article Rating
Exit mobile version