:

info.krc.karelia.ru

win -:|:- koi -:|:- iso -:|:- dos -:|:- mac

Start -:|:- -:|:-

awk
( )


() .


www.nevod.ru


Ϸ˺ º Ȱ awk
(Aho, Weinberg, Kernigan)

awk - .
- . "awk"
"shell".

1. ú awk-

(), :

{}
{}
. . .

:

{} - .
- .


,
";" .

( shell "#.........").

:
"f-awk"
( --- ):

.. 1980 50
.. 1979 40
.. 1979 40
.. 1970 60

awk '{print}' f-awk # ;
echo
awk '// {print}' f-awk # , "".
echo
awk '// {}' f-awk # , ""
echo
awk '// {print("!")}' f-awk

:
.. 1980 50
.. 1979 40
.. 1979 40
.. 1970 60

.. 1979 40
.. 1970 60

.. 1979 40
.. 1970 60

!
!

("BEGIN"-
"END" - ""):

BEGIN {}
{}
{}
. . .
END {}


2. ˷ awk

:

1) awk [-Fc] 'prog.awk' [files]

, (
" ' ") ,
"-Fc" - , "c"
"file" - , -
. ( ).

cat f-awk | awk '// {print}'



awk '// {print}' < f-awk

,

awk '// {print}' f-awk

"-Fc" :

awk '// {print($2)}' f-awk
awk -F0 '// {print($2)}' f-awk

:

..
..

6

"awk" (
"$2") , "". (,
"$0" ).
, "-F"
"0", ..
, :

.. 1979 40
---------------------^--------------------
1- 2- ()

.. 1970 60
-----------------^---^--------------------
1- 2- 3- ()


2) awk [-Fc] -f prog.awk [files]

"-f" , awk-
, (
".awk"
).

3. awk-˵

awk :
.
awk
awk-.
: :
NR ;
NF ;
RS ; "\0"
FS ; /
ORS ; RS
OFS ; FS
OFMT c; "%.6g"
FILENAME .


. "0" (
!).
:
- ,
- ,
- ,
- .

awk ,
:
- (), ;
- "+", .

:

awk '{a = $3 $4; print a}' f-awk
awk '{a = $3+$4; print a}' f-awk

:
198050
197940
197940
197060
2030
2019
2019
2030

awk '{}
END {a = 2 + 2 ; print a}' < f-awk
awk '{}
END {a = 2 + "2" ; print a}' < f-awk
awk '{}
END {a = 2 + "" ; print a}' < f-awk
awk '{}
END {a = ""+"" ; print a}' < f-awk
awk '{}
END {a = 2.2 + 2.000 ; print a}' < f-awk

:
4
4
2
0
4.2

',
. -
. , ..
, , :
day [Jan][31] = Mon
day [Feb][01] = Tue
day [Feb][02] = Wed

,
( "p-1"):
John 100
Mary 200
Mary 200
John 100
John 300

awk '{sum[$1] += $2; print $1 sum[$1]} ' < p-1

( ):
John100
Mary200
Mary400
John200
John500

:
=, +=, -=, *=, /=, %=, +, /, %, ++, --.

, , - :
<, <=, ==, !=, >=, >

:
!, ||, &&

"" - .


4. awk-

1) awk '{print ($2, $3)}' f-awk

:
.. 1980
.. 1979
.. 1979
.. 1970

2) awk '// {print ($2, $3)}' f-awk

:
.. 1979
.. 1970

3) awk '// {print ($1, 2000 - $3)}' f-awk

:
21
30

4) awk '{ s = s + $4}
END {print (" :" s)
print (" :" s/NR)}' f-awk

:
:190
:47.5

5) awk '{ s += $4 }
{print("NR="NR, "NF="NF)}
END {print ("FILENAME=" FILENAME)
print (" " $4 "\"\" \
)")
print (" :" s)
print (" :" s/NR)}' f-awk

:
NR=1 NF=4
NR=2 NF=4
NR=3 NF=4
NR=4 NF=4
FILENAME=f-awk
"" )
:190
:47.5


5. ¾

"" ,
"", ,
.
ǰ. "{"
.
:
1) ;
2) ;
3) .

:
1) $3 != $4 && $3 > 1970
$3 % 2 == 1
$1=="" - , , .

2) /ab/ /a b/, / ab/ /ab /
N ^ -
N !^ -

:
awk '$3~0 {print} ' < f-awk
echo
awk '$3!~0 {print} ' < f-awk

.. 1980 50
.. 1970 60

.. 1979 40
.. 1979 40

3)
, :
/^a/ - "a"
/a$/ - "a"
\+ -
[abc] - "a", "b" "c"
[a-] -
* - 0
+ - 1
? - 0 1
ab|cd - "ab" "cd"

:
awk ' $3~/(7[0-9])$/ {print} ' f-awk

