Awk command, awk command details

Source: Internet
Author: User

Awk command, awk command details
1. awk Workflow
A) execute BEGIN first; B) read the file and read a record separated by/n linefeeds. c) divide the record by the specified domain separator. d) Fill in the domain, $0 indicates all domains, $1 indicates the first domain, and $ n indicates the domain (the default domain separator is the blank key or tab key) e) start the action actionf corresponding to the Execution Mode) then start to read the second record until all the records are read. g) execute the END operation.


2. Usage
Awk [-F field-separator] '{pattern + action}' {filenames} 1. pattern indicates the content that awk looks for in the Data. action indicates a series of commands executed when matching content is found. Action {} can have multiple statements separated. 2. [-F domain separator] is optional. Filenames is a file to be processed. In awk, each line of a file is called a domain separated by a domain separator. Generally, the default domain separator is a space without specifying the-F domain separator.
3. awk built-in Variables
ARGC command line parameter count ARGV command line parameter arrangement ENVIRON supports the number of records in the FNR file browsed by FILENAME awk for system environment variables in the queue. FS sets the input domain separator, it is equivalent to the number of domains in the command line-F option NF browsing record NR number of records read OFS output domain separator ORS output record separator RS control record Separator
4. awk built-in string functions
Gsub (r, s) replaces r with s in $0
Gsub (r, s, t) Replace r with s in the entire t
Index (s, t) returns the first position of string t in s.
Length (s) returns the length of s.
Match (s, r) test whether s contains a string matching r
Split (str, array, fs) divides s into sequence a in fs
Sprint (fmt, exp) returns the exp formatted by fmt
Sub (r, s) replaces s with the leftmost longest substring in $0
Substr (s, p) returns the suffix starting with p in string s.
Substr (s, p, n) returns the suffix of string s starting from p to n.

5. BEGIN and END
BEGIN indicates the END operation performed before processing any row. It indicates the processing performed after all input rows are processed.
6. conditional statements
if (expression) {    statement;    statement;    ... ...}if (expression) {    statement;} else {    statement2;}if (expression) {    statement1;} else if (expression1) {    statement2;} else {    statement3;}

7. Array
Because the subscript of an array in awk can be numbers and letters, the subscript of an array is usually called a key ). Both values and keywords are stored in an internal table that uses hash for key/value applications. Because hash is not stored in sequence, you will find that the array content is not displayed in the expected order. Arrays and variables are automatically created when they are used, and awk automatically determines whether they are stored as numbers or strings. In general, arrays in awk are used to collect information from records. They can be used to calculate the sum, count words, and track the number of times the template is matched.
awk -F ':' 'BEGIN {count=0;} {name[count] = $1;count++;}; END{for (i = 0; i < NR; i++) print i, name[i]}' /etc/passwdrootdaemonbinsyssyncgames......

Awk-F' \ t' {a [$1] ++} 'end' {for (j in a) print a [j], j} 'filename| sort-rn | more filename: applebananaappleapplebananapear
Output result: 3 apple2 banana1 pear




How to Use the linux awk command? Awk: used for dividing rows into numeric segments to process small data.
Running Mode: awk 'condition type 1 {Action 1} Condition Type 2 {Action 2}... 'filename

# Last | awk '{print $1 "\ t" $3}' <= view registrant data. Only logon names and IP addresses are displayed and separated by [tab ].

Awk built-in Variables
Variable name meaning

Total number of fields owned by each NF row ($0)

Row number of data processed by NR currently awk

Default Space key for the current FS Separator

Awk logical operators
Meaning of the computing unit
> Greater
<Less
> = Greater than or equal
<= Less than or equal
= Equal
! = Equal

Example:
Cat/etc/passwd | awk '{FS = ": "} $3 <10 {print $1" \ t "$3} '<= file/etc/passwd ": "To view less than 10 data in the third column, and only the accounts and third columns are displayed.

In my summary of awk, I hope it will help me write Oh copy
How to Use awk Part 8: How does awk read the print ARGV [I]; # prints the parameters recorded by awk in sequence} '$ * execute the statement: [root @ myfreelinux pub] # bash analyze. awk first-arg second-argawkfirst-argsecond-arg explanation: ARGCARGV [] awk built-in variable ARGC: an integer represents the number of all parameters except option-v-f and its corresponding parameters on the command line ARGV [] String Array ARGV [0] ARGV [1] ARGV [ARGC-1] represents execute the command [root @ myfreelinux pub] # bash analyze. awk first-arg second-argARGC value 3 ARGV [0] awkARGV [1] value first-arg ARGV [2] value second-arg re-ratio # awk-vx = 21-f program Fir-data sec-data and # awk '{print $1, $2} 'fir-data sec-data both ARGC values are 3 ARGV [0] awkARGV [1] fir-dataARGV [2] sec-data command line-f program-vx = 21 "The program section '{print $1 $2}' will be included in ARGC and ARGV [] awk. ARGC is used to determine the number of data files to be opened and the ARGC value is forcibly changed; if the ARGC value is set to 1 awk by the user, the ARGV [1] ARGV [2] file name will be used as a file name on the command line to open the file and read data; the program uses ARGV [1] ARGV [2] and other variables to obtain data from the data file on the command line. Under the existing awk program content: [root @ myfreelinux pub] # cat test1.awk #! /Bin/awk-fBEGIN {for (I = 0; I <ARGC; I ++) print ARGV [I];} run the preceding program: [root @ myfreelinux pub] # awk-f test1.awk arrive. dat today_result1awkarrive.dattoday_result1 add test1.awk content to change to test2.awk content: [root @ myfreelinux pub] # cat test2.awk #! /Bin/awk-fBEGIN {number = ARGC; # Use number to remember the actual number of parameters. ARGC = 2; # Set ARGC = 2awk to only the data file for (I = 0; I <ARGC; I ++) print ARGV [I];} Run and view the run end: [root @ myfreelinux pub] # awk-f test2.awk arrive. dat today_result1 today_result2awkarrive.dat will find that although ARGC = 3 people set ARGC = 2awk for execution, only the arrive parameter is recognized. modify dattest2.awk to the following content: [root @ myfreelinux pub] # cat test3.awk #! /Bin/awk-fBEGIN {number = ARGC; ARGC = 2; for (I = 0; I <ARGC; I ++) print ARGV [I]; for (I = ARGC; I <number; I ++) print ARGV [I];} Run and view the running Knot: by modifying ARGC and modifying awk, you can identify the number of parameters actually stored in ARGV. You can only enable ARGV [1] = arrive if ARGC is set to 2awk. dat we use ARGV [2] ARGV [3] to obtain the today_result1today_result2 parameter on the command line.

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.