Awk command testing (Chapter 4 awk)

Source: Internet
Author: User

Where is awk holy?

In short, awk is a programming language tool used to process text. The language of the awk utility is similar to the shell programming language in many ways, although awk has its own syntax. When awk was initially created, it was intended for text processing, and the basis of this language is to execute a series of commands as long as there is a pattern match in the input data. This utility scans each row in the file (this is fully validated in subsequent tests) and finds the pattern that matches the content given in the command line. If the Matching content is found, perform the next programming step. If no matching content is found, process the next row.

Although operations may be complex, the command syntax is always:

Awk '{Pattern + action}' // pattern is a regular expression

Specifically, pattern indicates the content that awk looks for in the data, and action is a series of commands executed when matching content is found. Curly braces ({}) do not always appear in the program, but they are used to group A series of commands according to a specific mode.

The comments starting with "//" are personal comprehension comments. In shell, the annotation identifier is.

The usage of the file grade is as follows:

[Root @ Biao linuxtest] # Cat grade
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
P. Bunny 02/99 48 yellow 12 35 28
J. Troll 07/99 4842 brown-3 12 26 26
L. tansley 05/99 4712 brown-2 12 30 28

[Root @ Biao linuxtest] # awk 'in in {print "name/n ------"} {print $1} end {print "end-of-Report"} 'grade
Name
------
M. tansley
J. Lulu
P. Bunny
J. troll
L. tansley
End-of-Report
[Root @ Biao linuxtest] # awk '$3 ~ /48/{print $0} 'grade
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
P. Bunny 02/99 48 yellow 12 35 28
J. Troll 07/99 4842 brown-3 12 26 26
[Root @ Biao linuxtest] # awk '$3 = "48" {print $0}' grade
P. Bunny 02/99 48 yellow 12 35 28
[Root @ Biao linuxtest] # awk '$4 !~ /Brown/{print $0} 'grade
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
P. Bunny 02/99 48 yellow 12 35 28
[Root @ Biao linuxtest] # awk '$4! = "Brown-2" {print $0} 'grade
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
P. Bunny 02/99 48 yellow 12 35 28
J. Troll 07/99 4842 brown-3 12 26 26
[Root @ Biao linuxtest] #

P10 // indicates the number of PPT pages given by the instructor.
[Root @ Biao linuxtest] # awk '/[Gg] reen/'grade
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
[Root @ Biao linuxtest] # awk '$1 ~ /^... A/'grade
M. tansley 05/99 48311 green 8 40 44
L. tansley 05/99 4712 brown-2 12 30 28
[Root @ Biao linuxtest] # awk '$1 = "P. Bunny" & $4 = "yellow" {print $0}' grade
P. Bunny 02/99 48 yellow 12 35 28
[Root @ Biao linuxtest] # awk '$1 = "P. Bunny" | $4 = "yellow" {print $0}' grade
P. Bunny 02/99 48 yellow 12 35 28
[Root @ Biao linuxtest] #

P10
The default field delimiter is space, which can be specified by-f.

P11 awk built-in Variables
Number of NF browsing records
Number of records read by NR

P12
[Root @ Biao linuxtest] # awk 'end {print Nr} 'grade
5
[Root @ Biao linuxtest] # awk '{print NF, NR, $0} end {print filename}' grade // print the number of records for this row
7 1 M. tansley 05/99 48311 green 8 40 44
7 2 J. Lulu 06/99 48317 green 9 24 26
7 3 p. Bunny 02/99 48 yellow 12 35 28
7 4 J. Troll 07/99 4842 brown-3 12 26
7 5 L. tansley 05/99 4712 brown-2 12 30 28
Grade
[Root @ Biao linuxtest] # echo $ PWD | awk-F/'{print $ NF }'
Linuxtest

P13 modify domain
[Root @ Biao linuxtest] # awk '{if ($1 = "M. tansley ") $6 = $6-1; print $1, $6, $7} 'grade
M. tansley 39 44
J. Lulu 24 26
P. Bunny 35 28
J. Troll 26 26
L. tansley 30 28
[Root @ Biao linuxtest] # awk '{if ($1 = "M. tansley ") {$6 = $6-1; print $1, $6, $7} 'grade
M. tansley 39 44
[Root @ Biao linuxtest] #

