[Shell Practice]--awk Exercises

Source: Internet
Author: User

1. What is the difference between sed and awk?

(1) Awk: Operations by column (domain); sed: line by row
(2) awk: Text processing language, suitable for extracting text; SED: non-interactive editor for editing text

2. Which parameter does awk use as a split parameter when it is working on a domain?

-F

3. Please print out the first domain of/etc/passwd and add "user account:" Before the contents of the first domain.
# cat/etc/passwd |awk-f: ' {print ' User account: ' $ '

4. Print out the third and fourth domains of/etc/passwd
# awk-f: ' {print $ ' \ t ' $4} '/etc/passwd


5. Match/etc/passwd third field greater than 100 shows full information
# CAT/ETC/PASSWD |awk-f: ' {if ($3>100) print$0} '

6. Please print the first domain, and the print header information is: This is the system user, the print tail information is: "================"
# cat/etc/passwd |awk-f: ' Begin{print "This is the system user"} {print $} end{print "================"} '

7. Print line numbers less than 15, and the last field matches bash information.
# cat/etc/passwd |awk-f: ' {if ($NR <15 && $NF ~/bash/) print$0} '

8. Please print out the first field matching daemon information.
# cat/etc/passwd |awk-f: ' $1== ' daemon '

10. Please print out the sum of the third field numbers
# cat/etc/passwd |awk-f: ' {sum=sum+$3}end{print sum} '
# cat/etc/passwd |awk-f: ' {sum=sum+$3}; End{print sum} '

11. Replace root in/etc/passwd with Gongda, and remember to temporarily replace the output screen to see the effect.
# cat/etc/passwd |awk-f: ' Gsub (/root/, "Gongda") {print $} '

12. Please match the information in the end of the last field bash of passwd, how many
# awk-f: ' ($NF ~ /bash/) {print NR} '/etc/passwd |wc-l
# cat/etc/passwd |awk-f: ' {if ($NF ~/bash/) print$0} ' |wc-l

13. Please also match the information in the passwd file, with mail and bash keywords
# cat/etc/passwd |awk-f: ' $0~/root|mail/'
# awk-f: ' {if ($0~/mail/| | $0~/bash/) print $} '/etc/passwd


14. Please match the information about the passwd third domain total greater than 500.
# cat/etc/passwd |awk-f: ' {if ($3>500) print $} '

15. The contents of the file are as follows:
Mike harrington:[510] 548-1278:250:100:175
Christian dobbins:[408] 538-2358:155:90:201
Susan dalsass:[206] 654-6279:250:60:50
Archie mcnichol:[206] 548-1348:250:100:175
Jody savage:[206] 548-1278:15:188:150
Guy quigley:[916] 343-6410:250:100:175
Dan savage:[406] 298-7744:450:300:275
Nancy mcneil:[206] 548-1278:250:80:75
John goldenrod:[916] 348-4278:250:100:175
Chet main:[510] 548-5258:50:95:135
Tom savage:[408] 926-3456:250:168:200
Elizabeth stachelin:[916] 440-1763:175:75:300
which
Mike Harrington Name
[510] 548-1278 Telephone
250:100:175 donations in the past three months

(1) Show all phone numbers
# awk-f: ' {print $} ' file

(2) Show Dan's phone number
# awk-f: ' {if ($1~/dan/) print$2} ' file

(3) Show Susan's name and phone number
# awk-f: ' {if ($1~/susan/) print$1,$2} ' file

(4) Show all surnames starting with D
# awk-f: ' {if ($1~/^d/) print$1} ' file

(5) Show all names beginning with a C or E
# awk-f: ' {if ($1~/^[c| e]/) print$1} ' file

(6) Displays all names with only four characters, where you can use the length function, for example: Length ($) ==10 the character 10
# cat file |awk-f: ' {print$1} ' |awk ' {if (length ($) ==4) print '} '

(7) Show all names with area code 916
# cat file |awk ' {print$2} ' |awk-f: ' {if ($2~/916/) print$1} '

(8) Show Mike's donation. Each value is displayed with a $ start. such as $250$100$175
# cat file |awk-f: ' {if ($1~/mike/) print "$" $ "$" $ "$4" $ "$"

(9) display last name followed by a comma and a name
# cat file |awk-f: ' {print$1} ' |awk ' {print$2 ', ' $ '

[Shell Practice]--awk Exercises

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.