Losbyday one of the powerful tools under Linux Akw,shell Essentials

Source: Internet
Author: User

Simple to use:
Awk: Performs an operation on a single line of the file in solitude.
Awk-f: ' {print $1,$4} ': Use ': ' to split the line and print the first fourth field of the line.


Detailed Description:
awk Command Introduction

The most basic function of the awk language is to browse and extract information in a file or string based on the specified rules, before awk extracts the information for other text operations, and the complete awk script is typically used to format the information in the text file


1. Call awk:



The first type of command line, such as:

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

Here commands is the true awk command, [-F domain delimiter] is optional, and awk is separated by a space by default, so if you want to browse text with spaces between domains, you do not have to specify this option, but if you browse for a file such as passwd that uses a colon as the delimiter for each field, you must use the-F option: Awk-f: ' Commands ' input-file

Second, insert all awk commands into a file and make the awk program executable, and then use the awk command interpreter as the first line of the script to invoke it by typing the script name

Third, insert all of the awk commands into a separate file and then invoke it, such as:

Awk-f Awk-script-file Input-file

The-F option indicates the awk script in the file Awk-script-file, Input-file is the file name that is browsed using awk


2. awk Script:



The awk script consists of a variety of operations and patterns, based on the delimiter (-f option), the default is a space, the contents of the read are placed in the corresponding domain, a row of records read, until the end of the file


2.1. Modes and actions



Any awk statement is made up of patterns and actions, and there may be many statements in an awk script. The mode section determines when an action statement triggers and triggers an event. Actions are actions on the data, and if you omit the mode part, the action will remain in the execution state at all times.

The pattern can be any conditional statement or compound statement or regular expression, with the pattern containing two special fields begin and end, using the BEGIN statement to set the count and the printhead, before the BEGIN statement is used before any text-browsing action, and then the text-browsing action is executed according to the input file; The end statement is used to print the total and end state flags of the output text after awk completes the text-browsing action, and the action must be enclosed in {}.

The actual action is indicated in curly braces {} and is often used for print actions, but there are longer codes such as if and loop looping statements and loop exits, and if you do not specify what action to take, awk prints out all the browsed records by default.


2.2. Domains and records:

< br> 
When Awk executes, its browse flag is $1,$2 ... $n, this method is called a domain tag. Using $1,$3 to refer to fields 1th and 3rd, note that this is a comma-delimited field, using the $-A to represent all domains. Example:
 
awk ' {print $} ' temp.txt > sav.txt  
 
Indicates that all fields are printed and the results are redirected to Sav.txt
 
awk ' {print $} ' temp.txt|tee sav.txt 
&NBSP
  Similar to the previous example, the difference is that it will be displayed on the screen
 
awk ' {print $1,$4} ' Temp.txt
&NBSP
   Prints only 1th and 4th fields
 
awk ' BEGIN {print ' name  grade\ n----"} {print $ \ t" $4} ' temp.txt 
 
means to hit the header, that is, the first line of the input is preceded by the "name  grade\n-------------", At the same time, the contents are separated by tab
 
awk ' BEGIN {print "being"} {print "} end {print" End "} ' temp &NBSP
 
Print both the information header and the message end
 

2.3. Conditional operator:



<, <=, = =,! =, >=, ~ match regular expressions,!~ mismatched regular expressions

Match: awk ' {if ($4~/asima/) print $} ' temp indicates that if the fourth field contains Asima, the entire bar will be printed

Exact match: awk ' $3== ' {print $} ' temp only prints records with Field 3 equal to ' 48 '

Mismatch: awk ' $!~/asima/' temp prints entire records that do not contain Asima

Not equal to: awk ' $! = ' Asima ' temp

Less than: awk ' {if ($1<$2) print $ "is smaller"} ' temp

Set case: awk '/[gg]reen/' temp prints a whole strip of records containing green, or green

Any character: awk ' ~/^...a/' temp prints the fourth character in the 1th field is a record, the symbol ' ^ ' represents the beginning of the line, conforms to '. ' Represents any character

or relationship matching: awk ' $0~/(ABC) | (EFG)/' temp ' when using |, the statement needs to be enclosed

and relationship: awk ' {if ($1== "a" && $2== "B") print $} ' Temp

Or or relationship: awk ' {if ($1== "a" | | $1== "B") print $} ' Temp


2.4. Awk built-in variables:

< Span style= "color: #808000;" >  

argc   command-line arguments nf       Browse record number of fields
agrv   command-line arguments arrange nr   number of records read &NBSP;&NBSP;&NB SP;
environ   supports the use of system environment variables in queues ofs   output domain delimiter
filename   filename    ors  output record delimiter
fnr   number of records    rs   control record delimiter
fs   set input field delimiter, same-f option nf       : Number of fields for browsing records



Example: awk ' end {print NR} ' temp Prints the number of read record strips at the end

