Shell script-awk learning Notes

Source: Internet
Author: User
M. Tansley05/9948311Green84044J. Lulu06/9948317green92426P. Bunny02/9948Yellow123528J. Troll07/994842Brown-31226...

Shell script learning Note 9-awk

# Cat grade.txt
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

Awk '{print $1, $4}' grade.txt
Print report header
Awk 'In in {print "Name \ t Belt \ n ------------------"} {print $1 "\ t" $4} 'grade.txt
 
End of printed information
# Awk 'In in {print "Name \ t \ n ------" }{ print $1} END {print "-- end of report --"} 'grade.txt

Name
------
M. Tansley
J. Lulu
P. Bunny
J. Troll
L. Tansley
-- End of report --

Regular expression and operation in awk
1) metacharacters \ ^ $. [] | () * +?
2) conditional operators <<==! == ~ Match regular expression !~ Do not match regular expression

Expression

Match to match a domain number with a regular expression. use '~ 'Followed by the regular expression, you can also use

In the if statement. awk, conditions after if are enclosed ().
# Awk '{if ($4 ~ /Brown/) print $0} 'grade.txt
J. Troll 07/99 4842 Brown-3 12 26 26
L. Tansley 05/99 4712 Brown-2 12 30 28
# Awk '$0 ~ /Brown/'grade.txt
J. Troll 07/99 4842 Brown-3 12 26 26
L. Tansley 05/99 4712 Brown-2 12 30 28

Exact match
# Awk '{if ($3 = "48") print $0}' grade.txt the third column contains many

48, so use exact match
P. Bunny 02/99 48 Yellow 12 35 28
# Awk '$3 = 48' grade.txt awk default action is

Print
P. Bunny 02/99 48 Yellow 12 35 28

Awk '{if ($6 <$7) print $1 "Try better at..."}' grade.txt
M. Tansley Try better...
J. Lulu Try better...

Set case sensitivity [] to match any character or word
Awk '{if ($4 ~ /[Gg] reen/) print $0} 'grade.txt
M. Tansley 05/99 48311 Green 8 40 44
J. Lulu 06/99 48317 green 9 24 26
Awk '/[Gg] reen/'grade.txt results are the same as above

Any character
[Root @ localhost awk_sed] # awk '$1 ~ /^... A/'grade.txt
M. Tansley 05/99 48311 Green 8 40 44
L. Tansley 05/99 4712 Brown-2 12 30 28
Awk '{if (/^... a/) print}' grade.txt results are the same as above

Or link match | when a vertical line character is used to match one of the two modes, the statement must be in parentheses.

Include
# Awk '$4 ~ /([Yy] ellow | [Bb] rown)/'grade.txt extraction level is yellow or

Brown Records
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

Beginning of line
# Awk '/^ 48/'grade.txt
# Awk '/^ P. B/'grade.txt
P. Bunny 02/99 48 Yellow 12 35 28
All expressions (except repeated characters) are valid in awk.
Compound Mode & |!
# Awk '{if ($1 = "P. Bunny" & $4 = "Yellow") print $0} 'grade.txt
P. Bunny 02/99 48 Yellow 12 35 28
Awk '{if ($4 = "green" | $4 ~ /Brown/) print $0} 'grade.txt
J. Lulu 06/99 48317 green 9 24 26
J. Troll 07/99 4842 Brown-3 12 26 26
L. Tansley 05/99 4712 Brown-2 12 30 28

Awk built-in variables
Number of ARGC command line parameters
ARGV command line parameter arrangement ARGV [n] indicates each element, and n indicates

Command line parameters
ENVIRON supports the use of system environment variables in the queue
FILENAME awk browsed file names awk can process many files simultaneously
Number of FNR browsing file records
FS sets the input domain separator, which is equivalent to the command line-F option. If comma is used as the domain separator

, Set FS = ","
Number of NF browsing records
Number of records read by NR
The default delimiter of the OFS output field is space. If you want to set it to #, write it to OFS = "#"
Default delimiter of ORS output records is new line (\ n)
The default RS control record delimiter is a new line (\ n)
The following example shows the built-in variables of awk.
NR allows you to quickly view the number of records. for example, you need to view the number of records after exporting database files.
# Awk 'end {print NR} 'grade.txt
5
# Awk '{print NR}' grade.txt
1
2
3
4
5
# Awk '{print NF, NR, $0} END {print FILENAME}' grade.txt print all students

Record, and print the record number, number of domains (NF), and file name
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.txt
When extracting information, it is best to first check whether there are records in the file.
# Awk '{if (NR> 0 & $4 ~ /Brown/) print $0} 'grade.txt
J. Troll 07/99 4842 Brown-3 12 26 26
L. Tansley 05/99 4712 Brown-2 12 30 28
A powerful function of NF is to pass the return value of the variable $ PWD into awk and display its directory. Here we need to refer

Fixed domain separator
# Echo $ PWD | awk-F/'{print $ NF }'
Awk_sed
Echo "/usr/local/src" | awk-F/'{print $ NF}' display file name
Src

Awk operator
When operators are used in awk, basic expressions are divided into numeric, string, variable, domain, and

Array element
= + = * =, = % = ^ = Value assignment operator
? Conditional expression operator
| &&! And, and, non
-! -Matching operators, including matching and non-matching
<<==! >>> Relational operators
+-*/% ^ Arithmetic operator
++ -- Prefix and suffix
1. set the input domain to the domain variable name
# Awk '{name = $1; belts = $4; if (belts ~ /Yellow/) print name "is belt"

Belts '} grade.txt
P. Bunnyis beltYellow
2. Comparison of domain values
Two methods: 1. assign a value to the variable name in BEGIN. 2. use the actual value in the link operation.
It is optional to enclose numbers in quotation marks. "27" and "27" are the same!
[Root @ localhost awk_sed] # awk '{if ($6 <27) print $0}' grade.txt
J. Lulu 06/99 48317 green 9 24 26
J. Troll 07/99 4842 Brown-3 12 26 26
# Awk 'In in {BASELINE = "27"} {if ($6 <27) print $0} 'grade.txt
J. Lulu 06/99 48317 green 9 24 26
J. Troll 07/99 4842 Brown-3 12 26 26
3. modify the value of a numeric field
When awk modifies any domain, it is important to remember that the actual input file cannot be modified.

Only awk copies saved in the cache. Awk will reflect the modification trace 0 in the NR or NF variable.
Awk '{if ($1 = "M. Tansley") $6 = $6-1; print $1 "\ t" $6 "\ t" $7 }'

Grade.txt
M. Tansley 39 44
J. Lulu 24 26
P. Bunny 35 28
J. Troll 26 26
L. Tansley 30 28
4. modify text fields
Awk '{if ($1 = "J. Troll") $1 = "J. L. Troll"; print $1}' grade.txt
M. Tansley
J. Lulu
P. Bunny
J. Troll
L. Tansley
5. only display the modification record. use curly brackets after the pattern to print only the modified part.
Awk '{if ($1 = "J. Troll") {$1 = "J. L. Troll"; print $1}' grade.txt
J. Troll
6. create a new output domain

Author "it career"

Related Article

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.