AWK und SED

awk ist eine Programmiersprache (Skriptsprache) zur Bearbeitung und Auswertung strukturierter Textdaten, beispielsweise CSV-Dateien.Der zugehörige Interpreter war eines der ersten Werkzeuge, das in der Version 3 von Unix erschien; es wird auch heute noch vielfach zusammen mit sed in Shell-Skripten eingesetzt, um Daten zu bearbeiten, umzuformen oder auszuwerten.

Ich habe hier einige nützliche Helfer zusammen gestellt.

# index.edit -- compile list of index entries for editing
#               new version that matches metacharacters
grep "^\.XX" $* | sort -u |
sed '
h
s/[][\\*.]/\\&/g
x
s/[\\&]/\\&/g
s/^\.XX //
s/$/\//
x
s/^\\\.XX \(.*\)$/\/^\\.XX \/s\/\1\//
G
s/\n//'
awk '# date-month -- convert mm/dd/yy to month day, year

# build list of months and put in array. 
BEGIN { 
        listmonths="January,February,March,April,May,June,July,August,September,November,December" 
        split( listmonths,month,"," )
}

# check that there is input
$1 != "" {

# split on "/" the first input field into elements of array
        z = split($1,date,"/")

# check that only one field is returned
        if (z == 1)
                # try to split on "-"
                z = split($1,date,"-")

# must be invalid
        if (z == 1)
                exit

# add 0 to number of month to ensure numeric type 
        date[1] += 0

# print month day, year
        print month[date[1]], date[2]", 19" date[3] 
}' -
echo "Do it once" | gawk '# lotto - pick x random numbers out of y 
# sort numbers in ascending order
function sort(ARRAY,ELEMENTS,   temp,i,j) {
        for ( i = 2; i <= ELEMENTS; ++i ) 
                for ( j = i; ARRAY[j-1] > ARRAY[j]; --j ) { 
                        temp = ARRAY[j]
                        ARRAY[j] = ARRAY[j-1]
                        ARRAY[j-1] = temp
        }
return 
}
# main routine
{
# test command line args; NUM = $1, how many numbers to pick 
#                     TOPNUM = $2, last number in series
        if ( NUM <= 0) 
                NUM = 6
        if ( TOPNUM <= 0 ) 
                TOPNUM = 30
# print "Pick x of y"
        printf("Pick %d of %d\n", NUM, TOPNUM) 
# load TOPNUM numbers into array
        for ( k = 1; k <= TOPNUM; ++k )
                range[k] = k
# seed random number using time and date; do this once
        srand() 
# loop until we have NUM selections
        for ( j = 1; j <= NUM; ++j ) {
                select = 1 + int(rand() * TOPNUM)
                # this conditional checks for duplicates
                if ( range[select] == "") { 
                        --j
                        continue
                }
                pick[j] = range[select]
                delete range[select]
        }

# sort numbers in ascending order
        #for ( i = 2; i <= NUM; ++i ) 
                #for ( j = i; pick[j-1] > pick[j]; --j ) { 
                        #temp = pick[j]
                        #pick[j] = pick[j-1]
                        #pick[j-1] = temp
       #}
# call sort function to sort pick array
        sort(pick,NUM) 

# loop through array and print picks.
        for ( j = 1; j <= NUM; ++j ) 
                printf("%s ", pick[j])
        printf("\n")

}' NUM=$1 TOPNUM=$2 
ls -l $* | awk '
# filesum: list files and total size in bytes
# input: long listing produced by "ls -l"

#1 output column headers
BEGIN { printf("%-15s\t%10s\n","FILE","BYTES" )
}
#2 test for 9 fields; files begin with "-"
NF == 9 && /^-/ {
        sum += $5       # accumulate size of file
        ++filenum       # count number of files
        printf("%-15s\t%10d\n", $9, $5)       # print filename and size
}

#3 test for 9 fields; directory begins with "d"
NF == 9 && /^d/ {
        print "<dir>", "\t", $9  #print <dir> and name
}

#4 test for ls -lR line ./dir:
$1 ~ /^\..*:$/ {
        print "\t" $0 # print that line preceded by tab
}

#5 once all is done,
END {
        #print total file size and number of files
        printf("Total: %d bytes  (%d files)\n", sum, filenum)
}' -
awk '# date-month -- convert mm/dd/yy to month day, year

# build list of months and put in array. 
BEGIN { 
        listmonths="January,February,March,April,May,June,July,August,September,November,December" 
        split( listmonths,month,"," )
}

# check that there is input
$1 != "" {

# split on "/" the first input field into elements of array
        z = split($1,date,"/")

# check that only one field is returned
        if (z == 1)
                # try to split on "-"
                z = split($1,date,"-")

# must be invalid
        if (z == 1)
                exit

# add 0 to number of month to ensure numeric type 
        date[1] += 0

# print month day, year
        print month[date[1]], date[2]", 19" date[3] 
}' -
#Eine Sequnenz erzeugen:

sed '/^>/s/.*//;/^$/=;/^$/d' text.txt | sed 's/[1-9].*/echo ">seq" \$(( ( & + 1 )\/2 ))/e'

Aufruf: seq [OPTION]... LETZTER
 oder:  seq [OPTION]... ERSTER LETZTER
 oder:  seq [OPTION]... ERSTER PLUS LETZTER
Die Zahlen von ERSTER bis LETZTER ausgeben, in Schritten von PLUS.

Erforderliche Argumente für lange Optionen sind auch für kurze erforderlich.
  -f, --format=FORMAT      Fließkomma‐FORMAT im Stil von printf benutzen
  -s, --separator=ZKETTE   ZKETTE benutzen, um Zahlen zu trennen (Vorgabe: \n)
  -w, --equal-width        gleiche Breite durch führende Nullen herstellen
      --help     diese Hilfe anzeigen und beenden
      --version  Versionsinformation anzeigen und beenden

Wenn ERSTER oder PLUS weggelassen werden, wird 1 angenommen. Ein weggelassenes
PLUS wird also als 1 interpretiert, selbst wenn LETZTER kleiner als ERSTER ist.
ERSTER, PLUS und LETZTER werden als Fließkommazahlen interpretiert.
PLUS ist normalerweise positiv, wenn ERSTER kleiner als LETZTER ist, und
PLUS ist normalerweise negativ, wenn ERSTER größer als LETZTER ist.
FORMAT muss in der Lage sein, ein Argument vom Typ „double“ auszugeben;
per Voreinstellung ist es %.PRÄZf, wenn ERSTER, PLUS und LETZTER alle dezimale
Fixkommazahlen sind mit einer maximalen Genauigkeit PRÄZ, ansonsten %g.
Die Angabe der Zahlen auf der Kommandozeilen muss im englischen Format erfolgen,
die Ausgabe hängt dagegen von der gewählten Locale ab (in diesem Fall wird also
ein Dezimalpunkt in der Eingabe und ein Dezimalkomma in der Ausgabe benutzt.)

GNU coreutils Onlinehilfe: <http://www.gnu.org/software/coreutils/>
Melden Sie Übersetzungsfehler für seq an <translation-team-de@lists.sourceforge.net>
Die vollständige Dokumentation ist hier: <http://www.gnu.org/software/coreutils/seq>
oder auch lokal mittels „info '(coreutils) seq invocation'“
Print Friendly, PDF & Email

1 Gedanke zu „AWK und SED“

Schreibe einen Kommentar

Cookie Consent mit Real Cookie Banner
%d Bloggern gefällt das: