Linux awk Learning

Source: Internet
Author: User
Awk:
awk Syntax: awk mode {action}
Patterns: Regular, relational expressions, patterns, begin,end,
Action: consists of commands, functions, and expressions.

Text Source file Scores.txt

How to execute awk:
1. Execute under command line

2. Execute awk script awk-f script name file name
3. Executable awk Script

Pattern Matching for awk:
1. Use regular mode to print out the line beginning with T, and take the first column only

2. Interval mode, that is, pattern one and the middle of the pattern two rows are matched to
3.BEGIN mode, operation before reading the file line (can put some file description, variables, etc. written in this mode)
4.END mode, operating at the end, contrary to begin
Write a script about begin and end:

Note A detail question:
In the script, the pattern is not quoted, and the operation requires {}, and {needs and patterns are in one row
There are multiple actions in a pattern, then a newline is written
Below the command line, a quotation mark is inside a pattern, and an operation. Two patterns separated by semicolons;

System built-in variables in awk
(Record most times = line)
$ A record currently being processed
$n the first few fields
NF the number of fields currently logged
NR has read a few records
Filenam file name
FS Field Delimiter
RS Record delimiter default: line break

Example: Source file Articles.txt

To re-set the delimiter:

Operators and expressions can also be supported in awk
Such as
BEGIN {
X=5/2
Print X
}

Conditional expression:
Syntax: expression?value1:value2
Relational operators

, <,==, ~,!~, et cetera.
8==8 {print} Executes the print operation if 8=8 is true
Example: ~ is the meaning of the match
The following is a list of records that match the first field (column) that starts with a K print

Linux awk Learning

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.