How to Use awk

Source: Internet
Author: User
Awk usage 1. Knowledge Point 1. Domain segmentation-F determines the delimiter. If-F is not added, CAT/etc/passwd is separated by spaces by default. | awk-F: '{print $1, $2, $3}' 2. Extraction domain CAT/etc/passwd | awk-F: '{print $1}' 3. $0 indicates the full local area, that is, all records CAT/etc/passwd | awk-F: '{print $0}' 4. Print CAT/etc/passwd separately | awk-F: '{print $1, $4} '5. print the report header # Cat/etc/passwd | awk-F: 'In in {print "this is system user"} {print $1} '| more6, End of Print Information # Cat/etc/passwd | awk-F: 'begin{ print "this is system user"} {print $1} End {print "=============================="} 'II. Knowledge Point 2 awk regular expression (1 )~ Match CAT/etc/passwd | awk '{if ($0 ~ /Root/) Print $0} '(2) exact match CAT/etc/passwd | awk-F:' {if ($1 ~ /Root/) Print $1} '(3) does not match! CAT/etc/passwd | awk '$0 !~ /Root/'cat/etc/passwd | awk-F: '{if ($1 ~ /Root/) Print $1} '(4) smaller than cat/etc/passwd | awk-F:' {if ($3 <200) Print $3} '(5) smaller than or equal to CAT/etc/passwd | awk-F: '{if ($3 <= 200) Print $3}' (6) greater than or equal to CAT/etc/passwd | awk-F: '{if ($3> = 200) Print $3}' (7) match multiple keywords CAT/etc/passwd | awk-F: '$1 ~ /(Root | user)/'(8) match the first CAT/etc/passwd of the row | awk-F:' $1 ~ /^ Root/'(9) awk use & | condition 1 is true & then condition 2 execution condition 1 is true | then condition 2 does not execute Cat/etc/passwd | awk-F: '{if ($1 = "root" & $5 = "root") Print $0} 'cat/etc/passwd | awk-F: '{if ($1 = "root" | $1 = "sdfsfsdsd ") print $0} 'awk NF and nrnf the number of viewing records in the domain NR the number of read records CAT/etc/passwd | awk-F: '{print NF} 'cat/etc/passwd | awk-F:' {print $1, NR} 'CAT/etc/passwd | awk-F: '{If (NR <10 & $1 ~ /Root/) Print $1} 'awk replace gsub (R, S) CAT/etc/passwd | awk-F: 'gsub (/root/, "alvinzeng ") {print $0} 'exercise questions in this chapter: 1 What is the default delimiter for awk? Space 2 extracts the first and 5th domain names of/etc/passwd and counts their line numbers cat passwd | awk-F: '{print $1, $5, NR} '3 extracts the first field and performs root matching, and then prints the report header content as "this is user" cat passwd | awk-F: 'begin{ print "this is user"} {if ($1 ~ /Root/) Print $1} '4. Extract the last domain. Use "#######" to separate the tail information with Cat passwd | awk-F: '{print $ NF} end {print "========"}' 5. Extract the first domain, please match the keyword cat passwd starting with user | awk-F: '{if ($1 ~ /^ User/) Print $1} '6 match the domain information with the keyword [0-9] in the full area cat passwd | awk-F: '$0 ~ /[0-9]/'7 print out the information entry cat passwd after the row is greater than 30 and match the first MySQL domain | awk-F: '{If (NR> 30 & $1 = "MySQL") Print $0}' 8. Print the last domain in/etc/passwd, how many bash statements are counted? How many nologin logs are there? Cat paasswd | awk-F: '{if ($ NF ~ /Bash $/) Print $ NF} '| print out the user information cat passwd whose uid is smaller than 500 but greater than 200 in WC-9 | awk-F: '{if ($3> 200 & $3 <500) Print $0}' 10. Print the user information with the key DHCP sub-Statement and tell the user on that line? Cat passwd | awk-F: '{if ($0 ~ /DHCP/) Print $0, NR} '11. How many users start with Wu? Cat passwd | awk-F: '{if ($0 ~ /^ Wu/) Print $0} '12 how many users are extracted from the global match home keyword? Do they have Bash logon permissions cat passwd | awk-F: '{if ($0 ~ /Home/& $ NF ~ /Bash $/) Print $0, $ NF} '13 matches the user information with the row number greater than 30 and the ball uid greater than 50 or less than 500 cat passwd | awk-F: '{if ($3> 50 & $3 <500 & Nr> 30) Print $0, NR}' 14: How many matching system users are there? And how many program users are there? The difference between system users and program users is the difference between nologin and bash. 15. match a user with a row number smaller than 30, and how many users match their bash, in the output of their row number?


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.