Awk learning notes

Source: Internet
Author: User

When learning awk, we feel that awk is similar to many languages. For example, awk and Lex both use the "mode {action}" method for parsing input strings. However, the difference is that awk has two special modes: one is begin, which is executed only once at the beginning, and the other is not matched, the initialization work can generally be handed over to it, which is a bit similar to the constructor; the other is end, which is executed only once at the end and can be used to output the statistical result.

The default variable in awk is $0, which indicates the entire line of input statements, such as/mark/{action}, which is equivalent to $0 ~ /Mark/{ation}, which is similar to the $ _ variable in Perl and can be improved.ProgramWriting efficiency, but it also increases the difficulty of maintenance.

The awk operator is very similar to the C language operator.

Awk comes with some processing functions, such as getting a string and replacing a string.

There are many built-in variables in the awk, and the corresponding results can be obtained by performing corresponding read/write operations on these variables.

1. Compile a script to familiarize yourself with the built-in variables listed below:

Number of argc command line parameters
Argv command line parameter Array
Filename string = current input file name
FNR current number of records in the current file (1 for the input file)
FS input field separator
Number of fields in the current NF record
Current number of NR records (all input files)
Ofmt value output format (default: %. 6g)
Delimiter of the output field of ofs (space by default)
ORS output record separator (default: Line Break)
RS input record separator (default: Line Break)

Run the script:

Awk-F awk_inline_variable testfile.txt

Note: The content of testfile.txt is described below

Execution result:

Ii. Some small exercises:

CAT/etc/passwd

1. view the users on the System

Awk-F ":" '{print $1}'/etc/passwd

Execution result:

At the beginning, Print $0 is written, and the result is always printed out all the files. Later I checked the information and found that the split string is continued from 1.

2. view the user name and user uid ON THE SYSTEM

Awk-F ":" '{print "username:" $1 "\ t \ tuid:" $3}'/etc/passwd

Execution result (partial results ):

3. Format output characters

After completing the above exercises, I found that the UID column is not aligned, so I searched for information and found that the printf function is also available in awk, which is the same as in C, then, the above content is formatted and output. The username is left aligned and the UID is right aligned.

Awk-F ":" '{printf ("username: %-20 s uid: % 10d \ n", $1, $3)}'/etc/passwd

Execution result (partial results ):

3. I found the exercise questions online:

File Name: testdata.txt

Mike Harrington: [510] 548-1278: 250: 100: 175

Christian Dobbins: [408] 538-2358: 155: 90: 201

Susan dalsass: [206] 654-6279: 250: 60: 50

Archie mcnickl: [206] 548-1348: 250: 100: 175

Jody Savage: [206] 548-1278: 15: 188: 150

Guy Quigley: [916] 343-6410: 250: 100: 175

Dan Savage: [406] 298-7744: 450: 300: 275

Nancy MCNEIL: [206] 548-1278: 250: 80: 75

John goldenrod: [916] 348-4278: 250: 100: 175

Chet main: [510] 548-5258: 50: 95: 135

Tom Savage: [408] 926-3456: 250: 168: 200

Elizabeth stachelin: [916] 440-1763: 175: 75: 300

The above database contains the phone number and the donation for the past three months.

1. display the phone number

Awk-F ":" '{print $2}' testfile.txt

2. Display Dan's phone number

Awk-F ":" '/daN/{print $2}' testfile.txt

3. Display Susan's name and phone number

Awk-F ":" '/Susan/{print $1 $2}' testfile.txt

4. The last name starting with D is displayed.

Awk-F "[:]" '$2 ~ /^ D/{print $2} 'testfile.txt

5. a c or e name is displayed.

Awk-F "" '/^ [ce]/{print $1}' testfile.txt

6. Only four characters are displayed.

Awk-F "" 'length ($1) = 4 {print $1} 'testfile.txt

7. display the person name with area code 916

Awk-F ":" '/\ [916 \]/{print $1}' testfile.txt

8. Mike's donation is displayed. 250 $100 $175 is displayed for each value.

Awk-F ":" 'In in {OFS = "$"}/Mike/{print $3, $4, $5} 'testfile.txt

9. The name is followed by a comma, such as Jody and savage.

Awk-F "[:]" '{print $1 "," $2}' testfile.txt

10. Write an awk script. Its function is as follows:

. Display the full name and phone number of the savage

. Show Chet donations

. Show the person who donated $250 for the first month

Awk-F awk_test testfile.txt

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.