Introduction to Unix awk commands

Source: Internet
Author: User
Introduction to Unix awk commands

The most basic function of the awk language is to browse and extract information based on specified rules in a file or string. Only after awk extracts information can other text operations be performed, A complete awk script is usually used to format information in a text file.

Call awk:
First, the command line method, such as awk[-F field-separator]'Commands' input-file (s)
Commands is a real awk command, and the [-F domain separator] is optional. awk is separated by spaces by default. Therefore, you do not need to specify this option to browse text with spaces between domains, however, if you browse a passwd file, the fields of this file use colons as the separator, you must use the-F option: awk-F: 'commands' input-File
Second, insert all the awk commands into a file, make the awk program executable, and then use the awk command interpreter as the first line of the script to call it by typing the Script Name
Third, insert all the awk commands into a separate file, and then call: awk-FAwk-script-file input-File
-F indicates the awk script in the file awk-script-file. The input-file is the file name browsed by awk.

Awk script:
The awk script consists of various operations and modes. Based on the delimiter (-F option), the default value is space. The read content is placed in the corresponding domain in sequence, and one row of records is read until the end of the file.
Modes and actions: Any awk statement is composed of modes and actions. There may be many statements in an awk script. The Mode part determines when the Action Statement is triggered and the event is triggered. An action is an operation performed on data. If the mode is omitted, the action is always executed.
The mode can be any conditional statement, compound statement, or regular expression. The mode contains two special fields begin and end. The begin statement is used to set the count and print headers. The begin statement is used before any Text Browsing action, then, the Text Browsing action starts based on the input file. The end statement is used to print the total number of output texts and the ending status mark after the awk completes the Text Browsing action. actions must be included in {}.
The actual action is specified in braces {} and is often used for printing, but there are still longer Code such as if and loop looping statements and loop exit. If no action is specified, awk prints all browsing records by default.

Domains and records:
When awk is executed, Its browsing is marked as $1, $2... $ n. This method is called domain tag. use $1 and $3 to refer to the 1st and 3rd domains. Note that the fields are separated by commas (,) and $0 is used to represent all domains.
For example, awk '{print $0}' temp.txt> sav.txt explain prints all the fields and redirects the result to sav.txt.
Awk '{print $0}' temp.txt | tee sav.txt is similar to the preceding example. The difference is that
Awk '{print $1, $4}' temp.txt only prints the 1st and 4th Fields
Awk 'in in {print "name grade/n -------------"} {print $1 usd/t "$4} 'temp.txt
Indicates that the information header is entered, that is, "Name grade/n -------------" is added before the first line of the input content, and the content is separated by tab.
Awk 'in in {print "being"} {print $1} end {print "end"} 'temp prints both the information header and end

Conditional Operators:
<, <=, = ,! =,> = ,~ Match regular expressions ,!~ Do not match Regular Expression
Match: awk '{if ($4 ~ /Asima/) Print $0} 'temp indicates that if the fourth domain contains Asima, the entire
Awk '$0 ~ /Asima/'temp indicates that the entire file contains Asima and is printed.
Exact match: awk '$3 = "48" {print $0}' temp only prints records whose 3rd domain is equal to "48"
Mismatch: awk '$0 !~ /Asima/'temp print the entire record that does not contain Asima
Not equal to: awk '$1! = "Asima" 'temp
Less than: awk '{if ($1 <$2) Print $1 "is smaller"}' temp
Set case sensitivity: awk '/[Gg] reen/'temp print the entire record containing green or green
Any character: awk '$1 ~ /^... A/'temp print the fourth character in the 1st domain is a record, ^ line first,. Any character
Or link match: awk '$0 ~ /(ABC) | (EFG)/'temp used | the statement must be included.
And Relationship: awk '{if ($1 = "A" & $2 = "B") Print $0}' temp
Or link: awk '{if ($1 = "A" | $1 = "B") Print $0}' temp

