Home > php教程 > PHP开发 > Detailed explanation of awk command

Detailed explanation of awk command

高洛峰
Release: 2016-12-15 10:36:23
Original
1332 people have browsed it

Detailed explanation of awk command

Simple use:

awk: perform operations on each line of a file.

awk -F: '{print $1,$4}' : Use ':' to split this line and print out the first and fourth fields of this line.

Detailed introduction:

AWK command introduction

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

1. Call awk:

The first command line method, such as:

awk [-Field-separator] 'commands' input-file(s)

The commands here are the real awk commands, [-F field separator] is optional, awk uses spaces to separate by default, so if you want to browse text with spaces between fields, you do not need to specify this option, but if you browse a passwd file, each field of this file uses a colon as a separator, you must use the -F option: awk -F : 'commands' input-file

Second, insert all awk commands into one file and make the awk program executable, then use the awk command interpreter as the first line of the script so that you can call it by typing the script name

The third way is to insert all the awk commands into a separate file and then call it, such as :

awk -f awk-script-file input-file

-f option specifies the awk script in the file awk-script-file, input-file is the file name browsed using awk

2. awk script:

awk script consists of various operations and modes. According to the delimiter (-F option), the default is a space. The read content is placed in the corresponding field in turn, and the records are read line by line. Until the end of the file

2.1. Patterns and actions

Any awk statement is composed of patterns and actions, and there may be many statements in an awk script. The mode part determines when action statements fire and trigger events. Actions are operations on data. If you omit the pattern part, the action will remain in execution at all times

Pattern can be any conditional statement or compound statement or regular expression. The pattern contains two special fields BEGIN and END. Use BEGIN The statement sets the count and print head. The BEGIN statement is used before any text browsing action, and then the text browsing action starts executing based on the input file; the END statement is used to print the total number of texts and the ending status flag after awk completes the text browsing action. Actions must Use {} to enclose

The actual action is specified within curly brackets {}, which is often used for printing actions, but there are also longer codes such as if and looping statements and loop exits. If you do not specify what action to take, By default, awk prints out all browsed records

2.2. Domains and records:

When awk is executed, its browsing tags are $1, $2...$n, this method is called domain tagging. Use $1 and $3 to refer to the 1st and 3rd fields. Note that commas are used to separate the fields, and $0 means to use all fields. For example:

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

means to print all fields and redirect the results to sav.txt

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

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

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

Only the first one will be printed 1 and 4th domain

awk 'BEGIN {print "NAME GRADEn----"} {print $1"t"$4}' temp.txt

indicates the header, that is, the input content. Add "NAME GRADEn-------------" before a line, and separate the content with tabs

awk 'BEGIN {print "being"} {print $1} END {print "end "}' temp

Print the header and tail of the message at the same time

2.3. Conditional operators:

=, ~ matches regular expressions Formula, !~ does not match the regular expression

Match: awk '{if ($4~/ASIMA/) print $0}' temp means if the fourth field contains ASIMA, print the entire

Exact match :awk '$3=="48" {print $0}' temp Only print the record whose third field is equal to "48"

No match: awk '$0 !~ /ASIMA/' temp Print the entire record without ASIMA Record

Not equal to: awk '$1 != "asima"' temp

Less than: awk '{if ($1

Set case: awk '/[Gg]reen/' temp Print the entire record containing Green, or green

Any character: awk '$1 ~/^...a/' temp Print the record where the fourth character in the first field is a, the symbol '^' represents the beginning of the line, and '.' represents any character

Or relational matching: awk '$0~/(abc)|(efg)/' temp When using |, the statement needs to be enclosed

AND and relational: awk '{if ( $1=="a" && $2=="b" ) print $0}' temp

OR: awk '{if ($1=="a" || $1=="b") print $0}' temp

2.4 . awk built-in variables:

Detailed explanation of awk command

Example: awk 'END {print NR}' temp Print 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 There is at least one record and contains Brown

Another usage of NF: echo $PWD | awk -F/ ' {print $NF}' Display the current directory name

2.5. awk operator:

Use operators in awk. Basic expressions can be divided into numbers, strings, variables, fields and arrays Element

Set input field to variable name:

awk '{name=$1;six=$3; if (six=="man") print name " is " six}' temp

field Value comparison operation:

awk 'BEGIN {BASE="27"} {if ($4

Modify the value of the numerical field: (the original input file will not be changed)

awk '{if ($1=="asima") $6=$6-1;print $1, $6, $7}' temp

Modify text field:

