IntroductionOf course a quick introduction shouldn't have an introduction.... What it can doWhat it can do is:
SyntaxCode is set up in a if-match-then code blocks. Every code block has a pattern in front of it. The most common are: BEGIN, END and /regular expression/. None of these blocks are mandatory, all are shown below. BEGIN{ #code here, this is a comment } /[314]*/{ } END{ } BEGIN is executed at the program beginning, END at the end. The rest of the matches are done to records. Records are collected from the input by splitting the input into chunks using the record seperator RS. When a record matches, it is split into fields using the field seperator FS. So the input data is scanned as "fieldFSfieldFSfieldRS". FS and RS are both variables and can be set using the "=" operator. They default to FS=" " and RS="\n" Both have their counterparts for output: OFS and ORS
Once the record has been split up (using FS), it's available like Bash variables: $0 is the whole record, $1 the first field, $2 the second, etc ... Normal variables have no type or distinct starting characters. End statements with a semicolon (";") and/or newline. Most common functions
ExecutionUse one of the following (or look at the manual once):
awk -f program-file
awk -f program-file -- file names to process awk -- "program-text" file names to process Examples
This is a very simple user list to very simple html conversion in AWK:
execute using
BEGIN{
FS = ":"
ORS= "<br />\n"
print "<html><body>"
}
//{
print "User <b>" $1 "</b>"
print "lives at " $6
user += 1
}
END{
print "";
print "A total of " user " users on the system."
print "</body></html>"
}
Line for line, the following is stated above:
Here is a quick line to get all
strings diskimage.img |awk -- "/^[A-Z][a-z][a-z] [0-9 ][0-9 ] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/{print; next}//{unmatched++}END{print unmatched \" lines unmatched.\"}"
For more examples, read the manual ( See alsoThis is such a quick and very dirty introduction to AWK that if you really want to use it
allot, you should read more on it. |