Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
awk [2024/07/19 00:42] admin angelegt |
awk [2025/05/05 14:06] (aktuell) admin |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | awk ist eine leistungsstarke Textverarbeitungswerkzeug in Unix ähnlichen Betriebssystemen. Es ermöglicht das Extrahieren und Verarbeiten von Daten in Textdateien. | ||
+ | =====Syntax===== | ||
+ | |||
+ | < | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | * pattern: Eine Bedingung, die überprüft wird. Wenn sie wahr ist, wird die Aktion ausgeführt. | ||
+ | * action: Eine Aktion, die ausgeführt wird, wenn das Muster erfüllt ist. | ||
+ | * file: Die Eingabedatei, | ||
+ | |||
+ | See also [[grep]], [[sed|sed]]. | ||
+ | |||
+ | Angenommen, du hast eine Datei namens example.txt mit folgendem Inhalt: | ||
+ | |||
+ | < | ||
+ | Apfel 10 | ||
+ | Banane 5 | ||
+ | Kirsche 20 | ||
+ | </ | ||
+ | |||
+ | Ein paar Beispiele: | ||
+ | < | ||
+ | # Jede Zeile einfach ausgeben | ||
+ | awk ' | ||
+ | |||
+ | # 1. Spalte jeder Zeile ausgeben | ||
+ | awk ' | ||
+ | |||
+ | # Bedingungen (wenn die 2. Spalte größer als 5 ist) | ||
+ | awk '$2 > 5 {print}' | ||
+ | |||
+ | # Berechnungen (2. Spalte jeder Zeile zu " | ||
+ | awk '{sum += $2} END {print " | ||
+ | |||
+ | # Regular esxpressions | ||
+ | awk '/ | ||
+ | |||
+ | # Multiline Kommentare (/**/) entfernen | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | =====CheatSheet===== | ||
+ | |||
+ | < | ||
+ | FILE SPACING: | ||
+ | |||
+ | # double space a file | ||
+ | awk ' | ||
+ | awk ' | ||
+ | |||
+ | # double space a file which already has blank lines in it. Output file | ||
+ | # should contain no more than one blank line between lines of text. | ||
+ | # NOTE: On Unix systems, DOS lines which have only CRLF (\r\n) are | ||
+ | # often treated as non-blank, and thus ' | ||
+ | awk ' | ||
+ | |||
+ | # triple space a file | ||
+ | awk ' | ||
+ | |||
+ | NUMBERING AND CALCULATIONS: | ||
+ | |||
+ | # precede each line by its line number FOR THAT FILE (left alignment). | ||
+ | # Using a tab (\t) instead of space will preserve margins. | ||
+ | awk ' | ||
+ | |||
+ | # precede each line by its line number FOR ALL FILES TOGETHER, with tab. | ||
+ | awk ' | ||
+ | |||
+ | # number each line of a file (number on left, right-aligned) | ||
+ | # Double the percent signs if typing from the DOS command prompt. | ||
+ | awk ' | ||
+ | |||
+ | # number each line of file, but only print numbers if line is not blank | ||
+ | # Remember caveats about Unix treatment of \r (mentioned above) | ||
+ | awk ' | ||
+ | awk ' | ||
+ | |||
+ | # count lines (emulates "wc -l") | ||
+ | awk ' | ||
+ | |||
+ | # print the sums of the fields of every line | ||
+ | awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}' | ||
+ | |||
+ | # add all fields in all lines and print the sum | ||
+ | awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}' | ||
+ | |||
+ | # print every line after replacing each field with its absolute value | ||
+ | awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }' | ||
+ | awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }' | ||
+ | |||
+ | # print the total number of fields (" | ||
+ | awk '{ total = total + NF }; END {print total}' | ||
+ | |||
+ | # print the total number of lines that contain " | ||
+ | awk '/ | ||
+ | |||
+ | # print the largest first field and the line that contains it | ||
+ | # Intended for finding the longest string in field #1 | ||
+ | awk '$1 > max {max=$1; maxline=$0}; | ||
+ | |||
+ | # print the number of fields in each line, followed by the line | ||
+ | awk '{ print NF ":" | ||
+ | |||
+ | # print the last field of each line | ||
+ | awk '{ print $NF }' | ||
+ | |||
+ | # print the last field of the last line | ||
+ | awk '{ field = $NF }; END{ print field }' | ||
+ | |||
+ | # print every line with more than 4 fields | ||
+ | awk 'NF > 4' | ||
+ | |||
+ | # print every line where the value of the last field is > 4 | ||
+ | awk '$NF > 4' | ||
+ | |||
+ | STRING CREATION: | ||
+ | |||
+ | # create a string of a specific length (e.g., generate 513 spaces) | ||
+ | awk ' | ||
+ | |||
+ | # insert a string of specific length at a certain character position | ||
+ | # Example: insert 49 spaces after column #6 of each input line. | ||
+ | gawk --re-interval ' | ||
+ | |||
+ | ARRAY CREATION: | ||
+ | |||
+ | # These next 2 entries are not one-line scripts, but the technique | ||
+ | # is so handy that it merits inclusion here. | ||
+ | |||
+ | # create an array named " | ||
+ | # is ' | ||
+ | | ||
+ | |||
+ | # create an array named " | ||
+ | # mdigit[" | ||
+ | for (i=1; i<=12; i++) mdigit[month[i]] = i | ||
+ | |||
+ | TEXT CONVERSION AND SUBSTITUTION: | ||
+ | |||
+ | # IN UNIX ENVIRONMENT: | ||
+ | awk ' | ||
+ | |||
+ | # IN UNIX ENVIRONMENT: | ||
+ | awk ' | ||
+ | |||
+ | # IN DOS ENVIRONMENT: | ||
+ | awk 1 | ||
+ | |||
+ | # IN DOS ENVIRONMENT: | ||
+ | # Cannot be done with DOS versions of awk, other than gawk: | ||
+ | gawk -v BINMODE=" | ||
+ | |||
+ | # Use " | ||
+ | tr -d \r <infile > | ||
+ | |||
+ | # delete leading whitespace (spaces, tabs) from front of each line | ||
+ | # aligns all text flush left | ||
+ | awk ' | ||
+ | |||
+ | # delete trailing whitespace (spaces, tabs) from end of each line | ||
+ | awk ' | ||
+ | |||
+ | # delete BOTH leading and trailing whitespace from each line | ||
+ | awk ' | ||
+ | awk ' | ||
+ | |||
+ | # insert 5 blank spaces at beginning of each line (make page offset) | ||
+ | awk ' | ||
+ | |||
+ | # align all text flush right on a 79-column width | ||
+ | awk ' | ||
+ | |||
+ | # center all text on a 79-character width | ||
+ | awk ' | ||
+ | |||
+ | # substitute (find and replace) " | ||
+ | awk ' | ||
+ | gawk ' | ||
+ | awk ' | ||
+ | |||
+ | # substitute " | ||
+ | awk '/ | ||
+ | |||
+ | # substitute " | ||
+ | awk ' | ||
+ | |||
+ | # change " | ||
+ | awk ' | ||
+ | |||
+ | # reverse order of lines (emulates " | ||
+ | awk ' | ||
+ | |||
+ | # if a line ends with a backslash, append the next line to it (fails if | ||
+ | # there are multiple lines ending with backslash...) | ||
+ | awk '/\\$/ {sub(/ | ||
+ | |||
+ | # print and sort the login names of all users | ||
+ | awk -F ":" | ||
+ | |||
+ | # print the first 2 fields, in opposite order, of every line | ||
+ | awk ' | ||
+ | |||
+ | # switch the first 2 fields of every line | ||
+ | awk '{temp = $1; $1 = $2; $2 = temp}' file | ||
+ | |||
+ | # print every line, deleting the second field of that line | ||
+ | awk '{ $2 = ""; | ||
+ | |||
+ | # print in reverse order the fields of every line | ||
+ | awk '{for (i=NF; i>0; i--) printf(" | ||
+ | |||
+ | # concatenate every 5 lines of input, using a comma separator | ||
+ | # between fields | ||
+ | awk ' | ||
+ | |||
+ | SELECTIVE PRINTING OF CERTAIN LINES: | ||
+ | |||
+ | # print first 10 lines of file (emulates behavior of " | ||
+ | awk 'NR < 11' | ||
+ | |||
+ | # print first line of file (emulates "head -1") | ||
+ | awk ' | ||
+ | |||
+ | # print the last 2 lines of a file (emulates "tail -2") | ||
+ | awk '{y=x " | ||
+ | |||
+ | # print the last line of a file (emulates "tail -1") | ||
+ | awk ' | ||
+ | |||
+ | # print only lines which match regular expression (emulates " | ||
+ | awk '/ | ||
+ | |||
+ | # print only lines which do NOT match regex (emulates "grep -v") | ||
+ | awk ' | ||
+ | |||
+ | # print any line where field #5 is equal to " | ||
+ | awk '$5 == " | ||
+ | |||
+ | # print only those lines where field #5 is NOT equal to " | ||
+ | # This will also print lines which have less than 5 fields. | ||
+ | awk '$5 != " | ||
+ | awk '!($5 == " | ||
+ | |||
+ | # matching a field against a regular expression | ||
+ | awk ' | ||
+ | awk '$7 !~ / | ||
+ | |||
+ | # print the line immediately before a regex, but not the line | ||
+ | # containing the regex | ||
+ | awk '/ | ||
+ | awk '/ | ||
+ | |||
+ | # print the line immediately after a regex, but not the line | ||
+ | # containing the regex | ||
+ | awk '/ | ||
+ | |||
+ | # grep for AAA and BBB and CCC (in any order on the same line) | ||
+ | awk '/AAA/ && /BBB/ && /CCC/' | ||
+ | |||
+ | # grep for AAA and BBB and CCC (in that order) | ||
+ | awk '/ | ||
+ | |||
+ | # print only lines of 65 characters or longer | ||
+ | awk ' | ||
+ | |||
+ | # print only lines of less than 65 characters | ||
+ | awk ' | ||
+ | |||
+ | # print section of file from regular expression to end of file | ||
+ | awk '/ | ||
+ | awk '/ | ||
+ | |||
+ | # print section of file based on line numbers (lines 8-12, inclusive) | ||
+ | awk ' | ||
+ | |||
+ | # print line number 52 | ||
+ | awk ' | ||
+ | awk ' | ||
+ | |||
+ | # print section of file between two regular expressions (inclusive) | ||
+ | awk '/ | ||
+ | |||
+ | SELECTIVE DELETION OF CERTAIN LINES: | ||
+ | |||
+ | # delete ALL blank lines from a file (same as "grep ' | ||
+ | awk NF | ||
+ | awk '/ | ||
+ | |||
+ | # remove duplicate, consecutive lines (emulates " | ||
+ | awk 'a !~ $0; {a=$0}' | ||
+ | |||
+ | # remove duplicate, nonconsecutive lines | ||
+ | awk ' | ||
+ | awk '!($0 in a){a[$0]; | ||
+ | </ | ||
+ | |||
+ | =====Links===== | ||
+ | |||
+ | * [[https:// |