Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

awk

Process text line by line as fields: print columns, filter by conditions, count and sum.

Niveau
RHCSA-examen
Groep
Tekst en zoeken
Root nodig
Nee
Pakket
gawk
Ook
gawk

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

awk reads input line by line and splits each line into fields: $1, $2 … ($0 is the whole line). An awk program is a list of pattern { action } pairs: when the pattern matches a line, the action runs.

Useful built-in variables: NR (line number), NF (number of fields, so $NF = last field), FS (field separator, set with -F), OFS (output separator). BEGIN { } runs before the first line, END { } after the last.

On RHEL, awk is GNU awk (gawk).

Zo schrijf je het

awk [OPTIONS] 'PROGRAM' [FILE...]
awk [OPTIONS] -f PROGRAM-FILE [FILE...]

Opties 28

Meest gebruikt

-F fs, --field-separator fs
The field separator, like -F: or -F, (a regex is allowed).
-v var=value, --assign var=value
Set a variable before the program starts.
-f file, --file file
Read the program from a file.
-e text, --source text
Program text (can be combined with -f).
-E file, --exec file
Like -f, but the last option (for scripts with #!).
-i file, --include file
Load an awk library file.
-l lib, --load lib
Load a shared library extension.
-b, --characters-as-bytes
Treat all input as bytes, not characters.
-c, --traditional
Compatibility mode: no GNU extensions.
-P, --posix
Strict POSIX mode.
-r, --re-interval
Allow {n,m} in regular expressions (default in new versions).
-n, --non-decimal-data
Read octal and hex numbers in the input.
-N, --use-lc-numeric
Use the local decimal point when reading input.
-M, --bignum
Use big numbers with any precision.
-S, --sandbox
Disable system(), redirections and reading other files.
-d[<file>], --dump-variables[=<file>]
Print all global variables at the end.
-D[<file>], --debug[=<file>]
Start the awk debugger.
-p[<file>], --profile[=<file>]
Write a profile of the program.
-o[<file>], --pretty-print[=<file>]
Write a nicely formatted version of the program.
-L[<value>], --lint[=<value>]
Warn about bad or not portable code.
-t, --lint-old
Warn about code that old awk versions do not support.
-O, --optimize
Turn on optimizations (default).
-s, --no-optimize
Turn off optimizations.
-I, --trace
Print each instruction as it runs.
-g, --gen-pot
Make a .pot file for translations.
-V, --version
Show the version.
-h, --help
Show help.

Voorbeelden 13

$ als gewone gebruiker# als root (of met sudo)

  1. 1Print one column

    awk '{print $1}' /var/log/httpd/access_log

    Field 1 = the client IP address. Fields are split by spaces.

  2. 2Use another separator

    awk -F: '{print $1, $7}' /etc/passwd

    User name and shell. -F: sets the separator.

  3. 3Filter by a condition

    awk -F: '$3 >= 1000 {print $1}' /etc/passwd

    Only normal users (UID 1000 and higher).

  4. 4Print lines that match a pattern

    awk '/sshd/ {print $1, $2, $3, $5}' /var/log/messages

    Like grep, but you choose the columns.

  5. 5The last field of each line

    awk '{print $NF}' file.txt

    NF = number of fields, so $NF = the last one.

  6. 6Sum a column

    ps aux | awk 'NR>1 {sum += $6} END {printf "%.1f MiB\n", sum/1024}'

    Total RSS memory of all processes. NR>1 skips the header.

  7. 7Disk use above 80%

    df -P | awk 'NR>1 && $5+0 > 80 {print $6 " is " $5 " full"}'

    $5+0 turns "85%" into the number 85.

  8. 8Count lines per value (like a pivot)

    awk '{count[$9]++} END {for (c in count) print c, count[c]}' /var/log/httpd/access_log

    Number of requests per HTTP status code.

  9. 9Pass a shell variable into awk

    LIMIT=500; awk -v max="$LIMIT" '$2 > max' usage.txt

    -v is the safe way; do not put shell variables inside the awk quotes.

  10. 10Change the output separator to make a CSV

    awk -F: 'BEGIN {OFS=","} {print $1, $3, $6}' /etc/passwd

    user,uid,home for every account.

  11. 11Print a range of lines

    awk 'NR>=10 && NR<=20' /etc/services

    Lines 10 to 20.

  12. 12Failed SSH logins per IP

    awk '/Failed password/ {print $(NF-3)}' /var/log/secure | sort | uniq -c | sort -rn | head

    The IP is the 4th field from the end in these log lines.

  13. 13Remove repeated lines and keep the order

    awk '!seen[$0]++' list.txt

    Prints each line only the first time it appears.

Let op

  • Put the program in single quotes, so the shell does not change $1.
  • Without -F, fields are split on runs of spaces and tabs (good for most command output).
  • awk counts fields from 1; $0 is the whole line.

Tips om te onthouden

  • Pattern { action }. $1 first field, $NF last, NR line number, -F: separator, END {} totals.