= 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;
}
i++;
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 -