Article directory
- 1. Get the remaining size of the root partition
- 2. Get the current machine ip address
- 3. Count the 5 most visited IPs in apache’s access.log
- 4. Print usernames and uids with UID greater than 500 in /etc/passwd
- 5. Match any line containing root or net or ucp in /etc/passwd
- 6. Process the following file contents, extract the domain names and count and sort them according to the domain names (Baidu Sohu interview questions)
- 7. Please print out the first domain of /etc/passwd and add "User Account:" in front of all the contents of the first domain.
- 8. Please print out the third and fourth fields of /etc/passwd
- 9. Please print the first field, and print the header information as: This is the system user, and print the tail information as: "================"
- 10. Please print out the information of the first domain matching daemon.
- 11. Please replace root in /etc/passwd with gongda. Remember to temporarily replace the output screen to see the effect.
- 12. Please match the information at the end of the last domain bash of passwd. How many pieces are there?
- 13. Please also match the information with the keyword mail or bash in the passwd file.
1. Get the remaining size of the root partition
df -hl | awk '/\/$/ {print $4}'
2. Get the current machine ip address
ifconfig | awk '/\<inet\>/ {print $2}'| head -1
3. Count the 5 most visited IPs in apache’s access.log
```linux cat access.log|awk '{print $1}'|sort -n|uniq -c|sort -nr|head -5 ```
4. Print usernames and uids with UID greater than 500 in /etc/passwd
awk -F : '$3>500 {print "用户名:"$1"\t\tUID: "$3}' /etc/passwd
5. Match any line containing root or net or ucp in /etc/passwd
awk -n '/root|net|ucp/' /etc/passwd
6. Process the following file contents, extract the domain names and count and sort them according to the domain names (Baidu Sohu interview questions)
test.txt
http://www.baidu.com/index.html
http://www.baidu.com/1.html
http://post.baidu.com/index.html
http://mp3.baidu.com/index.html
http://www.baidu.com/3.html
http://post.baidu.com/2.html
awk -F / '{print $3}' test.txt| sort -r| uniq -c
7. Please print out the first domain of /etc/passwd and add "User Account:" in front of all the contents of the first domain.
awk -F : '{print "用户账号:"$1}' /etc/passwd
8. Please print out the third and fourth fields of /etc/passwd
awk -F : '{print "UID:"$3" GID:"$4}' /etc/passwd
9. Please print the first field, and print the header information as: This is the system user, and print the tail information as: "================"
awk -F : 'BEGIN{print "这是系统用户"}{print $1}END{print "====="}' /etc/passwd
10. Please print out the information of the first domain matching daemon.
awk -F : '/^daemon/ {print $1}' /etc/passwd
11. Please replace root in /etc/passwd with gongda. Remember to temporarily replace the output screen to see the effect.
sub matches the first occurrence of a string that matches the pattern, equivalent to sed 's//'.
gsub matches all strings matching the pattern, equivalent to sed 's//g'.
For example:
awk '{sub(/Mac/,"Macintosh");print}' urfile 用Macintosh替换Mac
awk '{sub(/Mac/,"MacIntosh",$1); print}' file 第一个域内用Macintosh替换Mac
Replacing the above sub with gsub means replacing all characters in the field that meets the conditions.
Damn, I'm actually experimenting with regular rules
awk -F : 'gsub(/root/,"gongda")' /etc/passwd
12. Please match the information at the end of the last domain bash of passwd. How many pieces are there?
Thinking that these are all awk exercises, I used AWK to do it.
awk -F : '{print $7}' /etc/passwd | awk -F / '{if($3=="bash"){x++}} END{print x}'
13. Please also match the information with the keyword mail or bash in the passwd file.
awk '$0~/mail|bash/' /etc/passwd