Awk built-in Variables:
Argc command line parameter count agrv command line parameter arrangement environ supports the use of system environment variables in the queue
Filename awk browsed file name FNR browsed file record count FS sets input domain separator, same as-F Option
Number of NF browsing records Nr number of records read OFS output domain Separator
ORS output record separator Rs control record Separator
Example: awk 'end {print Nr} 'temp prints the number of read records at the end
Awk '{print NF, NR, $0} end {print filename}' temp
Awk '{If (NR> 0 & $4 ~ /Brown/) Print $0} 'temp has at least one record and contains Brown
Another usage of NF: Echo $ PWD | awk-F/'{print $ NF}' displays the current directory name

Awk Operator:
Using operators in awk, basic expressions can be divided into numbers, strings, variables, fields, and array elements.
Set the input field to the variable name:
Awk '{name = $1; six = $3; if (six = "man") Print name "is" Six}' temp
Domain value comparison operation:
Awk 'in in {base = "27"} {if ($4 <base) Print $0} 'temp
Modify the value of a numeric field: (the original input file will not be changed)
Awk '{if ($1 = "Asima") $6 = $6-1; print $1, $6, $7}' temp
Modify text fields:
Awk '{if ($1 = "Asima) ($1 =" DESC "); print $1}' temp
Show only modification records: (only show what is needed. Differentiate the previous command. Note {})
Awk '{if ($1 = "Asima) {$1 =" DESC "; print $1}' temp
Create a new output domain:
Awk '{$4 = $3-$2; print $4}' temp
Statistical column value:
Awk '(TOT + = $3); end {print tot}' temp displays the content of each column
Awk '{(TOT + = $3)}; end {print tot}' temp only displays the final result
File length addition:
Ls-L | awk '/^ [^ d]/{print $9 usd/t "$5} {tot + = $5} end {print" totkb: "tot }'
Only list file names:
Ls-L | awk '{print $9}' in general, the file name is a 9th domain

Awk built-in string functions:
Gsub (R, S) replaces R with S in $0
Awk 'gsub (/name/, "Xingming") {print $0} 'temp
Gsub (R, S, T) Replace r with S in the entire t
Index (S, T) returns the first position of string t in S.
Awk 'begin{ Print Index ("Sunny", "NY")} 'temp returns 4
Length (s) returns the length of S.
Match (S, R) test whether S contains a string matching r
Awk '$1 = "J. Lulu" {print match ($1, "U")}' temp returns 4
Split (s, A, FS) divides s into sequence a in FS
Awk 'in in {print split ("12 #345 #6789", myarray ,"#")"'
Returns 3, and myarray [1] = "12", myarray [2] = "345", myarray [3] = "6789"
Sprint (FMT, exp) returns the exp formatted by FMT
Sub (R, S) replaces R with S in the leftmost longest substring in $0 (only Replace the first matched string)
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.

Use of printf Functions:
Character conversion: Echo "65" | awk '{printf "% C/N", $0}' output
Awk 'in in {printf "% F/N", 999} 'output 999.000000
Formatted output: awk '{printf "%-15 S % s/n", $1, $3}' temp displays the first domain in the left alignment.

Other awk usage:
Pass a value to an awk command line:
Awk '{if ($5 <age) Print $0}' age = 10 temp
Who | awk '{if ($1 = user) Print $1 "are in" $2' user = $ LOGNAME use environment variables
Awk script command:
Start! /Bin/awk-F. If this sentence does not exist, the self-contained script cannot be executed. For example:

  1. ! /Bin/awk-F
  2. # All comment lines must start with a hash '#'
  3. # Name: student_tot.awk
  4. # To Call: student_tot.awk grade.txt
  5. # Prints total and average of club student points
  6.  
  7. # Print a header first
  8. Begin
  9. {
  10. Print "Student date Member No. grade age points Max"
  11. Print "name joined gained point available"
  12. Print "============================================ ==================================="
  13. }
  14. # Let's add the scores of points gained
  15. (TOT + = $6 );
  16. # Finished processing now Let's print the total and average point
  17. End
  18. {
  19. Print "club student total points:" tot
  20. Print "average club student points:" TOT/n
  21. }

Awk Array:
Basic loop structure of awk for (element in array) print array [element]
Awk 'in in {record = "123 #456 #789"; split (record, myarray ,"#")}
End {for (I in myarray) {print myarray [I]}

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.