1. Imitate the behavior of WC, count the number of characters, number of lines, and number of words of a file in a text file.
awk ' {numofchar+=length ($); Numofword+=nf}end{print Numofchar "" Numofword "" NR} ' file
2. In the above example, there is a question, the number of characters in the statistics contains a space, then the number of characters that do not contain a space how to calculate?
awk ' {for (i=1; i<=nf; i++) numofchar+=length ($i)}end{print Numofchar} ' file
How to reference an external variable in 3.awk
Name= "Pola"
echo "" | awk ' {print ' awk awk awk "' $name '"} '
Double quotes in single quotes $var.
In 4.awk numeric calculations, how do you recognize a string as a series of numbers?
0 The starting string is recognized as 8, and the string starting with 0x is identified by 16, in addition to the decimal. These are ideal situations, and for other cases, match the longest prefix, that is, how many matches.
echo "0x12" | awk ' {printf ("%d\n", Strtonum ($));} '
5. How to complete the words at the beginning of the dot (.)?
Readelf an. o file will have the contents of the. Text section, if you want to find the line containing the word ". Text"? Note that this "word" is the beginning of a point, and then followed by a regular word, in fact, this is not a word! So when we use grep "\b\.text\b" to sift through such a line, we can't even sift it out, why? \b is a meta-character and is also a 0-wide assertion that you can think it will match in such a strange position: two characters in the middle! It's going to match (numbers, letters, underscores) with the middle position (space, punctuation, line break). Now, our. Text, because the left side of the point is a space, so the space between the DOT does not match on the \b, so use \b can not search out a word!
6. Read the offset of the code snippet in the file in an. o file
Readelf-s WRITE.O | awk '/\s\.text/{sub (/\[.*\]/, "", $ A); Print $4} '
"Part of the pattern in awk and sub-functions such as sub, the regular expression is placed in//"
awk Use Cases