Built-in string functions in the awk language in AIX

Source: Internet
Author: User
Built-in string function [plain] www.2cto in The awk language in AIX. built-in string functions & mdash; & mdash; & mdash
The built-in string function [plain] www.2cto.com in The awk language in AIX replaces r gsub (r, s) with s in $0, t) replace r index (s, t) with s in the entire t to return the length (s) at the first position of the string t in s to return the length of s. match (s, r) test if s contains the string split (s, a, fs) matching r. return fs to split s into series a sprint (fmt, exp) and return the exp sub (r, s) replace s substr (s, p) with the leftmost longest substring in $0 to return substr (s, p, n) of the suffix starting from p in string s) returns the suffix of n starting from p in string s ------------------------- ------------------------------------- 1. the gsub function replaces 4842 of rows containing 4842 with 4899: awk 'gsub (/4842/, 4899) {print $0} 'grade.txt J. troll 07/99 4899 Brown-3 12 26 26 awk '{if ($3 ~ /4842/) print $0} 'grade.txt [or awk' $0 ~ /4842/'grade.txt] J. troll 07/99 4842 Brown-3 12 26 26 2. the index function finds the location where ny first appeared: awk 'end {print index ("Bunny", "ny ")} 'grade.txt 4: Locate the first occurrence location of ny in the first domain containing Bunny, and print this line: awk '$1 ~ /Bunny/{print index ($1, "ny") ":" $0} 'grade.txt 6: P. bunny 02/99 48 Yello 12 35 28 3. length function awk 'if ($1 ~ /Tran/) | (length ($1)> 7) {print $0} 'grade.txt error: awk: syntax error at line 1 of program <if ($1 ~ /Tran/) | (...> context is >>> if <($1 ~ /Tran/) | (length ($1)> 7) {print $0} awk: bailing out at line 1 of program <if ($1 ~ /Tran/) | (...> cause of error: all actions must be included in. Correct: awk '{if ($1 ~ /Tran/) | (length ($1)> 7) {print $0} 'grade.txt M. tansley 05/99 48311 Green 8 40 44 L. tansley 05/99 4712 Brown-2 12 30 28 4. match function awk 'In in {print match ("abcd",/A/)} '0 awk' BEGIN {print match ("abcd",/c /)} '3 awk' $1 = "J. lulu "{print match ($1," u ")} 'grade.txt 4 5. split function awk 'In in {print split ("123 #456 #789", myarray, "#")} '3 6. sub function awk 'if ($1 ~ /Troll/) {print $0} 'grade.txt if must be placed in {}; otherwise, awk: Syntax error at line 1 of program <if ($1 ~ /Troll/) {prin...> context is >>> if <($1 ~ /Troll/) {print $0} awk: bailing out at line 1 of program <if ($1 ~ /Troll/) {prin...> awk '{if ($1 ~ /Troll/) {print $0} 'grade.txt J. troll 07/99 4842 Brown-3 12 26 26 7. substr function awk '$1 = "L. tansley "{print substr ($1, 1, 5)} 'grade.txt L. tan does not have the third parameter awk '$1 = "L. tansley "{print substr ($1, 1)} 'grade.txt L. the three parameters in Tansley are greater than the domain length awk '$1 = "L. tansley "{print substr ($100,)} 'grade.txt L. tansley specifies the length of awk '$1 = "L. tansley "{print substr ($1, 1, length ($1)-1)} 'grade.txt L. the starting position of tanlupus is 0 awk '$1 = "L. tansley "{prin T substr ($1, 0, length ($1)-1)} 'grade.txt L. intercept the entire line awk '$1 = "L. tansley "{print substr ($0, 1, 15)} 'grade.txt L. tansley 05/9 prints the captured data and the original data awk '$1 = "L. tansley "{print substr ($0, 15)} END {print $0} 'grade.txt L. tansley 05/9 L. tansley 05/99 4712 Brown-2 12 30 28 connect the intercepted string to an awk '$1 = "L. tansley "{print substr ($0, 1, 15)" ___ 3 blanks "} 'grade.txt L. tansley 05/9 ___ 3 blanks get name awk '{print substr ($ )} 'Grade.txt Tansley Lulu Bunny Troll Tansley 8. input the echo "_ yeeXun" | awk '{print length ($0)} string to awk from shell )} '7 STR = "grade.txt" echo $ STR | awk' {print substr ($ STR,)} 'awk: illegal field $ () input record number 1, file-source line 1 of program <{print substr ($ STR, 1...> cause of error: awk does not know $ STR. the data passed by pipeline commands is considered as $0. Therefore, replace $ STR with $0: echo $ STR | awk '{print substr ($, 5)} 'grade truncation suffix echo $ STR | awk' {print s Ubstr ($)} 'txt echo $ STR | awk' {print substr ($0, match ($0 ,/\. /) + 1)} 'txt: ls-l total 28-rw-r -- 1 xxxx group 0 Nov 19 cat-rw-r -- 1 xxxx group 28 Nov 14 cat_file.txt drwxr-xr-x 2 xxxx group 512 Nov 21 c_src-rw-r -- 1 xxxx group 356 Nov 16 data. f-rw-r -- 1 xxxx group 284 Nov 19 delete_me_and_die-rwxr -- r -- 1 xxxx group 61 Nov 8 first2 -Rw-r -- 1 xxxx group 235 Nov 19 grade.txt-rwxr -- r -- 1 xxxx group 354 Nov 17 info.txt-rwxr ----- 1 xxxx group 23 Nov 7 myfile drwxr -xr-x 2 xxxx group 512 Nov 21 SQL _src-rwxr -- r -- 1 xxxx group 225 Nov 15 test. bak-rwxr -- r -- 1 xxxx group 225 Nov 15 test. SQL-rw-r -- 1 xxxx group 1998 Nov 15 who. out-rw-r -- 1 xxxx group 229 Nov 19 14:44 wow ls-l | Awk '{print substr ($9, match ($9 ,/\. /) + 1)} 'cat txt c_src f delete_me_and_die first2 txt myfile SQL _src bak SQL out wow find the file with the suffix ls-l | awk '{if (match ($9, /\. /)> 0) {print $9} 'cat_file.txt data. f grade.txt info.txt test. bak test. SQL who. out and then intercept the suffix: ls-l | awk '{if (match ($9 ,/\. /)> 0) {print substr ($9, match ($9 ,/\. /) + 1)} 'txt f txt bak SQL out. separate the file name from the suffix: ls-l | awk '{if (match ($9 ,/\./ )> 0) {print $9 "#" substr ($9, match ($9 ,/\. /) + 1)} 'cat_file.txt # txt data. f # f grade.txt # txt info.txt # txt test. bak # bak test. SQL # SQL who. out # out use ">" to write data to the file and directly overwrite the data; ">", additional. -- The end --
 
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.