awk132
TRANSCRIPT
-
8/11/2019 awk132
1/15
pecially when they are databases. The new version of awk, called nawk, provides additional
capabilities.[1] Every modern Unix system comes with a version of new awk, and its use is
recommended over old awk.
[1] It really isnt so new. The additional features were added in 1!"#, and it was first shipped
with $ystem % &elease '.1 in 1!"(. )evertheless, the name was never chan*ed on most systems.
+ifferent systems vary in what the two versions are called. $ome have oawkand awk, for the oldand new versions, respectively. thers have awkand nawk. $till others only have awk, which is
the new version. This example shows what happens if your awkis the old one-$ awk 1 /dev/nullawk: syntax error near line 1
awk: bailing out near line 1
awkexits silently if it is the new version.
$ource code for the latest version of awk, from ell /abs, can be downloaded startin* at rian
0erni*hans home pa*e- http://cm.bell-labs.com/~bwk. ichael rennans mawkis available via
anonymous 2T3 fromftp://ftp.whidbey.net/pub/brennan/mawk1.3.3.tar.gz. 2inally, the 2ree$oftware 2oundation has a version of awkcalled gawk, available from
ftp://gnudist.gnu.org/gnu/gawk/gawk-3.0.4.tar.gz. 4ll three pro*rams implement 5new5 awk.
Thus, references below such as 5nawkonly,5 apply to all three. gawkhas additional features.
6ith ori*inal awk, you can-
Thin7 of a text file as made up of records and fields in a textual database.
3erform arithmetic and strin* operations.
Use pro*rammin* constructs such as loops and conditionals.
3roduce formatted reports.
6ith nawk, you can also-
+efine your own functions.
Execute Unix commands from a script.
3rocess the results of Unix commands.
3rocess command8line ar*uments more *racefully. 6or7 more easily with multiple input streams.
2lush open output files and pipes 9latest ell /abs awk:.
In addition, with ;)U awk9gawk:, you can-
Use re*ular expressions to separate records, as well as fields.
$7ip to the start of the next file, not ed optionsare-
http://oreilly.com/catalog/unixnut3/chapter/ch11.html#AUTOID-45360http://oreilly.com/catalog/unixnut3/chapter/ch11.html#AUTOID-45360 -
8/11/2019 awk132
2/15
-fs
$et the field separator tofs. This is the same as settin* the system variable "#. ri*inal awk
allows the field separator to be only a sin*le character. nawkallowsfsto be a re*ular expression.
Each input line, or record, is divided into fields by whitespace 9blan7s or tabs: or by some other
user8definable record separator. 2ields are referred to by the variables $1, $,..., $n. $%refers to
the entire record.-!var=value
4ssi*n a !alueto variable !ar. This allows assi*nment before the script be*ins execution9available in nawkonly:.
To print the first three 9colon8separated: fields of each record on separate lines-awk -F: '{ print $1; print $2; print $3 }' /etc/passwd
ore examples are shown in the section 5$imple 3attern83rocedure Examples.5
11.3 Patterns and Proceduresawkscripts consist of patterns and procedures-pattern &procedure
oth are optional. Ifpatternis missin*, &procedureis applied to all lines? if &procedureis
missin*, the matched line is printed.11.3.1 Patterns4 pattern can be any of the followin*-(regular expression(
relational expression
pattern-matching expression
BEG!
E!)
Expressions can be composed of @uoted strin*s, numbers, operators, functions, defined
variables, or any of the predefined variables described later in the section 5uilt8in %ariables.5
&e*ular expressions use the extended set of metacharacters and are described in Ahapter
B,"attern $atching.
*and $refer to the be*innin* and end of a strin* 9such as the fields:, respectively, ratherthan the be*innin* and end of a line. In particular, these metacharacters will notmatch at a
newline embedded in the middle of a strin*.
&elational expressions use the relational operators listed in the section 5perators5 later in this
chapter. 2or example, $ + $1selects linChapter 11 The awk
Programming LanguageContents:
Aonceptual verview
Aommand8/ine $yntax
3atterns and 3rocedures
uilt8in %ariablesperators
%ariables and 4rray 4ssi*nmentsUser8+efined 2unctions
;roup /istin* of aw7 2unctions and Aommands
Implementation /imits
4lphabetical $ummary of 2unctions and Aommands
http://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_01.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_02.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_03.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_04.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_05.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_06.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_07.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_08.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_09.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_10.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_01.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_02.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_03.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_04.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_05.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_06.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_07.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_08.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_09.htmhttp://oreilly.com/catalog/unixnut3/chapter/ch11.html#ch11_10.htm -
8/11/2019 awk132
3/15
11.1 Conceptua !"er"iewawkis a pattern8matchin* pro*ram for processin* files, especially when they are databases. The
new version of awk, called nawk, provides additional capabilities.[1] Every modern Unix system
comes with a version of new awk, and its use is recommended over old awk.
[1] It really isnt so new. The additional features were added in 1!"#, and it was first shipped
with $ystem % &elease '.1 in 1!"(. )evertheless, the name was never chan*ed on most systems.+ifferent systems vary in what the two versions are called. $ome have oawkand awk, for the old
and new versions, respectively. thers have awkand nawk. $till others only have awk, which is
the new version. This example shows what happens if your awkis the old one-$ awk 1 /dev/nullawk: syntax error near line 1
awk: bailing out near line 1
awkexits silently if it is the new version.
$ource code for the latest version of awk, from ell /abs, can be downloaded startin* at rian
0erni*hans home pa*e- http://cm.bell-labs.com/~bwk. ichael rennans mawkis available via
anonymous 2T3 fromftp://ftp.whidbey.net/pub/brennan/mawk1.3.3.tar.gz. 2inally, the 2ree
$oftware 2oundation has a version of awkcalled gawk, available fromftp://gnudist.gnu.org/gnu/gawk/gawk-3.0.4.tar.gz. 4ll three pro*rams implement 5new5 awk.
Thus, references below such as 5nawkonly,5 apply to all three. gawkhas additional features.
6ith ori*inal awk, you can-
Thin7 of a text file as made up of records and fields in a textual database.
3erform arithmetic and strin* operations.
Use pro*rammin* constructs such as loops and conditionals.
3roduce formatted reports.
6ith nawk, you can also-
+efine your own functions.
Execute Unix commands from a script.
3rocess the results of Unix commands. 3rocess command8line ar*uments more *racefully.
6or7 more easily with multiple input streams.
2lush open output files and pipes 9latest ell /abs awk:.
In addition, with ;)U awk9gawk:, you can-
Use re*ular expressions to separate records, as well as fields.
$7ip to the start of the next file, not
-
8/11/2019 awk132
4/15
The BEG!pattern lets you specify procedures that ta7e place beforethe first input line is
processed. 9;enerally, you set *lobal variables here.:
The E!)pattern lets you specify procedures that ta7e place afterthe last input record is
read.
In nawk, BEG!and E!)patterns may appear multiple times. The procedures are mer*ed
as if there had been one lar*e procedure.Except for BEG!and E!), patterns can be combined with the oolean operators ..9or:, //
9and:, and 9not:. 4 ran*e of lines can also be specified usin* comma8separated patterns-pattern0pattern
11.3.2 Procedures3rocedures consist of one or more commands, functions, or variable assi*nments, separated bynewlines or semicolons, and contained within curly braces. Aommands fall into five *roups-
%ariable or array assi*nments
3rintin* commands
uilt8in functions
Aontrol8flow commands
User8defined functions 9nawkonly:11.3.3 Simpe Pattern-Procedure #xampes
3rint first field of each line-& rint $1
3rint all lines that containpattern-(pattern(
3rint first field of lines that containpattern-(pattern( & rint $1
$elect records containin* more than two fields-!" +
Interpret input records as a *roup of lines up to a blan7 line. Each line is a sin*le field-BEG! & "# = 23n24 5# = 22
3rint fields C and ' in switched order, but only on lines whose first field matches thestrin* 5U&;E)T5-$1 , (65GE!7( & rint $80 $
Aount and print the number ofpatternfound- (pattern( & 99x
E!) & rint x
4dd numbers in second column and print total- & total 9= $
E!) & rint 2olumn total is20 total
3rint lines that contain less than CD characters-lengt; %
3rint each line that be*ins with%ame:and that contains exactly seven fields-!" == ? // (*!ame:(
3rint the fields of each input record in reverse order, one per line- &
for
-
8/11/2019 awk132
5/15
-
8/11/2019 awk132
6/15
Arithmetic String Control Flow I/O Time Program-
Functions Functions Statements Processing Functions ming
os['] gsub['] ontinue fflus;[G] systime[#] funtion[']
ex in@ex@oHw;ile['] getline['] system[']
int lengt; exit next
log mat;['] for nextfile[G]
ran@['] slit if rint
sin['] srintf return['] rintf
sIrt sub['] w;ile
sran@['] substr
tolower[']touer[']
['] 4vailable in nawk.
[#] 4vailable in gawk.
[G] 4vailable in ell /abs awkand gawk.
11.0 mpementation Limitsany versions of awkhave various implementation limits, on thin*s such as-
)umber of fields per record
)umber of characters per input record
)umber of characters per output record
)umber of characters per field )umber of characters per rintfstrin*
)umber of characters in literal strin*
)umber of characters in character class
)umber of files open
)umber of pipes open
The ability to handle "8bit characters and characters that are all >ero 94$AII )U/:
gawkdoes not have limits on any of these items, other than those imposed by the machine
architecture andHor the operatin* system.
11.1 pha4etica Summary o, unctions and Commands
The followin* alphabetical list of 7eywords and functions includes all that are available in awk,nawk, and gawk. nawkincludes all old awkfunctions and 7eywords, plus some additional ones
9mar7ed as 567:. gawkincludes all nawkfunctions and 7eywords, plus some additional ones
9mar7ed as 5/7:. Items mar7ed with 5%7are available in the ell /abs awk. Items that arent
mar7ed with a symbol are available in all versions.
atan2atan
-
8/11/2019 awk132
7/15
4reakbreak
Exit from a w;ile, for, or @oloop.
coselose
-
8/11/2019 awk132
8/15
for
-
8/11/2019 awk132
9/15
int
-
8/11/2019 awk132
10/15
Ls 4 strin*.
L@ 4 decimal number.
Lnmf 4 floatin*8point number? nK total number of di*its. mK number of di*its after decimal
point.
L[-]nc nspecifies minimum field len*th for format type c, while -left8
-
8/11/2019 awk132
11/15
Version Variable Description
"EF))7K# 4 space8separated list of field widths to use for splittin* up the record,
instead of "#
G!H5EOD#E 6hen true, all re*ular expression matches, strin* comparisons, and calls
to in@ex >= + += = == &elational operators
9blan7: Aoncatenation
9 - 4ddition, subtraction
J ( L ultiplication, division, and modulus 9remainder:
9 - Unary plus and minus, and lo*ical ne*ation
* JJ Exponentiation
99 -- Increment and decrement, either prefix or postfix
$ 2ield reference
11.& 9aria4es and rray ssignments%ariables can be assi*ned a value with an K si*n. 2or example-"# = 202
Expressions usin* the operators 9, -, (, and L9modulo: can be assi*ned to variables.4rrays can be created with the slit
-
8/11/2019 awk132
12/15
E!) & rint ount[2wi@get2] Print the count
=ou can use the special forloop to read all the elements of an associative array-for
-
8/11/2019 awk132
13/15
Use optional e&prto set a new seed for the random number *enerator. +efault is the time of day.
&eturn value is the old seed. )J
str,timestrftime
-
8/11/2019 awk132
14/15
whiew;ile eros removed
G Eor fconversion, whichever is shortest, with trailin* >eros removed
o Unsi*ned octal value
s $trin*
x Unsi*ned hexadecimal number? uses a8ffor 1D to 1G
V Unsi*ned hexadecimal number? uses D8"for 1D to 1G
L /iteral L
The optionalflagis one of the followin*.
Character Description
- /eft8eros removed.
% 3ad output with >eros, not spaces. This happens only when the field width is wider
than the converted result.
The optional widthis the minimum number of characters to output. The result will be padded to
this si>e if it is smaller. The %fla* causes paddin* with >eros? otherwise, paddin* is with spaces.
-
8/11/2019 awk132
15/15