awk
The content of each line is called 记录
, and the string separated by special characters is called 字段
.
field reference
echo 'a : 1 : good ::::' >> awktest.txt
echo 'b : 2 : well ::::' >> awktest.txt
echo 'c : 3 : food ::::' >> awktest.txt
echo 'd : 4 : hood ::::' >> awktest.txt
echo 'e : 5 : bell ::::' >> awktest.txt
echo 'o : 6 : toll ::::' >> awktest.txt
echo '1 : a : good : h :::' >> awktest.txt
echo '2 : b : well : say :::' >> awktest.txt
echo '3 : c : food : gold :::' >> awktest.txt
Put the following content:
a : 1 : good ::::
b : 2 : well ::::
c : 3 : food ::::
d : 4 : hood ::::
e : 5 : bell ::::
o : 6 : toll ::::
1 : a : good : h :::
2 : b : well : say :::
3 : c : food : gold :::
write into awktest.txt
it.
awk
By default, spaces are used as field separators (column separators) for field separation. Field separation refers to splitting records into fields. awk '{ print $6}' awktest.txt
will output the sixth field of each line to the screen.
Well enough
awk 'pattern { action }
pattern { action }
pattern { action }' file
Action output.
awk '/^a/{ print $6}
/^3/{print $7}' awktest.txt
You can first output a
the sixth field in the record starting with , and then output 3
the seventh field in the record starting with , and use regular expressions here.
Options can be used -F
to modify the field separator. [[:space:]]
is a space, awk -F "[[:space:]]:[[:space:]]" '{ print $2}' awktest.txt
you can use 空格
+ :
+ 空格
as the field separator, and then output the second field in each record.
You can also use variables to record the line number, awk -F "[[:space:]]:[[:space:]]" '{ print x++,$3}' awktest.txt
you can use 空格
+ :
+ 空格
as the field separator, and then output the third field in each record, and write the line number minus 1 in front.
This article is a study note for Day 8 in August, and the content comes from Geek Time "100 Lectures on Linux Practical Skills" .