echo 'a : 1 : good : g' >> awkexpress.txt
echo 'b : 2 : well : w' >> awkexpress.txt
echo 'c : 3 : food : f' >> awkexpress.txt
echo 'd : 4 : hood : h' >> awkexpress.txt
echo 'e : 5 : bell : b' >> awkexpress.txt
echo 'o : 6 : toll : t' >> awkexpress.txt
echo '1 : a : good : g' >> awkexpress.txt
echo '2 : b : well : s' >> awkexpress.txt
echo '3 : c : food : f' >> awkexpress.txt
Put the following content:
a : 1 : good : g
b : 2 : well : w
c : 3 : food : f
d : 4 : hood : h
e : 5 : bell : b
o : 6 : toll : t
1 : a : good : g
2 : b : well : s
3 : c : food : f
write into awkexpress.txt
it.
assignment operator
=
Is awk
the assignment operation, 变量名 = 变量值
you can assign a value to the variable.
awk ' { var="string"; print var " # " $0 }' awkexpress.txt
Just string
assign it to var
a variable.
arithmetic operator
Arithmetic operators such as , , , and awk
can be used in . , using Add variable from to .+
-
*
/
%
awk '{ var = var + 1;print var " # " $0 }' awktest.txt
+
var
0
9
system variable
FS: field separator
OFS: output field separator
NF: number of fields, NF (Number: number; Field: field)
NR: the record number of each line, and the record is incremented when there are multiple files.
FNR: If there are multiple files, they are isolated from each other, and each file starts to grow again from 1
awk 'BEGIN{ FS=" : " }{ print $3,$4 }' awkexpress.txt
Use 空格
+ :
+ 空格
as the field separator. By default, OFS="空格"
the third field and the fourth field can be output to the screen.
awk 'BEGIN{ FS=" : " }{ print $3,$4 }' awkexpress.txt
It is equivalent to awk 'BEGIN{ FS=" : ";OFS=" " }{ print $3,$4 }' awkexpress.txt
.
awk 'BEGIN{ FS=" : ";OFS="/#"}{ print $3,$4}' awkexpress.txt
, put the output separated by 空格
+ :
+ to the screen.空格
第3个字段+“/#”+第4个字段
awk 'BEGIN{ FS=" : " }{ print $3,$4,"/#Field Number:" NF }' awkexpress.txt
, where NF
is the number of fields in each record.
awk 'BEGIN{ FS=" : " }{ print $3,$4,"/#Record Number:" NR }' awkexpress.txt
You can see the record number of each line NR
.
sed 's/g/g : wood/2' awkexpress.txt | awk 'BEGIN{ FS=" : ";OFS="===" }{ print $3,$4,"Field Number:" NF,"Record Number:" NR }'
It can be seen Record Number
that there are 5 in the two lines of 1 and 7 Field Number
, because sed
the command adds more to the end of these two lines : wood
.
awk 'BEGIN{ FS=" : " }{ print $3,$4,"/#Record Number:" NR }' awkexpress.txt awkexpress.txt
, you can see that the two files are uniformly numbered from 1, and the files are not isolated.
awk 'BEGIN{ FS=" : " }{ print $3,$4,"/#Record Number:" FNR }' awkexpress.txt awkexpress.txt
You can see that the two files are numbered with each other, starting from 1.
relational operator
<
, >
, <=
, >=
, ==
, !=
, ~
(match), !~
are all relational operators.
boolean operator
&&
, ||
, !
are Boolean operators. This article is a study note for Day 11 in August, and the content comes from "Linux Practical Skills 100 Lectures"
by Geek Time .