Analysis of linux grep regular expression exercises

Reflections and suggestions:

  • Because of the rich grammar, think about how to solve a problem as much as possible ;

  • Think more about the scenarios in which such answers can be applied;

  • Finished a question pause to think about

  • Familiar with the document ' man grep '

Basic part

1. Use multiple ways to output all lines in the /proc/meminfo file that start with uppercase or lowercase s

Tip: Ignore case; different ways of writing expansion

grep -i '^ s' / proc / meninfo

echo -e "sb\nSb\nsB\nSB" | grep -i '^s'

grep -E "^(s|S)" /etc/fstab

grep "^\(s\|S\)" /etc/fstab

 

2. Display information about root, centos, or user users on the current system

grep -E '^root|^centos|^root' /etc/passwd

 

3. Output all function names in the file /etc/rc.d/init.d/functions (the line with parentheses after a word)

Tips: the beginning of the word, the end of the bracket

grep -P "\b\w+\(\)" /etc/rc.d/init.d/functions

 

4. Output the last file name ssc in the path string /ssa/sssb/ssc

Tip: Characters other than slash / must appear at least once (the file name is not empty), and end with a character other than slash /

echo /sss/ssss/sss | grep -o -P "[^/]+$"

Variation: output the path except the last file name

Tip: Any character and end with a slash

echo /sss/ssss/sss | grep -o -P ".*/"

 

5. Find the number between 0-255 in the ifconfig command;

Tip: Classified discussion/expanded regular

ifconfig | grep -E "\b([1-9] | [1-9][0-9] | 1[0-9[0-9] | 2[0-4][0-9] | 25[0-5] )\b"

 

6. Find the line where the file username is the same as the shell name

Tip: The user name is the first word, the shell name is the last word, and the middle is treated as any character

grep -P '^\b(\w+)\b.*\1$' /etc/fstab

 

7. Output the ip address in /???/???

Tip: The host number is between 1-254, which can be modified according to the above 0-255, but it is more complicated, and it is more troublesome to output the private IP address.

grep -Po '(? <= inet) (. *) (? = net)' / ??? / ???

 

Advanced part

8. Recursively search for the character main{} in all .php and html files

grep"main()" . -r --include *.{php,html}

 

9. Recursively search for lines ending with /sbin/nologin in multiple file directories

grep -r -n '/sbin/nologin$' /etc

 

10. Search for the line that does not start with an English letter in fstab and display the line number

Tip: Start with an English letter and negate

grep -E -v -n '^ [a-zA-Z]' / etc / fstab

Someone'^ [^ a-zA-Z]'

 

11.Filter blank lines and lines beginning with #

grep -E -v '^$|^#' /etc/fstab

 

12. Find out the line in the file with at least one blank character followed by a non-blank character

grep -P '^ \ s \ S +' /etc/grub2.cfg

 

13. List all system users

Prompt: UID: 1-999

grep -P '\b[1-9]\d{0,2}\b' /etc/passwd

cat /etc/passwd | awk -F: '$3>1 && $3<1000{print $0}'

 

14. In the filter file, the line starting with a word or a word preceded by _ will be displayed, and the two lines before and after are displayed

Tip: _?; must have words

grep -C2 -P '_?\b\w+\b' /etc/rc.d/init.d/functions

Guess you like

Origin blog.csdn.net/weixin_31789689/article/details/106796866