P14 Print name tab difference first. If the value of column 6th is smaller than the value of column 7th, create column 8th = column 7th-column 6th print column 1st and column 8th
[Root @ Biao linuxtest] # awk 'in in {print "name/t difference"} {if ($6 <$7) {$8 = $7-$6; print $1, $8} 'grade
Name difference
M. tansley 4
J. Lulu 2
[Root @ Biao linuxtest] #

P15/^ [^ d]/match rows not starting with d
[Root @ Biao linuxtest] # awk '(TOT + = $6); end {print "club student total points:" tot}' grade
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
P. Bunny 02/99 48 yellow 12 35 28
J. Troll 07/99 4842 brown-3 12 26 26
L. tansley 05/99 4712 brown-2 12 30 28
Club student total points: 155
[Root @ Biao linuxtest] # ls-L | awk '/^ [^ d]/{print $8 usd/t "$5} {tot + = $5} end {print" total KB: "tot }'

PM 2545
2230
16: 08 527360
PM 184
PM 0
Total KB: 532319

P18 built-in string functions
[Root @ Biao linuxtest] # awk 'in in {print index ("bunny", "NY")} 'grade
4 // return the latter's first position in the former
[Root @ Biao linuxtest] # awk '$1 = "J. troll" {print length ($1) "" $1}' grade
7 J. Troll // print the length of $1
[Root @ Biao linuxtest] # awk 'in in {print match ("ABCD", "")}'
1 // determine whether the former contains the latter
[Root @ Biao linuxtest] #

P19
/B backspace key/T Tab key
/F go to the paper form/DDD octal value
/N New Line/C any other special characters, such as // is the backslash symbol
/R press ENTER
Awk 'in in {print "/nmay/tday/n/nmay/T/104/141/171 "}'
[Root @ Biao linuxtest] # awk 'in in {print "/nmay/tday/n/nmay/T/104/141/171 "}'

May Day

May Day
[Root @ Biao linuxtest] #

P21-output function printf
[Root @ Biao linuxtest] # echo "65" | awk '{printf "% C/N", $0}' // C is an ascii code
A
[Root @ Biao linuxtest] # echo "65" | awk '{printf "% F/N", $0}' // F is a floating point
65.000000
[Root @ Biao linuxtest] # awk '{printf "%-15 S % s/n", $1, $3} 'grade //-the alignment S is a string and does not quite understand this statement
M. tansley 48311
J. Lulu 48317
P. Bunny 48
J. Troll 4842
L. tansley 4712

P22 writes commands to files
[Root @ Biao linuxtest] # Cat awkfile
/^ Mary/{print "Hello Mary! "} // If Hello Mary is printed starting with Mary, then 1 2 3 columns are printed. Otherwise, only 1 2 3 columns are printed.
{Print $1, $2, $3}
[Root @ Biao linuxtest] # awk-F awkfile EP
Tom Jones 4424
Hello Mary!
Mary Adams 5346
Sally Chang 1654
Billy Black 1683
[Root @ Biao linuxtest] #

P23 script file
[Root @ Biao linuxtest] # Cat grade // view the content of the grade File
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
P. Bunny 02/99 48 yellow 12 35 28
J. Troll 07/99 4842 brown-3 12 26 26
L. tansley 05/99 4712 brown-2 12 30 28
[Root @ Biao linuxtest] # Cat tot // view the content of the tot File
#! /Bin/awk-F

# Print a header first
Begin {
Print "Student date no. grade age points Max"
Print "name joined gained point availabe"
Print "============================================ ==================================="
}

# Let's add the scores of points gained
(TOT + = $6)

End {
Print "club student total points:" tot
Print "average club student points:" TOT/NR
}

[Root @ Biao linuxtest] #./tot grade // execute the script
Student date no. grade age points Max
Name joined gained point availabe
========================================================== ==============================
M. tansley 05/99 48311 green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
P. Bunny 02/99 48 yellow 12 35 28
J. Troll 07/99 4842 brown-3 12 26 26
L. tansley 05/99 4712 brown-2 12 30 28
Club student total points: 155
Average club student points: 31
[Root @ Biao linuxtest] #

I just tested the commands on the instructor's courseware and basically understood what it meant! Now in the contact stage, many things need more systematic understanding. In addition, if you seldom use these commands in your life and study, you will forget it sooner or later! For all awk commands and their powerful applications, this is just the tip of the iceberg.

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.