Simple awk examples
Last revision August 6, 2004
Table of Contents:
|
These examples work on the pangea system, running Compaq (formerly Digital) Tru64 Unix version 4.0g. The output format of the ps command on other systems may vary.
ps augx | awk '/^farrell/ || /^gp111ins/'
Run the ps command and extract only those lines for processes
owned by either farrell or gp111ins. The process account name
is the first field on the line.
ps augx | awk '{print ($3*$4) " -- " $0}'
The third and fourth fields of the ps output show percent CPU and
percent memory used by the process. Multiply them together to get a crude measure
of overall resources. Print that new quantity followed by the original ps
output line. Because concatenation of text takes precedence over multiplication
of variables, you need to enclose the $3*$4 expression within parentheses
so the multiplication is done first before concatenating its result to the
-- string and the $0 variable (original line contents).
ps augx | awk '$3 > 1.0'
Only show processes that are using at least 1.0% of the CPU time.
ps augx | awk '$5 ~ /M/ {s=s+$5};END {print "total vm =" s}'
Add up the sum of virtual memory sizes being used by all processes that are using
at least one Megabyte. The ps command on pangea shows memory use
suffixed by K character (for Kilobytes) if less than 1024 Kilobytes,
or M character (for Megabytes) if more. The suffixing character is
ignored when awk converts string value to numeric value.
awk '{print NR,$0}' input > output
Add line numbers to a file.