Linux grep command and linuxgrep command

Source: Internet
Author: User

Linux grep command and linuxgrep command
Introduction

Grep is a powerful text search command that can be used to search for a file that contains specified search content. It can use regular expressions to perform complex filtering operations, it can also send other commands to the MPs queue for filtering. For example, we often use ps-ef | grep command to analyze a single process ".

 

Syntax

Grep [OPTION]... PATTERN [FILE]...

By default, no parameter is used to display matching Row Records. You can use -- help to view the supported parameters. This article only lists some commonly used commands.

-A: Do not ignore the binary data in the output content-B <n>: the output matches n Bytes of rows. -C: only display the number of rows that meet the conditions, not the content.-d: this parameter must be set when you are looking for a directory rather than a file. Otherwise, an error is returned.-H: add the name of the row to the output row. -H: the file name to which the row belongs is not added before the output row. This is the default option. -I: Ignore case-L: list file names of files that do not match the search content-l: list file names of files that match the search content-m <n>: only the specified n rows matching are output. -O: only the searched content is displayed, and other content of the row is not displayed. -Q: No output-r: If you want to traverse all files in the entire directory, you can use this parameter-v: to display the row information that does not match, opposite to the default value-V: show version information

Regular Expression

Escape characters are required for {} () When grep is used in combination with regular expressions for filtering.

Command

Description

^

Match the character when it is enabled

$

Match at the end of the character

.

Match any character (including carriage return and new line)

[...]

Match any single character in brackets

[M-n]

Match any single character between m and n, for example [0-9], [a-z], [A-Z]

[^...]

Cannot match any single character in the brackets

A *

Matches 0 or multiple a, including null

A \ {m \}

Match m

A \ {m ,\}

Match m or more

A \ {m, n \}

Match m to n

\(.... \)

Make the mode element into a single element. For example, (do) * means to match 0 multiple or multiple do

 

Common grep usage

Create Test Data

grep --help >/tmp/grep.text

1. filter other commands

Query processes that contain sbin

[Root @ localhost ~] # Ps-ef | grep "sbin"
Root 1 0 0 11: 04? 00:00:01/sbin/init
Root 543 1 0 :04? 00:00:00/sbin/udevd-d
Root 1559 1 0 :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 :04? 00:00:18/usr/sbin/vmtoolsd
Root 1992 1 0 :04? 00:00:00/sbin/rsyslogd-I/var/run/syslogd. pid-c 5
Root 2078 1 0 :04? 00:00:00/usr/sbin/modem-manager
Root 2122 1 0 :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/restart
Root 2133 1 0 11: 05? 00:00:00/usr/sbin/acpid
Root 2219 1 0 11: 05? 00:00:00/usr/sbin/mongothd -- 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/TPD-binary-nodaemon
Root 3220 1 0 00:00:00 05 tty2/sbin/mingetty/dev/tty2
Root 3222 1 0 00:00:00 05 tty3/sbin/mingetty/dev/tty3
Root 3224 1 0 00:00:00 05 tty4/sbin/mingetty/dev/tty4
Root 3227 543 0? 00:00:00/sbin/udevd-d
Root 3228 1 0 00:00:00 05 tty5/sbin/mingetty/dev/tty5
Root 3230 1 0 00:00:00 05 tty6/sbin/mingetty/dev/tty6
Root 3231 543 0? 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? 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 00:00:00 pts/1 grep sbin

2. query the number of rows

Query the number of rows containing "-d" [root @ localhost ~] # Grep-c "\-d"/tmp/grep.txt 6

3. $

Query rows ending with lines

[root@localhost ~]# 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 offset with output lines  -n, --line-number         print line number with output lines

4. {m ,}

Query rows that contain 2 or more S [root @ localhost ~] # 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 are TYPE;-r is given,-otherwise. if fewer than two FILEs are given, assume-h.
Summary

Note: Use escape characters. If you want to use a regular expression, you must use () to enclose multiple characters instead of a single character but multiple characters, there are still a lot of tips for using grep, which will not be listed one by one here.

 

 

Note:

Author: pursuer. chen

Blog: http://www.cnblogs.com/chenmh

All essays on this site are original. You are welcome to repost them. However, you must indicate the source of the article and clearly give the link at the beginning of the article.

Welcome to discussion

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.