awk -F' ' -v var=value 'BEGIN { beginblock } { formatblock } END { endblock }'echo "..." | awk ...cat filenames | awk ...awk ... filenames| Variable | Bedeutung | Standardwert |
|---|---|---|
| FS | Input Field Separator - trennt $1, $2, … (-F) | Whitespace (Space, Tab, CR) |
| OFS | Output Field Separator - für { print $1, $2 } | Leerzeichen |
| RS | Input Record Separator - trennt Zeilen ($0 pro Record) | \n |
| ORS | Output Record Separator - für print am Ende (newline) | \n |
| CS | Character Separator - nur in manchen awk-Versionen | selten benutzt |
| NF | Number of Fields - Anzahl der Felder in der aktuellen Zeile | - |
| NR | Number of Records - Anzahl der bisher gelesenen Zeilen | - |
| FNR | File Number of Records - Zeilenindex der aktuellen Datei | - |
| FILENAME | Name der aktuellen Datei (wenn filename an awk übergeben) | - |
| Command | Beschreibung | Beispiel |
|---|---|---|
| print print $0 | ganze Zeile "as-is" (default block) | echo "a b c" | awk '{ print $0 }' ⇒ a b c |
| print $1 / print $2 / ... | xtes Wort (ohne quotes) pro Zeile | echo "foo text" | awk '{ print $2 }' ⇒ text |
| print "Text" $1 "Text" | 3 Werte werden concatiniert | echo "foo text" | awk '{ print "A" $1 "B" }' ⇒ AfooB |
| print "Text", $1, "Text" | 3 Werte werden mittels OFS concatiniert | echo "foo text" | awk '{ print "A", $1, "B" }' ⇒ A foo B (see OFS) |
| printf "format", Zahl/Formel | format "%.0f" ganzzahlig runden (ohne NL) | echo "123.789" | awk '{ printf "%.0f", $1 }' ⇒ 124 |
| printf "format", "Text" | format "%s" Text in format einsetzen (ohne NL) | echo "foo text" | awk '{ printf "%s is %s", $2, $1 }' ⇒ text is foo |