grep command exercise

Keywords: Linux regex

1. Show / etc/passwd

Lines in the file that do not end with / bin/bash;

grep -v "/bin/bash$" /etc/passwd

2. Find the two or three digits in the / etc/passwd file;

grep -E '\<[0-9]{2,3}\>' /etc/passwd

3. Find the line in the / etc/rc.d/rc.sysinit or / etc/grub2.cfg file that starts with at least one white space character and is followed by a non white space character;

grep '^[[:space:]]\+[^[:space:]]' /etc/grub2.cfg

4. Find the line ending with 'list' followed by 0, 1 or more white space characters in the result of "netstat -tan" command;

netstat -tan | grep 'LISTEN[[:space:]]*$' 

5. Find all lines in the / proc/meminfo file that begin with uppercase or lowercase S; There are at least three implementation methods;

 grep -i "^s" /proc/meminfo
 grep "^[sS]" /proc/meminfo
 grep -E "^(s|S)" /proc/meminfo

6. Display the relevant information of root, centos or user1 users on Xiaoqian system;

grep -E "^(root|centos|user1)\>" /etc/passwd

7. Find the line in the / etc/rc.d/init.d/functions file where a word is followed by a parenthesis; Tip: in_ Or start with a letter and end with (), which is actually the name of our shell function.
For example:

checkpid()
__kill_pids_term_kill_checkpids()
__kill_pids_term_kill()
__pids_var_run()
__pids_pidof()
daemon()
grep  -E  -o  "[_[:alnum:]]+\(\)"  /etc/rc.d/init.d/functions

8. Use echo command to output an absolute path, and use egrep to get the base name;
Prompt: take out its path name; Similar to the result of executing the dirname command on it; For example, / etc/sysconfig /, the base name is / etc

echo /etc/sysconfig/ | egrep -o "^/.*[^/]" | egrep -o "^/.*/" | egrep -o  "^/.*[^/]"

9. Find the IP address in the ifconfig command result;

ifconfig | grep -A 2 ens33 | grep -w inet | grep -Eo 't.*n'| cut -d' ' -f2

10. Add users bash, testbash, basher and nologin (its shell is / sbin/nologin); Then find the line in the / etc/passwd file where the user name is the same as the shell name;

grep  -E  "^([^:]+\>).*\1$"  /etc/passwd
egrep "^(.*):.*\1$" /etc/passwd

Test requirements

: write your regular expression according to the requirements of the topic and post the output.

file.txt Document content:
48      Dec     3BC1977 LPSX   68.00   LVX2A   138
483     Sept    5AP1996 USP    65.00   LVX2C   189
47      Oct     3ZL1998 LPSX   43.00   KVM9D   512
219     dec     2CC1999 CAD    23.00   PLV2C   68
484     nov     7PL1996 CAD    49.00   PLV2C   234
483     may     5PA1998 USP    37.00   KVM9D   644
216     sept    3ZL1998 USP    86.00   KVM9E   23

4

practice:
1 the total number of lines containing the "48" string

 grep -i '48' file.txt |wc -l
 grep -c 48 file.txt

2 displays the line numbers of all lines containing the "48" string

 grep -n 48 file.txt
 grep -n '48' file.txt

3 exactly match lines containing only the "48" string
PS: a more effective way to extract exact matches using grep is to add \ > after extracting strings

grep  "48\>" file.txt

4 extract city locations with codes 4 8 4 and 4 8 3

PS: use[ ]To specify the string range
[root@localhost ~]# grep "48[43]" file.txt
483     Sept    5AP1996 USP    65.00   LVX2C   189
484     nov     7PL1996 CAD    49.00   PLV2C   234
483     may     5PA1998 USP    37.00   KVM9D   644
[root@localhost ~]# grep "48[4|3]" file.txt
483     Sept    5AP1996 USP    65.00   LVX2C   189
484     nov     7PL1996 CAD    49.00   PLV2C   234
483     may     5PA1998 USP    37.00   KVM9D   644

5 display so that the beginning of the line is not 4 or 8
PS: you can use the ^ mark in square brackets to indicate that the beginning of the line is not a character

 grep -v "^[48]" file.txt
grep -c 48 file.txt
[root@XKWB5705 zhaoyj]# grep -v "^[4|8]" file.txt 

[root@XKWB5705 zhaoyj]# grep -v "^[48]" file.txt 

[root@XKWB5705 zhaoyj]# grep -v "^[4,8]" file.txt 

[root@XKWB5705 zhaoyj]# grep "^[^48]" file.txt    

#This is the simplest way directly

6 show rows with September
PS: use the - i switch to mask the case sensitivity of month Sept

 grep -i "sept" file.txt

7 shows all codes that start with K and end with D
PS: dot. Represents any character. Since the code is a five character string, three dots are used in the middle to represent any character

 grep -i "K...D" file.txt

8 display the code with the first two uppercase letters, the middle two arbitrary letters and ending in C
PS: [A-Z] indicates any capital letter

grep  "[A-Z]\{2\}..C" file.txt

9 query all records starting with 5 and ending in 1996 or 1998

 grep  "5..199[6,8]" file.txt

 grep '5..[1998|1996]' file.txt
 grep '5..[19981996]' file.txt

10 take out all three digit records

 grep "\<[0-9][0-9][0-9]\>" file.txt
 grep '\<[0-9]\{3\}\>' file.txt

11 take out the records of all letter and number combinations

[root@localhost ~]# grep '[a-zA-Z0-9]' file.txt

12 take out all non digital records

[root@localhost ~]# grep '[^0-9]' file.txt

13 take out all non alphabetic records

[root@localhost ~]# grep  '[0-9]' file.txt

Posted by hernan on Thu, 14 Oct 2021 11:51:12 -0700