Professional Documents
Culture Documents
3/21/14
linux: awk
UnixForum
Linux
( )
.
.
mobi-sklad.com
.
. .
videonabludenie.mediapalace.ru
- .
e -mail:
1.
,
awk - , .
.
,
. Aho,
Weinberger Kernighan (, Brian Kernighan), ,
1977 .
C, awk.
Linux , ,
, , , ,
.
Arnold Robbins awk.
2. awk?
awk - / . awk
Larry Wall, Perl.
awk
sed.
awk, Perl,
. sed, awk
,
, .
awk
. Debian
, awk ,
, aptitude.
:
$ dpkg -l | awk ' {print $2} ' > installed
,
rpm's -qa, , . ,
dpkg -l
, ,
.
3.
, , awk,
, - : awk 'condit ion { act ion }'.
, , ,
, vim, (, grep,
, , ,
), :
$ dpkg -l | awk ' /'vim'/ {print $2} '
, "vim"
. , awk . "vim" "lib",
1300 . ,
, awk .
, ,
. ,
awk. ,
, GNU awk ( ,
). - ,
bash, ksh sh, (t)csh.
4.
,
s tdin
,
s tdin
,
http://rus-linux.net/MyLDP/consol/awk.html
1/1
linux: awk
s e d, awk
f
,
,
.
program,
inputfile
"He llo, world"
awk
,
,
EOF
#! /bin/awk -f
BEGIN { print "Hello, world!" }
awk
"He llo, world!"
(
chmod
)
awk
null,
80
,
(NF
Numbe r of Fie lds )
0
100
, NR
Numbe r
of Rows
,
"patte rn",
s tdin
3/21/14
2/1
linux: awk
3/21/14
j.
,
j.
awk.
"bcd"
inve ntory
,
10.
inve ntory,
,
.
OFS - Output
Fie ld Se parator
(
)
"a::c:d"
"4",
,
,
.
;
,
$4
( )
$6
( )
(
$5),
"a::c:d::ne w"
"6".
( )
.
FS=[ ]
"a".
\\
"John Doe
1234 Unknown
Ave .
Doe ville , MA",
3/1
linux: awk
print ""
}
,
.
3/21/14
,
:
awk 'BEGIN {
> OFMT = "%.0f" # print numbers as \
integers (rounds)
> print 17.23, 17.54 }'
17
18 ,
(Output
ForMaT)
.
awk 'BEGIN {
> msg = "Dont Panic!"
> printf "%s\n", msg
>} '
printf
, C.
10
,
,
.
,
"phone lis t".
http://rus-linux.net/MyLDP/consol/awk.html
,
$1, ,
.
foo bar.
(
C)
s tart s top.
awk '
> BEGIN { print "Analysis of \"foo\"" }
> /foo/ { ++n }
> END { print "\"foo\" appears", n,\
"times." }' inputfile
BEGIN
END
,
.
.
awk, C,
?:.
4/1
linux: awk
3/21/14
else
print "x is odd"
awk '{ i = 1
while (i <= 3) {
print $i
i++
}
}' inputfile
,
.
,
.
BEGIN {
if (("date" | getline date_now) <= 0) {
print "Can't get system date" > \
"/dev/stderr"
exit 1
}
print "current date is", date_now
close("date")
}
,
0,
,
-
. :
awk 'BEGIN {
> for (i = 0; i < ARGC; i++)
> print ARGV[i]
> }' file1 file2
awk
file 1 file 2
for (i in frequencies)
delete frequencies[i]
foo[4] = ""
if (4 in foo)
print "This is printed, even though foo[4] \
is empty"
awk-
ctime ()
C.
awk.
Cliff.
cat apache-anon-noadmin.log | \
awk 'function ri(n) \
{ return int(n*rand()); } \
BEGIN { srand(); } { if (! \
($1 in randip)) { \
randip[$1] = sprintf("%d.%d.%d.%d", \
ri(255), ri(255)\
, ri(255), ri(255)); } \
$1 = randip[$1]; print $0 }'
Apache (IP
)
5.
, awk
. ,
awk, (
), , awk.
,
.
.
http://rus-linux.net/MyLDP/consol/awk.html
5/1