Awk command details

Source: Internet
Author: User

Easy to use:

Awk: perform operations on a row in a file by itself.

Awk-F: '{print $1, $4}': Use ':' to split the row and print the first and fourth fields of the row.




AWK command Introduction


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.


1. Call awk:


The first command line method, such:


Awk [-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 awk commands into a separate file and then call them, for example:


Awk-f awk-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.


2. 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.


2.1. modes and actions


All awk statements are 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.


2.2. domain and record:


During awk execution, 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 fields. Note that the fields are separated by commas (,) and $0 is used to represent all fields. For example:


Awk '{print $0}' temp.txt> sav.txt


Sort prints all the fields and redirects the result to sav.txt.


Awk '{print $0}' temp.txt | tee sav.txt


Similar to the previous example, the difference is that it will be displayed on the screen.


Awk '{print $1, $4}' temp.txt


Only 1st and 4th fields are printed.


Awk 'in in {print "name grade \ n ----"} {print $1 "\ 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


Print both the information header and end


2.3. 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


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. '^' indicates the first line and '.' indicates 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


2.4. awk built-in variables:



Number of command line parameters


Number of browsing records


Command line parameter arrangement


Number of read records


Support the use of system environment variables in the queue


Output domain Separator


Awk browsed file name


Output record Separator


Number of browsing file records


Control record Separator


Set the input domain separator, which is the same as the-F option.


Number of browsing records


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


2.5. 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 "\ 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


2.6. 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.


2.7. Use of the printf function:


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.


2.8. 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:


! /Bin/awk-f


# All comment lines must start with a hash '#'


# Name: student_tot.awk


# To call: student_tot.awk grade.txt


# Prints total and average of club student points


# Print a header first






Print "Student Date Member No. Grade Age Points Max"


Print "Name Joined Gained Point Available"


Print "============================================ ============================"




# Let's add the scores of points gained


(Tot + = $6 );


# Finished processing now let's print the total and average point






Print "Club student total points:" tot


Print "Average Club Student points:" tot/N




2.9. 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]}



3.0 awk custom statements


I. Condition judgment Statement (if)

If (expression) # if (Variable in Array)
Statement 1
Statement 2

In the format, "Statement 1" can be multiple statements. If you want to facilitate the Unix awk judgment and facilitate your own reading, you 'd better enclose multiple statements. The Unix awk branch structure can be nested in the following format:

If (expression)

{Statement 1}

Else if (expression)
{Statement 2}
{Statement 3}

[Chengmo @ localhost nginx] # awk 'in in {
Tested = 100;
If (test> 90)
Print "very good ";
Else if (test> 60)
Print "good ";
Print "no pass ";

Very good


Each command statement can end.


Ii. Loop statements (while, for, do)

1. while statement


While (expression)



[Chengmo @ localhost nginx] # awk 'in in {
Tested = 100;
Total = 0;
While (I <= test)
Total + = I;
I ++;
Print total;

2. for Loop

The for loop has two formats:

Format 1:

For (variable in array)



[Chengmo @ localhost nginx] # awk 'in in {
For (k in ENVIRON)
Print k "=" ENVIRON [k];

AWKPATH =.:/usr/share/awk
OLDPWD =/home/web97
SSH_ASKPASS =/usr/libexec/openssh/gnome-ssh-askpass
LANG = zh_CN.GB2312


Note: ENVIRON is an awk constant and a typical subarray.

Format 2:

For (variable; condition; expression)



[Chengmo @ localhost nginx] # awk 'in in {
Total = 0;
For (I = 0; I <= 100; I ++)
Total + = I;
Print total;


3. do Loop



{Statement} while (condition)


[Chengmo @ localhost nginx] # awk 'in in {
Total = 0;
I = 0;
Total + = I;
I ++;
} While (I <= 100)
Print total;


The above is an awk flow control statement. We can see from the syntax above that it is the same as the C language. With these statements, many shell programs can be handed over to awk, and the performance is very fast.

Break When a break statement is used for a while or for statement, the program exit loop occurs.
Continue When a continue statement is used for a while or for statement, the program is moved to the next iteration cyclically.
Next Can read the next input line and return to the top of the script. This avoids other operations on the current input row.
Exit Statement to exit the main input loop and transfer the control to the END, if the END exists. If no END rule is defined or the exit statement is applied to the END, the script is executed.

The above control judgment on awk

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: 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.