linux shell awk exercise

1. Get the remaining size of the root partition

df -hl | awk '/\/$/ {print $4}'

Insert image description here

2. Get the current machine ip address

ifconfig | awk '/\<inet\>/ {print $2}'| head -1

Insert image description here

3. Count the 5 most visited IPs in apache’s access.log


I don’t have this file
```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

Insert image description here

5. Match any line containing root or net or ucp in /etc/passwd

awk -n '/root|net|ucp/' /etc/passwd

Insert image description here

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

Insert image description here

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

Insert image description here

8. Please print out the third and fourth fields of /etc/passwd

awk -F : '{print "UID:"$3" GID:"$4}' /etc/passwd

Insert image description here

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

Insert image description here

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

Insert image description here

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}'

Insert image description here

13. Please also match the information with the keyword mail or bash in the passwd file.

awk '$0~/mail|bash/' /etc/passwd

Insert image description here

Guess you like

Origin blog.csdn.net/m0_51828898/article/details/128571588