Awk in linux/unix shell

Source: Internet
Author: User

Awk in linux/unix shell has learned some usage of awk today to share with you the preparation work: the first ls prepared by www.2cto.com. the content in out is as follows: [plain] [oracle @ localhost testDir] $ cat ls. out total 68-rwxr -- r -- 1 oracle oinstall 885 Feb 23 awk. sh-rw-r -- 1 oracle oinstall 216 Feb 23 blank_file-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh drwxr-xr-x 2 oracle oinstall 4096 Feb 15 folder-rw-r -- 1 oracle oinstall 0 Feb 23 ls. out -Rwxr-xr-x 1 oracle oinstall 26 Nov 10 22:30 main. sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22: 39 shelltest. sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :51 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 :52 test2.sh is actually ls-l> ls. from the list of files saved in a directory, go to the question [plain] # calculate the total size of non-directory files: ls-l | awk '/^ [^ d]/{print $9 "\ t" $5; total + = $5} END {print "the total size of the files is:" total} 'awk. out 121 awk. sh 2250 blank_file 216 find. sh 642 ls. out 511 main. sh 26 shelltest. sh 44 test1.sh 99 test2.sh 99 the total size of the files is: 4008 # Replace 279 with 289 and output www.2cto.com awk 'gsub (/279/, 289) {print $0} 'ls. out # output the subscript of xr for the first time. If 0 is returned, awk '{print index ($1, "xr") "" $1}' ls is not found. out 0 total 4-rwxr -- r -- 0-rw-r -- 4-rwxr -- r -- 4 drwxr-xr-x 0-rw-r -- 4-rwxr -xr-x 4-rwxrwxrwx 4-rwxr-xr-x 4-rwxr-xr-x # output the ninth domain Is awk. sh row and calculate the length of the ninth domain awk '$9 = "awk. sh "{print length ($9)" "$9} 'ls. out 6 awk. sh # output the subscript containing the test string in the ninth domain. If 0 is returned, awk '{print match ($9, "test") "" $9}' ls is not found. out 0 0 awk. sh 0 blank_file 0 find. sh 0 folder 0 ls. out 0 main. sh 6 shelltest. sh 1 test1.sh 1 test2.sh # Use # As the separator to split string 123 #456 #789 and save it to the array myarray, and output the length of the array awk' BEGIN {print split ("123 #456 #789", myarray ,"#")} '3 # output the ninth field as the string test1.sh, replace string 99 with 100, and output awk '$9 = "Test1.sh" {print sub (/99/, "100", $0)} {print $0} 'ls. out total 68-rwxr -- r -- 1 oracle oinstall 885 Feb 23 awk. sh-rw-r -- 1 oracle oinstall 216 Feb 23 blank_file-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh drwxr-xr-x 2 oracle oinstall 4096 Feb 15 folder-rw-r -- 1 oracle oinstall 0 Feb 23 ls. out-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22: 30 main. sh-rwxrwxrw X 1 oracle oinstall 44 Jan 7 shelltest. sh 1-rwxr-xr-x 1 oracle oinstall 100 Feb 23 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 test2.sh # output substring awk '$9 = "awk. sh "{print substr ($9, 1, 2)} 'ls. out aw # output sub-string awk 'BEGIN {STR = "lubinsu is the best"} END {print substr (STR, 3)}' ls. out binsu is the best # output substring STR = "lubinsu is the best! "Echo $ STR | awk 'end {print substr ($ STR, 1)} '> awk. out lubinsu is the best! # Format the output string: awk '{printf "%-15 s % s \ n", $1, $9}' ls. out total-rwxr -- r -- awk. sh-rw-r -- blank_file-rwxr -- r -- find. sh drwxr-xr-x folder-rw-r -- ls. out-rwxr-xr-x main. sh-rwxrwxrwx shelltest. sh-rwxr-xr-x test1.sh-rwxr-xr-x test2.sh # perform the operation awk based on the input variable '{if ($5 <size) print $0} 'size = 1000 ls. out total 68-rwxr -- r -- 1 oracle oinstall 885 Feb 23 awk. sh-rw-r -- 1 oracle oinstall 216 Feb 2 3 blank_file-rwxr -- r -- 1 oracle oinstall 642 Feb 15 find. sh-rw-r -- 1 oracle oinstall 0 Feb 23 16:32 ls. out-rwxr-xr-x 1 oracle oinstall 26 Nov 10 22: 30 main. sh-rwxrwxrwx 1 oracle oinstall 44 Jan 7 22: 39 shelltest. sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 test1.sh-rwxr-xr-x 1 oracle oinstall 99 Feb 23 test2.sh # Check the remaining space: df-k | awk '($4 ~ /^ [0-9]/) {printf ("%-15 s % s \ n", $6, $4 )} '/8254824/boot 265512/dev/shm 517676 # check who is using the system www.2cto.com who | awk' {print $1 "is logged on"} 'root is logged on oracle is logged on root is logged on # Use of the array awk 'in in {record = "1234 #567 #890 "; split (record, myarray, "#")} END {for (I in myarray) {print myarray [I]} '/dev/null 1234 567 890

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.