Linux 7 awk instance for leeboy

Source: Internet
Author: User

Detailed description of awk Regular Expressions and built-in function instances:

1. Fuzzy match: awk '{if ($3 ~ /97/) Print $0} 'data. F: print the row if the third item contains "97"

Awk '{if ($4 !~ /Ufcx/) Print $0} 'data. F: print if the third item does not include ufcx

2. Exact match: awk '{if ($5 = 66) Print $0} 'data. F: print if item 5 is 66

Awk '{if ($5! = 66) Print $0} 'data. F: print if the fifth item is not 66

Awk '{if ($1> $5) Print $0} 'data. F: if the first item is greater than the fifth item, print

3. case-insensitive match: awk '{If (/[ss] EPT/) Print $0}' data. F: Yes, a row is printed.

Awk '/[ss] EPT/{print $2}' data. F: Yes, the second field is printed.

4. Arbitrary match: awk '{if ($2 ~ /^. E/) Print $0} 'data. F: In the second field, the second character is E, and the output

Awk '{if ($4 ~ /(LPS | FCX)/) Print $0} 'data. F: If the fourth field contains LPS or FCX, the output is

5. &, |:

Awk '{if ($3 ~ /1993/& $2 = "Sept") Print $0} 'data. F: outputs if both sides are true

Awk '{if ($3 ~ /A9/| $2 = "Sept") Print $0} 'data. F: if one side is true, the output is

6. variable definition:

Awk '{date = $2; Price = $5; If (date ~ /[Ss] EPT/) print "price is" Price} 'data. F: variable definition. If date is set to Sept or Sept, the price is output.

7. Modify the value (the value of the source file remains unchanged)

Awk '{Baseline = 42; if ($1> baseline) $5 = $5 + 100; print $0}' data. F: Three-line program separated ";"

If the text field is modified, add "". For example: awk '{if ($2 = "may") $2 = "TT"; print $0} 'data. f

All data is shown above, awk '{if ($2 = "may") {$2 = "TT"; print $0}' data. f: only the modified data is displayed. Take a closer look. In fact, the syntax is the same as that of C, but a {} symbol is added to the outermost side.

8. Create a new domain: (the source file value remains unchanged)

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}" indicates two different code segments. If no end accumulation result is returned, end can be understood as a sign of the Code section, in this way, only the final result {print total} is output and executed once.

10. Statistics file size:

Ls-L | awk '{If (/^ [^ d]/) Total = + $5} end {print "Total KB:" Total }': /^ [^ d]/match at the beginning of the line without specifying the Domain value $1

11. awk built-in variables:

Number of argc command line parameters

Argv command line parameter arrangement

Environ
Support the use of system environment variables in the queue

Filename awk browsed file name

Number of FNR browsing file records

FS sets the input domain separator, which is equivalent to the command line-F Option

Number of NF browsing records

Number of records read by NR

OFS output domain Separator

ORS output record Separator

RS control record delimiter

12. awk built-in string processing functions

Gsub (R, S) replaces R with S in $0

Gsub (R, S, T) Replace r with S in the entire t

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

Length (s) returns the length of S.

Match (S, R) test whether S contains a string matching r. The returned position is

Split (s, A, FS) divides s into sequence a in FS

Sprint (F m T, exp) returns the exp formatted by f m t

In sub (R, S, $0) $0, s replaces the position where R appears for the first time.

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.

13. awk 'gsub (/6 \./, 78) {print $0} 'data. F: replace all "6." With 78 and Output

Awk '{if ($2 = "Sept") {sub (/3/, "9", $0); print $0} 'data. f: Only Replace the first

Awk 'in in {print index ("hello", "Lo")} ': the output value is 4.

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

Awk 'in in {print match ("ABCD", "B")} ': Output 2

Awk 'in in {print match ("ABCD",/B/)} ': "//" and "" have the same effect

Awk 'in in {print split ("123 #234 #654", myarray, "#")} ': returns the number of array elements. 123 #234 #654 is a string, use "#" as the separator to put the string into an array.

Awk '{if ($1 = 34) print substr ($3, 2, 7)} 'data. f

Awk 'in in {print substr ("helloleeboy",)} ': Output ellole

Awk 'in in {print substr ("helloleeboy",)} 'data. F: Output n times of ellole, n is the number of rows of data. f

31. awk 'in in {print "May \ tday \ n \ nmay \ 104 \ 141 \ 171 "}':

\ 104 \ 141 \ 171 indicates day. \ T: Tab key, \ n: line feed, \ DDD:

32. Echo "65" | awk '{printf "% C \ n", $0}': printf function, which is similar to C and outputs as. (ASCII code)

Echo "65" | awk '{printf "% d \ n", $0}': Output 65 numbers.

Awk '{printf "%-15 S % s \ n", $2, $3}' data. F: "%-15s" is left aligned with 15 characters in length

Awk '{If (age <$1) Print $0}' age = 80 data. F and awk '{age = 49; If (age <$1) Print $0} 'data. f results are the same. The former transmits the value to awk, and the latter defines a variable in 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.