awk Regular expressions and the use of built-in functions examples of methods _linux shell

Source: Internet
Author: User
Tags diff file size function examples

awk regular Expressions and built-in function examples are detailed:

1, Fuzzy matching:

Copy Code code as follows:

awk ' {if ($3~/97/) print $} ' DATA.F: Print the line if "97" is included in the third item
awk ' {if ($4!~/ufcx/) print $} ' DATA.F: Print if UFCX is not included in the third item

2. Exact match:

Copy Code code as follows:

awk ' {if ($5==66) print $} ' DATA.F: Print if item fifth is 66
awk ' {if ($5!=66) print $} ' DATA.F: Print if item fifth is not 66
awk ' {if ($1>$5) print $} ' DATA.F: Print If the first item is greater than item fifth

3, Case matching:

Copy Code code as follows:

If the awk ' {if (/[ss]ept/) print $} ' DATA.F: Compliant, print one line.
awk '/[ss]ept/{print $} ' DATA.F: Compliant, print the second field

4, any match:

Copy Code code as follows:

awk ' {if ($ ~/^.e/) print $} ' DATA.F: In the second field, the second character is E, the output
awk ' {if ($ ~/(LPS|FCX)/) print $} ' DATA.F: Fourth field contains LPs or FCX output

5, &&,| | :

Copy Code code as follows:

awk ' {if ($ ~/1993/&& $2== ' Sept ') print $ ' DATA.F: Both sides are true output
awk ' {if ($ ~/a9/| | $2== ' Sept ') print $} ' DATA.F: One side for true output

6. Variable definition:

awk ' {date=$2;price=$5. if (date ~/[ss]ept/) print ' price ' = ' DATA.F: A variable definition that satisfies the price output of date Sept or Sept.

7, modify the value (the source file value does not change)

Copy Code code as follows:

awk ' {baseline=42; if ($1>baseline) $5=$5+100 print $} ' DATA.F: Three lines of procedure to ";" Segmentation

If you are modifying a text field, add "". For example: awk ' {if ($2== "may") $2= "TT"; print $} ' DATA.F

Above all show all data, awk ' {if ($2== ' may ') {$2= ' TT '; print $} ' DATA.F This only shows modified data, look carefully, in fact the syntax is the same as C, except that you have added a {} symbol at the very outset.

8, create a new domain: (source file values unchanged)

Copy Code code as follows:

awk ' {if ($5>$1) {$8=$5-$1;print $1,$8}} ' DATA.F:

Or awk ' {if ($5>$1) {Diff=$5-$1;print $1,diff}} ' DATA.F

9, Data statistics:

awk ' {(total+=$5)}end{print total} ' data.f: ' {(total+=$5)} ' and ' {print total} ' represent two different pieces of code, and if there is no cumulative result for each of the end output, End can be understood as the flag of the code paragraph, so that only the final result, the {print total}, is executed only once.

10, Statistics File size:

Copy Code code as follows:

Ls–l | awk ' {if (/^[^d]/) total=+$5}end{print total KB: ' Total} ':/^[^d]/header matching can not write field values

11. awk Built-in variables:

Number of ARGC command line arguments

ARGV Command line parameter arrangement

The use of system environment variables in ENVIRON support queues

FileName awk Browse file name

Number of records FNR browsing files

FS Set input field separator, equivalent to command line-f option

NF Browse record number of fields

The number of records that NR has read

OFS Output Field Separator

ORS Output Record Separator

RS Control Record Separator

12. awk built-in String handler function

Gsub (R, s) replaces R with s in whole $

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

Length (s) returns s

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

Split (S, A, FS) on FS will be divided into sequence a

Sprint (f M T, exp) returns exp after format F m t

Sub (R, S, $) $ s replaces the position where the first R appears

SUBSTR (S, p) returns the suffix portion of the string s that starts with P

SUBSTR (S, p, n) returns the suffix portion of the string s starting with the length n from p

13. awk ' Gsub (/6\./,78) {print $} ' DATA.F: All "6." Replace with 78 and output

Copy Code code as follows:

awk ' {if ($2== "Sept") {sub (/3/, "9″,$0); print $}} ' DATA.F: Replace only the first occurrence of the

awk ' begin{print index ("Hello", "Lo")} ': Output value is 4

awk ' {if ($3== "3bc1997″) print length ($)" "$}" data.f

awk ' Begin{print match ("ABCD", "B")} ': Output 2

awk ' Begin{print match ("ABCD",/b/)} ': '//' and ' ' effect is the same

awk ' BEGIN {print split (' 123#234#654″, myarray, ' # ')} ': Returns the number of array elements, 123#234#654 is a string, and takes "#" as the delimiter, putting the string into an array.

awk ' {if ($1==34) print substr ($3,2,7)} ' DATA.F

awk ' Begin{print substr ("Helloleeboy", 2,7)} ': Output ellole

awk ' Begin{print substr ("Helloleeboy", 2,7)} ' DATA.F: output n times ellole,n number of rows DATA.F

14. awk ' Begin{print ' May\tday\n\nmay \104\141\171″} ': \104\141\171 represents day. \t:tab key, \ n: NewLine, \ddd: Octal

15, echo "65" | awk ' {printf '%c\n ', $} ': printf function, similar to C, output to a. (ASCII code)

Copy Code code as follows:

echo "65" | awk ' {printf '%d\n ', $} ': output 65 digits.

awk ' {printf '%-15s%s\n ', $2,$3} ' data.f: '%-15s ' left-aligned 15-character length

awk ' {if (age<$1) print $} ' age=80 data.f and awk ' {age=49;if (age<$1) print $} ' data.f as a result, the former passes the value into awk, which defines a variable in awk.

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.