Awk expression for Linux learning

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.txtit.
insert image description here

assignment operator

=Is awkthe assignment operation, 变量名 = 变量值you can assign a value to the variable.
awk ' { var="string"; print var " # " $0 }' awkexpress.txtJust stringassign it to vara variable.
insert image description here

arithmetic operator

Arithmetic operators such as , , , and awkcan be used in . , using Add variable from to .+-*/%
awk '{ var = var + 1;print var " # " $0 }' awktest.txt+var09
insert image description here

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.txtUse 空格+ :+ 空格as the field separator. By default, OFS="空格"the third field and the fourth field can be output to the screen.
insert image description here
awk 'BEGIN{ FS=" : " }{ print $3,$4 }' awkexpress.txtIt is equivalent to awk 'BEGIN{ FS=" : ";OFS=" " }{ print $3,$4 }' awkexpress.txt.
insert image description here
awk 'BEGIN{ FS=" : ";OFS="/#"}{ print $3,$4}' awkexpress.txt, put the output separated by 空格+ :+ to the screen.空格第3个字段+“/#”+第4个字段
insert image description here

awk 'BEGIN{ FS=" : " }{ print $3,$4,"/#Field Number:" NF }' awkexpress.txt, where NFis the number of fields in each record.
insert image description here

awk 'BEGIN{ FS=" : " }{ print $3,$4,"/#Record Number:" NR }' awkexpress.txtYou can see the record number of each line NR.
insert image description here

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 Numberthat there are 5 in the two lines of 1 and 7 Field Number, because sedthe command adds more to the end of these two lines : wood.
insert image description here

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.txtYou can see that the two files are numbered with each other, starting from 1.

insert image description here

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 .

Guess you like

Origin blog.csdn.net/qq_42108074/article/details/132197468