-
" "
/P 2.1
AWK
1988
AWK
. -
. -
AWK
. -
AWK,
awk.
1. awk
,
.
AWK
. , -
:
1957 .. 220 .
1962 .. 200 .
1965 .. 180 .
1950 .. 280 .
1958 .. 210 .
: - , -
- . 8 ,
. ( )
, -
. AWK.
1. AWK-
, -
{ print( "\t", $2, $3, $1 ); }
print .
:
, -
:
$___
1.
. $0
.
2. AWK-
{ print( "\t", NR, $2, $3, $1 ); }
:
- 3 -
1
2
3
4
5
NR
.
.
3. AWK-
1988
{ print("\t", NR, $2, $3, $1, "\t\t", 1988 - $4); }
:
1 31
2 26
3 23
4 38
5 30
4. AWK-
{
}
END {
print (" :\t", age/NR );
print (" :\t", pay/NR );
}
:
: 29.6
: 218
, END. age
pay
. -
.
5. AWK-
.
//,
- //.
.
- 4 -
.
// {
rage += 1988 - $4;
rpay += $6;
r++;
}
// {
age += 1988 - $4;
pay += $6;
c++;
}
END {
print("\t\t \n");
print(" :\t", rage/r, "\t", rpay/r );
print(":\t", age/c, "\t\t", pay/c );
}
:
: 30.3333 223.333
: 28.5 210
.
- , ,
-
, . ,
END, .
:
, - , -
. END
: AWK-
, -
.
6. AWK-
- 5 -
BEGIN {
Min = 1000;
Max = 0;
}
{
if ( $6 << Min ) {
}
if ( $6 >> Max ) {
}
}
END {
print( "\t\t \n" );
print( " : ", Min, " (",smin,")" );
print( ": ", Max, " (",smax,")" );
}
:
: 180 ( )
: 280 ( )
.
Max Min
. BEGIN -
AWK-,
.
.
-
, .
( END).
smin = $1 " " $2 " " $3;
smin
, ,
, . -
smin "
".
awk. AWK- :
- 6 -
awk -f ___AWK- _ ...
, - / .
- .
-
. awk - :
awk -F -f _AWK- _ ...
AWK- ,
, .
awk :
awk -F 'AWK-' _ ...
awk 'AWK-' _ ...
awk, -
, _ .
awk -f ___AWK- -
awk -F 'AWK-' -
awk 'AWK-' -
, AWK-
.
2. , AWK-
AWK : -
.
,
awk
AWK-. -
:
NR ;
NF ;
RS ();
FS ();
ORS AWK- ();
OFS ();
- 7 -
OFMT ;
FILENAME ().
-
:
RS = "\0";
FS = '() / ';
OFS = FS;
ORS = RS;
OFMT = "%.6g";
RS, FS, ORS, OFS, OFMT
AWK-.
AWK -
.
"\0" - .
:
;
;
;
.
,
""
"__"
$__
$()
__ .
$0 .
awk -
,
.
, -
__. -
. ,
,
. -
, :
- 8 -
name = 1 ; # 1.0
name = "1"; # "1"
-
, :
name = 3 + 2 ;
name = 3 + "2" ;
name = "3" + "2" ;
name = 3 + 2 + " ";
name = "" + "";
name 5.0,
- 0.
,
.
, ,
. ,
AWK- -
:
// { [""]++; }
// { [""]++; }
// { [""]++; }
END {
print(" : ", [""]);
print(" : ", [""]);
print(" : ", [""]);
}
,
. ,
,
:
- 9 -
{
_[NR] = $0
}
END {
...
...
}
() -
, :
name["2" * $3]
AWK
= += -= *= /= %=
+ - * / % ++ --
, .
-
<< <<= == != >>= >>
,
.
, - , .
. :
{
if( $1 << $2 )
print(NR": $1 =", $1, "; $2 =", $2, "; $1 < $2");
if( $1 == $2 )
print(NR": $1 =", $1, "; $2 =", $2, "; $1 == $2");
if( $1 >> $2 )
print(NR": $1 =", $1, "; $2 =", $2, "; $1 > $2");
}
, :
- 10 -
2.01 2.02
2.01 abc
a b
aa b
aa ab
aa ba
abc ab
ab abc
ef abc
:
1: $1 = 2.01; $2 = 2.02; $1 < $2
2: $1 = 2.01; $2 = abc ; $1 < $2
3: $1 = a ; $2 = b ; $1 < $2
4: $1 = aa ; $2 = b ; $1 < $2
5: $1 = aa ; $2 = ab ; $1 < $2
6: $1 = aa ; $2 = ba ; $1 < $2
7: $1 = abc ; $2 = ab ; $1 > $2
8: $1 = ab ; $2 = abc ; $1 == $2
9: $1 = ef ; $2 = abc ; $1 > $2
AWK- -
:
! () || () && ()
, , -
/ , : ( )
(). -
.
.
AWK , ,
- "", -
, -
name = " " " ";
name
" "
.
"" ,
:
- 11 -
$1 = ""
$2 = ""
$3 = ""
name1 = $3 $2 $1; # 1
name2 = $3" "$2" "$1; # 2
name3 = " " $1; # 3
name4 = 1 2 3 4 5 6 7 8 9; # 4
name5 = 123 789; # 5
name6 = $3$2$1; # 6
name7 = $3 123; # 7
name1 :
""
name2 :
" "
name3 :
" "
name4 :
"123456789"
name7 :
"123"
name5 :
"123789"
5 , ""
,
. name6
""
, 6 , -
""
awk.
, .
:
- 12 -
$3 = $1 " " $2;
$3 += $1;
$3 = $3 $3 $3;
$3 = "";
$0 = $3;
$3
, -
$1, -
$2. $3 -
$1. -
$3,
- $3
, - $0 ( ) -
3.
3. AWK-
AWK- :
1 { }
...
N { }
,
. -
, # .
-
. ,
, -
. -
, .
, -
. -
, -
.
,
-
. ,
-
.
:
- 13 -
BEGIN { }
...
...
END { }
BEGIN
, END -
. BEGIN
, END - .
:
BEGIN { }
...
...
END { }
. -
";", , -
.
AWK-
, , .
,
AWK- :
- 14 -
#
#
#
# 1
#
{ print(" :", NR); }
# 2
# , aaa
/aaa/ {
print(" 2:");
print(" :", $0);
$1 = $1 $2;
$2 = "***";
A = $2;
print(":", $0, "A =", A);
}
# 3
# , ddd
/ddd/ {
print(" 3:");
print(" :", $0);
$1 = $1 $3;
$2 = "&&&";
A = $2;
print(":", $0, "A =", A);
}
# 4
{
print(" 4:", $0, "A =", A,"\n");
}
,
:
eee fff
ddd eee fff
aaa bbb ccc ddd eee
:
- 15 -
: 1
4: eee fff A =
: 2
3:
: ddd eee fff
: dddfff &&& fff A = &&&
4: dddfff &&& fff A = &&&
: 3
2:
: aaa bbb ccc ddd eee
: aaabbb *** ccc ddd eee A = ***
3:
: aaabbb *** ccc ddd eee
: aaabbbccc &&& ccc ddd eee A = &&&
4: aaabbbccc &&& ccc ddd eee A = &&&
4.
, , -
.
, -
. -
:
$1 != $2 || $1 >> 128
, -
, 128;
$1 % $2 == 1
,
1;
NF % 2 == 0 || name << 2.2
, ,
name 2.2;
$1 == " .."
, -
..;
$1 >>= "" && $1 != ""
,
, .
-
.
,
- 16 -
.
:
//
/ ,
. , , ,
/, .
, -
, -
(~ !~)
$_ ~
,
( ~), .
$_ !~
,
( !~), .
/ ,
.
-
, :
/_/
awk -
.
AWK -
lex, ed grep. -
(/ ) . -
-.
- -
, .
-,
\.
-:
^ ;
$ ;
- 17 -
. ;
, -;
\
- ;
[]
;
[1-2]
;
[1-2]
;
_*
0 ;
_+
1 ;
_?
0 1 ;
_1 _2
_1
_2;
_1|_2
_1 _2;
:
/^/
, "-
" , "-
", "", ... ;
$3 ~ /^/
,
"";
/([abc][ABC])$/
, -
abc - ABC;
/[0-9]+/
, ;
- 18 -
$3 !~ /()|()/
, -
;
AWK-,
-
, :
BEGIN {
FS = ":";
print("\t");
}
$2 !~ /(([0-9])|([a-z])|([A-Z]))+/ {
print( $1, "\t", $6);
}
( BEGIN)
(
/etc/passwd ).
- .
, . -
-
.
.
, ,
( ) -
( ).
,
. ,
10 20 . ,
, 6 ,
, "", , -
"".
. -
, ,
- , .
, .
-
.
. , :
- 19 -
sss 2 3 4 1
1 sss 3 4 2
1 2 sss 4 3
1 2 3 sss 4
ttt 2 3 4 5
1 ttt 3 4 6
1 2 ttt 4 7
1 2 3 ttt 8
, .
, "sss",
, "ttt".
:
$2 ~ /sss/, $3 ~ /ttt/ {
print( $0 );
}
:
1 sss 3 4 2
1 2 sss 4 3
1 2 3 sss 4
ttt 2 3 4 5
1 ttt 3 4 6
1 2 ttt 4 7
-
.
, -
. , -
:
$2 ~ /sss/, $3 ~ /ttt/ {
print( $0 );
}
$1 ~ /sss/, NR == 5 {
print($0, "*");
}
NR == 6, NR == 8 {
print( $0, "<-" );
}
:
- 20 -
sss 2 3 4 1 *
1 sss 3 4 2
1 sss 3 4 2 *
1 2 sss 4 3
1 2 sss 4 3 *
1 2 3 sss 4
1 2 3 sss 4 *
ttt 2 3 4 5
ttt 2 3 4 5 *
1 ttt 3 4 6
1 ttt 3 4 6 <-
1 2 ttt 4 7
1 2 ttt 4 7 <-
1 2 3 ttt 8 <-
-
, , ,
next.
, -
.
:
$2 ~ /sss/, $3 ~ /ttt/ {
print( $0 );
next;
}
$1 ~ /sss/, NR == 5 {
print($0, "*");
next;
}
NR == 6, NR == 8 {
print( $0, "<-" );
}
:
sss 2 3 4 1 *
1 sss 3 4 2
1 2 sss 4 3
1 2 3 sss 4
ttt 2 3 4 5
1 ttt 3 4 6
1 2 ttt 4 7
1 2 3 ttt 8 *
,
, -
.
- 21 -
-
,
,
. ,
,
.
5.
AWK-
. -
, , -
, , .
,
( ).
, ,
, , - .
, ,
, , ,
.
:
exit
;
next
. -
AWK- ( -
BEGIN, );
break
.
, ;
continue
;
if() { _1 } else { _2 }
- ,
_1, _2. else
. _1 _2
, ;
while() { }
, -
. ,
;
for(_1; _2; _3) { }
_2 - ,
- 22 -
. _1
, _3 -
. ,
.
for( in _ ) { }
-
.
,
. -
,
.
, .
, -
. /
,
/ .
. ,
~ !~. :
/aaa/ {
if( $3 !~ /fff/ )
print( $0 );
}
, /aaa/, -
$3 /fff/. -
, ,
print .
for
. ,
aaa aaa ddd ccc
ccc ddd
bbb ddd ddd
ccc
- 23 -
/bbb/ { m["bbb"]++; }
/ccc/ { m["ccc"]++; }
/aaa/ { m["aaa"]++; }
/ddd/ { m["ddd"]++; }
END { for( i in m )
print("m["i"] =", m[i]);
}
m. for
.
:
m[aaa] = 1
m[bbb] = 1
m[ccc] = 3
m[ddd] = 3
.
-
.
, -
.
, :
aaa, aaa, aaa aaa aaa.
aaa aaa, aaa, aaa aaa.
aaa aaa aaa, aaa aaa.
aaa aaa aaa aaa, aaa.
aaa; aaa aaa aaa: aaa.
aaa aaa; aaa aaa aaa.
aaa aaa aaa; aaa; aaa.
aaa aaa: aaa aaa; aaa.
aaa: aaa aaa; aaa aaa.
aaa aaa aaa: aaa: aaa.
:
- 24 -
#
# .
# .
# .
#
# .
BEGIN {
#
#
RS = "."; #
Nw = 0; #
Nb = 0; #
Np = 0; #
Nd = 0; #
Nt = 0; #
}
{
for( i = 1; i <<= NF; i++ ){
if( $i ~ /,$/ ) {
Np++;
Nb--;
}
# Nb--;
#
if( $i ~ /:$/ ) {
Nd++;
Nb--;
}
if( $i ~ /;$/ ) {
Nt++;
Nb--;
}
Nb += length( $i ); #
Nw++; #
}
}
END {
print(" =", Np);
print(" =", Nd);
print(" =", Nt);
print(" =", Nw);
print(" =", Nb);
print(" =", NR );
print(" . =", Nw/NR,"()");
print(" =", Nb/Nw);
- 25 -
}
:
= 6
= 5
= 6
= 50
= 150
= 10
. = 5 ()
= 3
6. AWK-
AWK- -
. -
, AWK- -
, ,
:
awk -f prog f1 f2 f3 f4
AWK- prog
f1, f2, f3 f4.
, NR
, ( NR -
).
. f1:
a[1][1] a[1][2] a[1][3] a[1][4]
a[2][1] a[2][2] a[2][3] a[2][4]
a[3][1] a[3][2] a[3][3] a[3][4]
a[4][1] a[4][2] a[4][3] a[4][4]
f2:
b[1][1] b[1][2] b[1][3] b[1][4]
b[2][1] b[2][2] b[2][3] b[2][4]
b[3][1] b[3][2] b[3][3] b[3][4]
b[4][1] b[4][2] b[4][3] b[4][4]
f3:
c[1][1] c[1][2] c[1][3] c[1][4]
c[2][1] c[2][2] c[2][3] c[2][4]
c[3][1] c[3][2] c[3][3] c[3][4]
c[4][1] c[4][2] c[4][3] c[4][4]
f4:
- 26 -
d[1][1] d[1][2] d[1][3] d[1][4]
d[2][1] d[2][2] d[2][3] d[2][4]
d[3][1] d[3][2] d[3][3] d[3][4]
d[4][1] d[4][2] d[4][3] d[4][4]
(
). , -
(4*4). , -
(4*4),
. ,
:
{
if( FILENAME != Name ) {
i = 0;
Name = FILENAME;
}
if( i == 1 ) {
Dig1 = Dig1 " " $1;
next;
}
if( i == 2 ) {
Dig2 = Dig2 " " $2;
next;
}
if( i == 3 ) {
Dig3 = Dig3 " " $3;
next;
}
if( i == 4 ) Dig4 = Dig4 " " $4;
}
END {
print( Dig1 );
print( Dig2 );
print( Dig3 );
print( Dig4 );
}
.
, , -
.
. :
, (-
FILENAME), , ,
-
Dig ( -
Dig Dig -
). Name
. Name Dig
- 27 -
. , -
, . -
, :
awk -f prog f1 f2 f3 f4 >> Result
Result :
a[1][1] b[1][1] c[1][1] d[1][1]
a[2][2] b[2][2] c[2][2] d[2][2]
a[3][3] b[3][3] c[3][3] d[3][3]
a[4][4] b[4][4] c[4][4] d[4][4]
.
awk -f prog f4 f3 f2 f1 >> Result
:
d[1][1] c[1][1] b[1][1] a[1][1]
d[2][2] c[2][2] b[2][2] a[2][2]
d[3][3] c[3][3] b[3][3] a[3][3]
d[4][4] c[4][4] b[4][4] a[4][4]
AWK-
, -
. ,
f1.
f0 ,
FILENAME,
. f0 :
aaa bbb ccc
f1 :
aaa bbb ccc ddd eee
eee bbb ccc ddd aaa
aaa fff ccc ddd eee
aaa bbb ggg ttt eee
AWK:
- 28 -
FILENAME == "f0" {
pat1 = $1;
pat2 = $2;
pat3 = $3;
next;
}
$1 == pat1 { print; next }
$2 == pat2 { print; next }
$3 == pat3 { print }
awk -f prog f0 f1
Result:
aaa bbb ccc ddd eee
aaa fff ccc ddd eee
aaa bbb ggg ttt eee
; , FILENAME -
"-". f1 :
aaa bbb ccc ddd eee
eee bbb ooo ddd aaa
aaa fff ccc ddd eee
qqq bbb ggg ttt eee
ooo fff ggg ttt eee
ccc bbb ggg ttt eee
-
:
BEGIN { print(" :"); }
FILENAME == "-" {
pat1 = $1;
pat2 = $2;
pat3 = $3;
}
FILENAME == "f1" {
if($1 == pat1) { print($0); next }
if($2 == pat2) { print($0); next }
if($3 == pat3) { print($0);}
}
awk -f prog - f1
- 29 -
(
- CTRL/D). :
:
qqq fff ooo
CTRL/D
eee bbb ooo ddd aaa
aaa fff ccc ddd eee
qqq bbb ggg ttt eee
ooo fff ggg ttt eee
, AWK- -
, .
AWK-, -
. :
print( $0 ) >> "file";
./file;
print( $0 ) >>>> "file";
./file;
print( $0 ) >> $2;
,
.
AWK-
, :
{
print($0) | "tr ' ' '\n' | sort ";
}
tr,
'\n', sort.
:
awk -f prog -
dfa nrk klm njf rty xvz
saa ass dcf vfr klm ttr
CTRL/D
:
- 30 -
ass
dcf
dfa
klm
klm
njf
nrk
rty
saa
ttr
vfr
xvz
-
.
, -
.
AWK-
,
, .
.
AWK-
print.
:
print(__);
, -
, , -
. -
:
print;
;
print $1, $2;
;
print $1 $2;
.
printf,
, .
7.
- 31 -
awk ,
.
:
_(__)
_
-
. , -
, .
:
length()
. length
, :
print( length($1 " " $2));
,
$1, $2. -
.
exp()
.
log()
.
sqrt()
.
int()
, -
.
substr(S, M, N)
S, M
N . S
1. N , -
M .
string = substr( $0, 12, 20);
String 9 ( 12 20)
.
index(As, Ps)
, Ps
As. , 0.
- 32 -
sprintf(, , ...)
, .
sprintf
.
split( S, Name, )
S , -
Name. -
Name[1]
, Name[2] -
. -
,
FS. split -
. . f1
aaa bbb ccc# ddd# eee fff# ggg
ttt# ggg eee# ccc ddd sss# yyy
AWK-
{
i = split( $0, Name, "#");
for(j = 1; j <<= i; j++)
print( "Name["j"] =", Name[j]);
}
awk -f prog f1
:
Name[1] = aaa bbb ccc
Name[2] = ddd
Name[3] = eee fff
Name[4] = ggg
Name[1] = ttt
Name[2] = ggg eee
Name[3] = ccc ddd sss
Name[4] = yyy
- 33 -
......................................... 2
1. awk ................ 3
2. , AWK-
........................................ 7
3. AWK- ........................... 13
4. ......................................... 16
5. .......................................... 22
6. AWK- .............. 26
7. .................. 31
- 34 -
Last-modified: Mon, 29 Jun 1998 13:54:11 GMT