On the basic usage of awk

Source: Internet
Author: User


1. Domains and records:

Takes the first field and begins and ends with the character begin and end

You typically use begin to display variables and preset variables, using end to output the final result.

awk ' BEGIN {print ' being '} {print '} ' end {print ' End '} ' file

2. conditional operator:

The first field, which contains expect, prints the entire line

awk ' {if ($1~/expect/) print $} ' c.sh or awk ' $1~/expect/{print} ' file

Exact match: Print only records with 3 field equal to "48"

awk ' $3== ' {print $} ' file

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

Mismatch: Print the entire record that does not contain Asima

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 ' ^ '

Representing the beginning of the line, conforming to '. ' Represents any character

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

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

For loop

Awk-f "," ' {for (i=1;i<=nf;i++) if ($i ~/a=/) {print $i}} '

3.awk built-in variables:

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

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

awk ' {total+=$1}end{print total} ' A.txt only shows the final result

Add File Lengths:

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

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

5.awk printing all columns except the second column

Cat/etc/passwd|awk-f: ' {$2=null;print} ' is a column made empty

On the basic usage of 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: 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.