awk Use Cases

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.