-
awk takes the statement or description that describe the data, and then will give out the corresponding format of data, so it is a data-driven language, not a procedure-driven language.
-
sort -rnk 5means that sort by the 5th column, that is the percentage of thedfoutput part -
head -3means that output only the first 3 lines of the whole output -
awkcan use the regrex to select the output of some forms, like the below command will only print the/dev/sdlinedf -h | awk '/dev\/sd/ { print $6 "\t: " $5 }' -
for regex, the form is like
awk 'EXPRESSION { PROGRAM }' file(s) -
In order to output some comments, we can use BEGIN statement
ls -l | awk 'BEGIN { print "Files found:\n" } /\<[a|x].*\.conf$/ {print $9 }' -
In order to output some ending comments, we can use the END statement
ls -l /etc/ | awk '/\<[a|x].*\.conf$/ { print $9 } END { print "Can I do anything after that all" }' -
Use a comma
,is quite different when output the content of nameawk '{ print $1 $2 }' test # will use just single line awk '{ print $1, $2 }' test # will be seperated by content in test -
Use
OFSandORSto denote the separator in the list and the end separator in the lineawk 'BEGIN { OFS=";" ; ORS="\n--->\n" } { print $1, $2 }' test
the above will use the ; to seperate the items and newline with ---> to seperate the lines
-
Use
NRto denote the record number that is being processed, this variable is the built-in variable that we should not change and will use by default -
We can use the
=to define our own variable, this can be a string or numeric value.
- The most important thing here, for
awk, however, is that it is sued for processing the result of file or command, it is used for splitting the result text file into some fields that will be represented by$1,$2or other values, we can use this field to represent the data flow and select some information from the source, or add some of our own information to the source, so that it can be displayed in a friendly form