AWK
-
" "
/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-
{
age += 1988 - $4;
pay += $6;
}
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 ) {
Min = $6;
smin = $1 " " $2 " " $3;
}
if ( $6 >> Max ) {
Max = $6;
smax = $1 " " $2 " " $3;
}
}
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