:
.. 1979 40
.. 1979 40
.. 1970 60

70- (7
).

6. ɵ


1) awk '$1=="" {print} ' f-awk

:
.. 1980 50

2) awk '$4/2==30 {print} ' f-awk

:
.. 1970 60

3) awk '$3 != $4 && $3 > 1970 {print} ' f-awk

:
.. 1980 50
.. 1979 40
.. 1979 40

4) awk '$1~/$/ {print} ' f-awk

:
.. 1980 50

5) awk '/^|/ {print} ' f-awk

:
.. 1980 50
.. 1979 40
.. 1970 60

6) awk '/1980/,/1979/ {print} ' f-awk

:
.. 1980 50
.. 1979 40


7. ²

awk :
1) ;
2)
3) ;
4) .

¾ ÿ



exit - ;
next - ,
awk-;
break - ;
continue - ;



if () {} [else {}]
while () {}
for (; ; ) {}
for ( in _) {}


.
.
.



1) awk ' $4~/40/ {if($3<=1980) {print(": " $1 )
M["40"]++}}
$4~/50/ {M["50"]++}
END {for(i in M)
{print(" i =" i " M[" i "]=" M[i])}} ' f-awk

:
:
:
i =40 M[40]=2
i =50 M[50]=1

2) awk ' BEGIN {ORS = " "}
{ for(k=NF; k>0; --k) {print $k}
{print RS}
} ' f-awk |
sed 's/^ //'

:
50 1980 ..
40 1979 ..
40 1979 ..
60 1970 ..

,
( "for"),
-
,
"print RS"
. "sed" ,
.
.

awk '{ for(k=NF; k>0; --k) {print $k}}' f-awk


:

50
1980
..

40
1979
..

40
1979
..

60
1970
..


, ";" , ..
, "print $k"

awk '{ for(k=NF; k>0; --k); {print $k}}' f-awk



.. 1980 50
.. 1979 40
.. 1979 40
.. 1970 60

"$k"
"0", "$0" - (!),
"print $k" .


8. ˲

awk
.
:

awk [-Fc] 'prog.awk' [file ...]
awk [-Fc] -f prog.awk [file ...]
^
.
"" awk
.

"f0" :

60

awk- "prim.awk" :

FILENAME == "f0" { # "f0"
w1 = $2 #
w2 = $1 # w1 - , w2 - 60
}
$1 == w1 { print (": "$1)} #
$4 == w2 { print (": " $4)} #
#



awk -f prim.awk f0 f-awk

:

:
: 60

"f0"
"w1", - "w2".
"f-awk".

"f-awk":

FILENAME == "f0" {
w1 = $2
w2 = $1
next
}
{ print (": "$1); next}
$4 == w2 { print (": " $4)}

:
:
:
:
:

"next",
:

: 60

("f0").
"next",
:

: 60

,
" { print (": "$1)}"
"f-awk", "next"

$4 == w2 { print (": " $4)}

.
:

awk -f prim.awk f0 f-awk

"prim.awk" :

BEGIN { print (" : ")}
FILENAME == "-" {
w1 = $2
w2 = $1
next
}
$1 == w1 { print (": "$1); next}
$4 == w2 { print (": " $4)}


9. ˵ ýƸ

:
sin (expr) expr
cos (expr) expr
exp (expr) expr
log (expr) expr
sqrt (expr) expr
int (expr)
length (s) s
printf (fmt, ...) ( )
fmt.
substr (s, m, n) n s,
m.
getline () .
0 - , 1.
index (s1, s2) ,
s1 s2, 0.
split (s, M, c) s M
c ( FS=" ");
.

.

1) awk ' BEGIN {FS = "."; a=0}
length ($1) > 8 {print (length ($1), $0);
a++
}
END {print (" : " a) }' f-awk

:
9 .. 1979 40
10 .. 1970 60
: 2

".",
8-, "length ($1)"
"$0".

2) awk '{i=split($0, Name, ".");
for (j=1; j<=i; j++)
print ("Name[" j "]=" Name[j])
}' f-awk

:
Name[1]=
Name[2]=
Name[3]= 1980 50
Name[1]=
Name[2]=
Name[3]= 1979 40
Name[1]=
Name[2]=
Name[3]= 1979 40
Name[1]=
Name[2]=
Name[3]= 1970 60

3) awk '{print (length)}' f-awk

:
22
22
22
22

,
.

4) awk '{printf "%7.2f %s\n", NR, $0}' f-awk
echo
awk '{printf "\t%s %s \n", NR, $0}' f-awk

:
1.00 .. 1980 50
2.00 .. 1979 40
3.00 .. 1979 40
4.00 .. 1970 60

1 .. 1980 50
2 .. 1979 40
3 .. 1979 40
4 .. 1970 60

 

Rambler's Top100 Service