1.grep
· Grep: global regular expression print__ global regular expression output.
· Grep command is used to find (filter) file qualifying string.
· Grep regular expression closely.
■ Syntax
grep - [ parameters ] <filename>
■ Common parameters
- -A <n (number of rows)>: the line to be drawn and displayed in line with the query, and displays the contents of the n-th row after row.
- -B <n (number of lines)>: To extract and display the rows that match the query, and displays the contents of n rows before the line.
- -c: count the number of rows to be displayed in line query rows.
- -i: Ignore the character size.
- -n: To extract and display the rows matching the query, and mark the number of the row in front of result rows.
- -v: withdrawn conform to query and display all lines except the content.
■ Instance
・grep -A<n(行数)> filename
Copy / etc / passwd into the / home / yubx directory
[root@dell-ap-01 yubx]# cp /etc/passwd /home/yubx
/ Vim commands modify the file with vi, the contents of the passwd file amended as follows:
1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin 7 dbus:x:81:81:System message bus:/:/sbin/nologin 8 polkitd:x:999:998:User for polkitd:/:/sbin/nologin 9 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 10 postfix:x:89:89::/var/spool/postfix:/sbin/nologin 11 chrony:x:998:996::/var/lib/chrony:/sbin/nologin 12 apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin 13 tomcat:x:1000:1000::/home/tomcat:/bin/bash 14 oracle:x:1001:1006::/home/oracle:/bin/bash 15 tomcat0101abcaaaaccc 16 1dddddddddkkkkkkkkkkkkkkkkkaaaaa 17 2fffffffffffffffffff 18 3aaaaaaaaaaaaaa 19 4hhhhhhhhhhhhhhh 20 5ggggggggggggggggggg 21 ~ 22 ~
Command execution results:
1 [root@dell-ap-01 yubx]# grep -A1 'tomcat' passwd 2 tomcat:x:1000:1000::/home/tomcat:/bin/bash 3 oracle:x:1001:1006::/home/oracle:/bin/bash 4 tomcat0101abcaaaaccc 5 1dddddddddkkkkkkkkkkkkkkkkkaaaaa 6 [root@dell-ap-01 yubx]# grep -A3 'tomcat' passwd 7 tomcat:x:1000:1000::/home/tomcat:/bin/bash 8 oracle:x:1001:1006::/home/oracle:/bin/bash 9 tomcat0101abcaaaaccc 10 1dddddddddkkkkkkkkkkkkkkkkkaaaaa 11 2fffffffffffffffffff 12 3aaaaaaaaaaaaaa 13 [root@dell-ap-01 yubx]# grep -A10 'tomcat' passwd 14 tomcat:x:1000:1000::/home/tomcat:/bin/bash 15 oracle:x:1001:1006::/home/oracle:/bin/bash 16 tomcat0101abcaaaaccc 17 1dddddddddkkkkkkkkkkkkkkkkkaaaaa 18 2fffffffffffffffffff 19 3aaaaaaaaaaaaaa 20 4hhhhhhhhhhhhhhh 21 5ggggggggggggggggggg 22 [root@dell-ap-01 yubx]
Resolution:
We can see from the contents of the file, the line containing "tomcat" character has two lines, namely line 13 and line 15.
- -A1 occasions, search for content in line with the line is drawn, and the content relative to the target line 1 line was also out of here.
- -A3 occasions, search for content in line with the line is drawn, and the content relative to the target line 3 line was also out of here.
- -A10 occasions, search for content in line with the line is drawn, and the content relative to the target line 10 is also out of line here. (Only a maximum of 5 line in the file)
In summary, -A <n> means that extracts and displays content corresponding to the search target line, the target line and the contents of all n lines.
・grep -B<n(行数)> filename
Role: extract and display the target line corresponding to the search content, and the goal line before the contents of all n rows.
※: -A and -B due almost, there is not a way of example.
· Grep -c 'to query string' filename
Role: print only the number of lines that match the text line, matching content is not displayed.
Example: Based on the above passwd file, execute the following commands.
[root@dell-ap-01 yubx]# grep -c 'tomcat' passwd 2
The results of the above means, passwd file has two lines containing "tomcat" string.
· Grep -i 'to query string' filename
Role: print only the number of lines that match the text line, matching content is not displayed.
Example: Based on the above passwd file, the following additional line.
... ...
4hhhhhhhhhhhhhhh
5ggggggggggggggggggg TOMCAT123456uuusssaaadd
~
Execute the following command:
[root@dell-ap-01 yubx]# grep -i 'TOMCAT' passwd tomcat:x:1000:1000::/home/tomcat:/bin/bash tomcat0101abcaaaaccc TOMCAT123456uuusssaaadd [root@dell-ap-01 yubx]# grep -i 'tomcat' passwd tomcat:x:1000:1000::/home/tomcat:/bin/bash tomcat0101abcaaaaccc TOMCAT123456uuusssaaadd [root@dell-ap-01 yubx]#
· Grep -n 'to query string' filename
Role: To extract and display the rows that match the query, and mark the number of the row in front of result rows.
Example: Based on the above document, the following code
[root@dell-ap-01 yubx]# grep -n 'tomcat' passwd
13:tomcat:x:1000:1000::/home/tomcat:/bin/bash
15:tomcat0101abcaaaaccc
[root@dell-ap-01 yubx]#
The results of the above "13, 15" is the line number where the target file line.
· Grep -v 'to query string' filename
Role: To extract and display the query in line outside all rows.
Example: Based on the above document, the following code
[root@dell-ap-01 yubx]# grep -v 'tomcat' passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin oracle:x:1001:1006::/home/oracle:/bin/bash 1dddddddddkkkkkkkkkkkkkkkkkaaaaa 2fffffffffffffffffff 3aaaaaaaaaaaaaa 4hhhhhhhhhhhhhhh 5ggggggggggggggggggg TOMCAT123456uuusssaaadd [root@dell-ap-01 yubx]#
As can be seen from the results above, contains "tomcat" that the two lines are not printed.
■ Written in the last
It cited here only a few grep common, example of the operation of the string.
This is just tip of the iceberg grep powerful features.
There are many parameters here and no introduction. Specific parameters and parameter action can be viewed using "grep --help."
grep is more powerful and regular conjunction, and many parameters can be used in combination.
In the subsequent regular [linux] essay, we will continue to explain the grep function.
the above.