awk '{if ($1= ="asima) ($1=="desc");print $1}' temp

Only show modification records: (Only show what is needed, distinguish from the previous command, pay attention to {})

awk '{ if ($1=="asima) {$1=="desc";print$1}}' temp

Create a new output field:

awk '{$4=$3-$2; print $4}' temp

Statistical column values:

awk '(tot+=$3);END {print tot}' temp will display the content of each column

awk '{(tot+=$3)};END {print tot}' temp Only the final result is displayed

Add the file lengths:

ls -l|awk '/^[^d]/ {print $9"t"$5} {tot+=$5} END{ print "totKB:" tot}'

Only the file name is listed:

ls -l|awk '{print $9}' Normally the file name is the 9th domain

2.6. awk built-in characters String function:

gsub(r, s) Replace r with s throughout $0

awk 'gsub(/name/,"xingming") {print $0}' temp

gsub(r , s, t) Replace r with s in the entire t

index(s, t) Return the first position of the string t in s

awk 'BEGIN {print index("Sunny", "ny ")}' temp Returns 4

length(s) Returns the length of s

match(s, r) Tests whether s contains a string matching r

awk '$1=="J. Lulu" {print match($1, "u")}' temp Return 4

split(s, a, fs) Divide s into sequence a on fs

awk 'BEGIN {print split("12 #345#6789",myarray,"#")"'

Returns 3, while myarray[1]="12", myarray[2]="345", myarray[3]="6789"

sprint(fmt, exp) Returns exp formatted by fmt

sub(r, s) Replace r with s from the leftmost longest substring in $0 (only replace the first matching string encountered)

substr(s, p) Return the string s The suffix part starting from p

substr(s, p, n) Returns the suffix part starting from p with length n in the string s

2.7. Use of printf function:

Character conversion : echo "65" |awk '{printf "%cn",$0}' Output A

{ printf "%-15s %sn", $1, $3}'temp will display all the first fields left aligned

2.8. Other awk usage:

Passing value to a line of awk command:

awk '{if ($5

who | awk '{if ($1==user) print $1 " are in " $2 ' user=$LOGNAME Use environment variables

awk script command:

Use !/bin/awk -f at the beginning. Without this sentence, the self-contained script will not be executed. 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

BEGIN

{

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

END

{

print "Club student total points :" tot

print "Average Club Student points :" tot/N

}

2.9. awk array:

The basic loop structure of awk

For (element in array) print array[element]

awk 'BEGIN {record="123#456#789";split(record, myarray , "#")}

END { for (i in myarray) {print myarray[i]} }

3.0 Custom statements in awk

1. Conditional judgment statement (if)

if (expression) #if (Variable in Array)

Statement 1

else

Statement 2


"Statement 1" in the format can be multiple statements. If you want to facilitate Unix awk judgment and facilitate your own reading, you It is best to enclose multiple statements with {}. Unix awk branch structure allows nesting, and its format is:

if(expression)

{statement 1}

else if(expression)

{statement 2}

else

{statement 3}


[chengmo @localhost nginx]# awk 'BEGIN{
test=100;
if(test>90)

{

 print "very good";
}
else if(test>60)
{
 print "good";
}
else
{
         print "no pass";
}
}'

very good

 

You can end each command statement with a ";" sign.

2. Loop statement (while, for, do)

1. while statement

format:

while (expression)

{statement}

example:

[chengmo@localhost nginx]# awk 'BEGIN{

test=100;

total=0;

while(i{
  total+=i;
 i++;
}
print total;
}'
5050

2.for loop

for loop has two formats:

Format 1:

for (variable in array)

{statement}

Example:

[chengmo@localhost nginx]# awk 'BEGIN{

for(k in ENVIRON)

{

 print k"="ENVIRON[k];

}
}'

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

SELINUX_LEVEL_REQUESTED=

SELINUX_ROLE_REQUESTED=
LANG=zh_CN.GB2312

. . . . . .

Explanation: ENVIRON is an awk constant and a sub-typical array.

Format 2:

for (variable; condition; expression)

{statement}

Example:

[chengmo@localhost nginx]# awk 'BEGIN{
total=0;
for(i=0; i{
  total+=i;
}
print total;
}'

5050

3.do loop

Format:

do

{statement}while (condition)

Example:

[chengmo@localhost nginx]# awk 'BEGIN{
total=0;
i=0;
do
{
total+=i;
i++;
}while(iprint total;
}'
5050

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

Detailed explanation of awk command


For more detailed explanations of awk commands, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template