awk ' {print nf,nr,$0} END {print FILENAME} ' temp

awk ' {if (nr>0 && $4~/brown/) print $} ' 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 numeric, string, variable, field, and array elements

Set input field to variable name:

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

Domain Value comparison operation:

awk ' BEGIN {base= ' + '} {if ($4<base) print $} ' Temp

Modify numeric Field values: (the original input file will not be changed)

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

To modify a text field:

awk ' {if ($1== "Asima) ($1==" desc ");p rint ' temp '

Show only Modified records: (Show only what you need, differentiate the previous command, note {})

awk ' {if ($1== "Asima) {$1==" desc ";p rint$1}} ' Temp

To create a new output domain:

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

Statistic column values:

awk ' (tot+=$3); END {print tot} ' temp Displays the contents of each column

awk ' {(tot+=$3)}; End {print tot} ' temp shows only the final result

Add File Lengths:

Ls-l|awk '/^[^d]/{print $9 "\ T" $ "{tot+=$5} end{print" TOTKB: "Tot} '

List file names only:

Ls-l|awk ' {print $9} ' general case file name is domain 9th


2.6. Awk built-in string functions:



Gsub (R,s) replaces R with s in the entire $

awk ' Gsub (/name/, "xingming") {print $} ' Temp

Gsub (R,S,T) replaces R with S in the whole t

Index (S,T) returns the first position of the string T in S

awk ' BEGIN {print index ("Sunny", "NY")} ' temp returned 4

Length (s) returns the lengths of S

Match (S,R) tests if s contains a string that matches R

awk ' $1== ' J.lulu ' {print match ($, "U")} ' temp returns 4

Split (S,A,FS) divides s into sequence a on FS

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

Return 3, while myarray[1]= "", 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 of $ A (replaces only the first encountered match string)

SUBSTR (s,p) returns the suffix part of the string s starting from P

SUBSTR (s,p,n) returns the suffix part of the string s starting from p in length n


2.7. The use of the printf function:



Character conversion: echo "|awk" {printf "%c\n", $} ' output a

awk ' BEGIN {printf '%f\n ', 999} ' output 999.000000

Formatted output: awk ' {printf '%-15s%s\n ', $1,$3} ' temp Displays the first field all left-justified


2.8. Other awk usage:



To pass a value to a row of AWK commands:

awk ' {if ($5<age) print $} ' age=10 Temp

W.H.O. | awk ' {if ($1==user) print $ "is in" $ user= $LOGNAME using environment variables

awk script command:

The beginning uses!/bin/awk-f, if there is no self-contained script will not execute, 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 the 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 cyclic basic 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 The custom statement in awk

I. Conditional judgment statement (IF)
if (expression) #if (Variable in Array)
Statement 1
Else
Statement 2
The format "Statement 1" can be more than one statement, if you want to facilitate the UNIX awk to judge also convenient for you to read, you'd better put multiple statements in {}. The Unix awk branching structure allows nesting, in the form of:
if (an expression)
{Statement 1}
else if (expression)
{Statement 2}
Else
{Statement 3}
[Email protected] nginx]# awk ' begin{
test=100;
if (test>90)
{
Print "Very good";
}
else if (test>60)
{
print "good";
}
Else
{
Print "No pass";
}
}‘
Very good

After each command statement, you can end with a ";" sign.

Two. Circular statement (WHILE,FOR,DO)
1.while statements
Format:
while (expression)
Statement
Example:
[Email protected] nginx]# awk ' begin{
test=100;
total=0;
while (I<=test)
{
Total+=i;
i++;
}
Print total;
}‘
5050
2.for Cycle
The For Loop has two forms:
Format 1:
For (variable in array)
Statement
Example:
[Email protected] 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
。。。。。。
Description: ENVIRON is an awk constant, a sub-typical array.
Format 2:
for (a variable; a condition; an expression)
Statement
Example:
[Email protected] nginx]# awk ' begin{
total=0;
for (i=0;i<=100;i++)
{
Total+=i;
}
Print total;
}‘
5050
3.do Cycle
Format:
Do
{statement}while (condition)
Example:
[Email protected] nginx]# awk ' begin{
total=0;
i=0;
Do
{
Total+=i;
i++;
}while (i<=100)
Print total;
}‘
5050

The above is the AWK Process Control statement, as you can see from the syntax above, as in the C language. With these statements, many shell programs can be handed to awk, and the performance is very fast.

If no end rule is defined or an exit statement is applied in end
break causes the exit program to loop when the break statement is used for a while or for statement.
continue When the continue statement is used for a while or for statement, the program loops to the next iteration.
next can cause the next input line to be read and returned to the top of the script. This avoids performing other procedures on the current input line. The
exit statement causes the primary input loop to exit and transfer control to end if the end exists. Terminates the execution of the script


Losbyday one of the powerful tools under Linux akw (go), Shell essentials

Related Article

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.