Lukáš Bařinka
man -f awk
awk
nawk
gawk
mawk
awk
nawk
gawk
mawk
awk
[ POSIX or GNU style options ] -f program-file [ -- ] file ...
awk
[ POSIX or GNU style options ] [ -- ] program-text file ...
-F ERE | set Field Separator using ERE |
-v var=val | set value val into variable varbefore BEGIN section |
-F ERE | nastaví Field Separator na ERE |
-v var=val | nastaví prom. var na hodnotu valpřed sekcí BEGIN |
awk 'BEGIN { FS = ":" }; { print $1 }' /etc/passwd
awk -F: '{ print $1 }' /etc/passwd
awk -v FS=: '{ print $1 }' /etc/passwd
awk '{ print $1 }' FS=: /etc/passwd
print expr
exit expr
num = sub( ERE, subst [, text] )
num = gsub( ERE, subst [, text] )
string = gensub( ERE, subst, flags [, text] )
print expr
exit expr
num = sub( ERE, subst [, text] )
num = gsub( ERE, subst [, text] )
string = gensub( ERE, subst, flags [, text] )
var |
Variable (value of variable) Inital (uninitialized variable) value: (int) 0 , (string) "" |
"val" |
String value e.g. "foo" |
number |
Numeric value e.g. 123.45 |
/ERE/ |
Extended Regular Expression |
$expr |
Value of exprth filed e.g. $NF — the last field, $(NF-1) — the last but one |
var |
Proměnná (hodnota proměnné) Výchozí (neinicializovaná proměnná) hodnota: (int) 0 , (string) "" |
"val" |
Řetězcová hodnota např. "foo" |
číslo |
Číselná hodnota např. 123.45 |
/ERE/ |
Rozšířený regulární výraz |
$expr |
Hodnota expr. pole e.g. $NF — poslední pole, $(NF-1) — předposlední pole |
if (condition) statement [ else statement ]
while (condition) statement
do statement while (condition)
for (expr1; expr2; expr3) statement
for (var in array) statement
break
continue
delete array[index]
delete array
exit [ expression ]
{ statements }
awk -F: '{ for(i=1;i<=NF;i++) print i,$i; print "-----" }' /etc/passwd
awk -f - /etc/passwd <<'AWK'
BEGIN {
FS = ":"
}
{
print "Record:", NR
for (i=1; i<=NF; i++) {
if ( i == 2 ) continue
if ( $i != "" ) print i, $i
else print i, "-none-"
}
print "-----"
}
AWK
Record: 1
1 root
3 0
4 0
5 root
6 /root
7 /bin/bash
-----
...
-----
Record: 47
1 debian-spamd
3 124
4 132
5 -none-
6 /var/lib/spamassassin
7 /usr/sbin/nologin
-----
awk -F: '
NF != 7 { print "NF"; e=1; exit 3 }
$2 != "x" { print "x"; e=1; exit 4 }
$3 !~ /^[0-9]+/ { print "uid"; e=1; exit 5 }
$4 !~ /^[0-9]+/ { print "gid"; e=1; exit 6 }
END {
if ( e ) exit
if ( NR==0 ) { print "empty"; exit 1 }
else { print "ok"; exit 0 }
}
' /etc/passwd; echo $?
arr[index] = value
split
function
item_count = split("one two three", arr, " ")
,
character
Translated to SUBSEP=\034
[ASCII file separator]
e.g. x=5; y=7; arr[x,y]
is stored as arr["5\0347"]
arr[index] = value
split
item_count = split("one two three", arr, " ")
,
Překládá na SUBSEP=\034
[ASCII file separator]
např. x=5; y=7; arr[x,y]
je uloženo jako arr["5\0347"]
for ( index in arr ) print index " -> " arr[index]
item_count = split("one two three", arr, " ")
for ( i=1; i<=item_count; i++ ) print i, arr[i]
if ( index in arr ) print arr[index]
for ( index in arr ) print index " -> " arr[index]
item_count = split("one two three", arr, " ")
for ( i=1; i<=item_count; i++ ) print i, arr[i]
if ( index in arr ) print arr[index]
a[1] = 5
a[2][1] = 6
history \
| awk '{ print $2 }' \
| sort \
| uniq -c \
| sort -rn \
| head
history \
| awk '
{ a[$2]++ }
END {
PROCINFO["sorted_in"]="@val_num_desc"
for( i in a ) {
print a[i] " " i
if (++c == 10) exit
}
}
'
{ file[NR] = $0 }
END {
srand()
for ( i=1; i<=NR; i++ ) {
RAND = int(NR*rand())+1
temp = file[i]
file[i] = file[RAND]
file[RAND] = temp
}
for ( i=1; i<=NR; i++ ) {
print "\"" file[i] "\""
# system( system( cmd " " X ) )
}
}
!x[$0]++
fflush( [file] )
close( file [, how] )
to
| from
fflush( [soubor] )
close( soubor [, jak] )
to
| from
getline
getline <file
getline var
getline var <file
command | getline [var]
command |& getline [var]
next
nextfile
print
print expr-list
print expr-list >file
printf fmt, expr-list
printf fmt, expr-list >file
print ... >> file
print ... | command
print ... |& command
print ... >> soubor
print ... | příkaz
print ... |& příkaz
system( cmd-line )
#!/usr/bin/awk -f
BEGIN {
server="wordnetweb.princeton.edu"
# define HTTP connection to web server
connection="/inet/tcp/0/"server"/www"
# construct HTTP request line
term=ARGV[1]
url="/perl/webwn?s=" term
request="GET " url " HTTP/1.0\n\n"
start=0
# send HTTP request
print "Sending request..." > "/dev/stderr"
print request |& connection
# receive HTTP response
print "Receiving response..." > "/dev/stderr"
while (connection |& getline) {
... # data processing
}
close(connection)
}
# debug to stderr
print ++lines,go "::" $0 >> "/dev/stderr"
# process data
if ( $0 ~ "<h3>" ) {
go=1
gsub("^.*<h3>","")
gsub("<[^>]*>","")
print
continue
}
if ( go && $0 ~ "<li>" ) {
gsub("<i>[:space:]*</i>","")
gsub("<i>","\n > ")
gsub("<[^>]*>","")
sub("S: \\(.\\) ","")
gsub(";","\n >")
print " * " $0
}
if ( $0 ~ "</ul>" ) {
go=0
continue
}
function name(parameter list) { statements }
function f(p, q , a, b) # a and b are local
f(1, 2)
return expr
function f(p, q , a, b) # a, b jsou lokální
f(1, 2)
return expr
app-admin app-shells dev-python net-dns sys-apps sys-power
app-arch app-text dev-util net-firewall sys-auth sys-process
app-backup app-vim dev-vcs net-fs sys-block virtual
app-editors dev-db mail-client net-libs sys-boot www-servers
app-emulation dev-java mail-mta net-mail sys-devel x11-libs
app-i18n dev-lang media-fonts net-misc sys-fs x11-misc
app-misc dev-libs media-libs net-nds sys-kernel x11-proto
app-portage dev-perl net-analyzer perl-core sys-libs
/var/db/pkg/app-misc
ca-certificates-20110502-r1
editor-wrapper-4
mc-4.8.1-r1
BUILD_TIME CXXFLAGS HOMEPAGE NEEDED SIZE
CATEGORY DEFINED_PHASES INHERITED NEEDED.ELF.2 SLOT
CBUILD DEPEND IUSE PF USE
CFLAGS DESCRIPTION KEYWORDS PKGUSE
CHOST EAPI LDFLAGS RDEPEND
CONTENTS environment.bz2 LICENSE repository
COUNTER FEATURES mc-4.8.1-r1.ebuild REQUIRED_USE
mime-types-8
pax-utils-0.2.3
realpath-1.15-r1
screen-4.0.3-r4
GNU Midnight Commander is a text based file manager
/var/db/pkg/app-misc/mc-4.8.1-r1/HOMEPAGE
http://www.midnight-commander.org
#!/usr/bin/awk -f
BEGIN {
FS = "/"
basedir = "/var/db/pkg"
print "===== Software list ====="
while ( "cd /var/db/pkg; ls -d */*" | getline ) {
if ( $1 != category ) {
category = $1
print "\n==== " category " ===="
}
pkg( $2 )
}
print "\n --- //Generated: " strftime( "%c" ) "//"
}
function pkg( info ) {
package = info
sub( "-[0-9].*", "", package )
version = info
sub( package"-", "", version )
desc_file = basedir"/" $0 "/DESCRIPTION"
getline description < desc_file
close( desc_file )
web_file = basedir "/" $0 "/HOMEPAGE"
getline homepage < web_file
close( web_file )
gsub( " ", " \\\\ ", homepage )
print " * **" package "** //(" version ")// \
\\\\ " description " \\\\ " homepage
}
BEGIN {
n = split( "f e d c b a", a, " " )
for ( i in a )
print i, a[i]
}
4 c
5 b
6 a
1 f
2 e
3 d
BEGIN {
n = split( "f e d c b a", a, " " )
for ( i=1; i<=n; i++ )
print i, a[i]
}
1 f
2 e
3 d
4 c
5 b
6 a
BEGIN {
split( "f e d c b a", a, " " )
n = asort( a )
for ( i=1; i<=n; i++ )
print i, a[i]
}
1 a
2 b
3 c
4 d
5 e
6 f
BEGIN {
a["z"] = 11
a["x"] = 99
a["y"] = 100
n = asorti( a, b )
for ( i=1; i<=n; i++ )
print i, b[i], a[b[i]]
}
1 x 99
2 y 100
3 z 11
PROCINFO["sorted_in"]
for
loops
Hodnota ovládá způsob, jakým je pole procházeno ve for
cyklu
"@ind_str_asc", "@ind_num_asc", "@val_type_asc",
"@val_str_asc", "@val_num_asc", "@ind_str_desc",
"@ind_num_desc", "@val_type_desc", "@val_str_desc",
"@val_num_desc", and "@unsorted" or "cmp_func"
function cmp_func(i1, v1, i2, v2)
BEGIN {
a["z"] = 11
a["x"] = 99
a["y"] = 100
PROCINFO["sorted_in"] = "@ind_str_asc"
for ( i in a )
print i, a[i]
}
x 99
y 100
z 11
function f(a, b, c, d) {
r = rand() - 0.5
print "?", a, b, c, d, r
return r
}
BEGIN {
srand()
a["z"]=11; a["x"]=99; a["y"]=100
PROCINFO["sorted_in"] = "f"
for ( i in a )
print i, a[i]
}
? y 100 z 11 0.439477
? x 99 z 11 -0.176757
x 99
z 11
y 100
<section id="toc">
<h2>
<span lang="en">TOC</span>
<span lang="cs">Obsah</span>
</h2>
<ol lang="en">
<li><a href="#syntax">Syntax</a></li>
<li><a href="#basic">Basic usage</a></li>
<li><a href="#control">Control Statements</a></li>
<li><a href="#arrays">Arrays</a></li>
<li><a href="#i-o">Input/Output</a></li>
<li><a href="#functions">Functions</a></li>
<li><a href="#traversal">Array Traversal</a></li>
</ol>
<ol lang="cs">
<li><a href="#syntax">Syntaxe</a></li>
<li><a href="#basic">Jednoduché použití</a></li>
<li><a href="#control">Řídící konstrukce</a></li>
<li><a href="#arrays">Pole</a></li>
<li><a href="#i-o">Vstup a výstup</a></li>
<li><a href="#functions">Funkce</a></li>
<li><a href="#traversal">Průchod polem</a></li>
</ol>
</section>
<section>
<section id="syntax">
<h2>
<span lang="en">Syntax</span>
<span lang="cs">Syntaxe</span>
</h2>
</section>
</section>
<section>
<section id="basic">
<h2>
<span lang="en">Basic usage</span>
<span lang="cs">Jednoduché použití</span>
</h2>
</section>
</section>
...
#!/usr/bin/awk -f
/<section id="toc">/,/<\/section>/ {
if ( /lang="/ ) lang = gensub( ".*lang=\"([^\"]+)\".*", "\\1", 1 )
if ( /li/ ) {
fragment = gensub( ".*#([^\"]+).*", "\\1", 1 )
if (lang == "en") {
frags[fidx++] = fragment
}
x = idx[lang]++
text = gensub( ".*>([^<]+)</a.*", "\\1", 1 )
item[x, lang] = text
}
}
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (key in frags) {
print "<section>"
print "\t<section id=\"" frags[key] "\">"
print "\t\t<h2>"
print "\t\t\t<span lang=\"en\">" item[key, "en"] "</span>"
print "\t\t\t<span lang=\"cs\">" item[key, "cs"] "</span>"
print "\t\t</h2>"
print "\t</section>"
print "</section>"
print ""
}
}