Introduction
grep is a powerful text-search command that can be used to search for a file that contains a specified search, it can use regular expressions to do complex filtering, it can also be used to filter other commands to the pipeline, such as the one we commonly use to analyze a single process is to take advantage of it "ps-ef |grep command ".
Grammar
grep [OPTION] ... PATTERN [FILE] ...
The default no parameter is to display the row record on the match, you can use--help to see the parameters it supports, this article will only list some of the more commonly used commands.
-A: The content of the output do not omit binary data-B<N>: Outputs the rows that match N bytes on. -C: Show only the number of rows that match the criteria, not the content-D: You need to set this parameter when you are looking for a directory instead of a file, otherwise you will get an error-H: Add the name of the file to which the line belongs before the content line of the output. -h: The default option is not to precede the output line with the file name that the row belongs to. -I: Ignore case-L: List file names for files that do not match the found content-L: Lists file names for files that match the Find content-M<N>: Outputs only the specified n rows on the match. -o: Displays only the contents of the search and does not show the other contents of the line. -Q: Nothing is output-r: If you need to traverse all the files of the entire directory, you can use this parameter-V: Show line information on no match, opposite to default-V: Display version information
Regular expressions
When grep is filtered with a regular, an escape character is required for {} ().
Command |
Description |
^ |
Match at the opening of a character |
$ |
Match at the end of a character |
. |
Match any character (including carriage return and new line) |
[....] |
Match any single character within the parentheses |
[M-n] |
Matches any single character from M to N, for example [0-9],[a-z],[a-z] |
[^..] |
Cannot match any single character in parentheses |
A * |
Match 0 or more A, including an empty |
A\{m\} |
Match m A |
A\{m,\} |
Match m or more a |
A\{m,n\} |
Match m to n a |
\(....\) |
Make a single element of a pattern element, such as (do) * meaning to match 0 or more do |
grep common usage
Create test data
-- Help >/tmp/grep.text
1. Filtering for other commands
Querying for processes that contain sbin
[Email protected] ~]# ps-ef |grep "Sbin"
Root 1 0 0 11:04? 00:00:01/sbin/init
Root 543 1 0 11:04? 00:00:00/sbin/udevd-d
Root 1559 1 0 11:04? 00:00:00/usr/sbin/vmware-vmblock-fuse-o subtype=vmware-vmblock,default_permissions,allow_other/var/run/ Vmblock-fuse
Root 1580 1 0 11:04? 00:00:18/usr/sbin/vmtoolsd
Root 1992 1 0 11:04? 00:00:00/sbin/rsyslogd-i/var/run/syslogd.pid-c 5
Root 2078 1 0 11:04? 00:00:00/usr/sbin/modem-manager
Root 2122 1 0 11:04? 00:00:00/usr/sbin/wpa_supplicant-c/etc/wpa_supplicant/wpa_supplicant.conf-b-u-f/var/log/wpa_supplicant.log-p/ Var/run/wpa_supplicant.pid
Root 2133 1 0 11:05? 00:00:00/usr/sbin/acpid
Root 2219 1 0 11:05? 00:00:00/USR/SBIN/BLUETOOTHD--udev
Root 2298 1 0 11:05? 00:00:00/usr/sbin/sshd
Root 3172 1 0 11:05? 00:00:00/usr/sbin/abrtd
Root 3199 1 0 11:05? 00:00:00/usr/sbin/atd
Root 3215 1 0 11:05? 00:00:00/usr/sbin/gdm-binary-nodaemon
Root 3220 1 0 11:05 tty2 00:00:00/sbin/mingetty/dev/tty2
Root 3222 1 0 11:05 tty3 00:00:00/sbin/mingetty/dev/tty3
Root 3224 1 0 11:05 Tty4 00:00:00/sbin/mingetty/dev/tty4
Root 3227 543 0 11:05? 00:00:00/sbin/udevd-d
Root 3228 1 0 11:05 tty5 00:00:00/sbin/mingetty/dev/tty5
Root 3230 1 0 11:05 tty6 00:00:00/sbin/mingetty/dev/tty6
Root 3231 543 0 11:05? 00:00:00/sbin/udevd-d
Root 3261 1 0 11:05? 00:00:00/usr/sbin/console-kit-daemon--no-daemon
Root 5923 2071 0 14:12? 00:00:00/sbin/dhclient-d -4-sf/usr/libexec/nm-dhcp-client.action-pf/var/run/dhclient-eth0.pid-lf/var/lib/ dhclient/dhclient-3a7ff4d9-5a09-46b1-bb20-0298a18e6b78-eth0.lease-cf/var/run/nm-dhclient-eth0.conf eth0
Root 6294 6044 0 15:50 pts/1 00:00:00 grep sbin
2. Query the number of rows
Query thenumber of rows that contain "-D" [[email protected] ~]-C "\-/ TMP/6
3.$
querying for lines ending in lines
[ [email protected] ~ ] # grep "lines$" / tmp/ grep.txt - x, -- line-regexp force PATTERN to match only whole lines - V, -- invert-match select non-matching lines - B, -- byte-offset Print the byte offse T with output lines - N, -- line-number Print line number with output lines
4.{m,}
query for rows that contain 2 or more s[[email protected] ~]# grep "\ (s\) \{2,\}"/Tmp/grep.txt PATTERN is, by default, a basic regular expression (BRE). -E--Extended-regexp PATTERN is an extended regular expression (ERE) -G--basic-regexp PATTERN is a basic regular expression (BRE) -P--Perl-regexp PATTERN is a Perl regular expression -S--no-messages Suppress error messages -H--no-filename Suppress the file name prefix on output -Q--quiet,--silent suppress all normal output --Binary-files=type assume that binary files is TYPE;-R isGiven-otherwise.IfFewer than, given, assume-H.
Summary
Note using the escape character, if you use a regular to find not a single character but more than one character need to use () to enclose multiple characters, grep has a lot of use of the technique is not listed here.
Note: pursuer.chen Blog:http://www.cnblogs.com/chenmh This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link. Welcome to the exchange of discussions |
Linux grep command