The grep command in Linux-from cyber

Source: Internet
Author: User
Tags grep regular expression line editor egrep

1. Role
The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. grep full name is global Regular Expr

Ession Print, which represents the global regular expression version, whose usage rights are all users.

2. Format
grep [Options]

3. Main parameters
[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

4.grep command uses a simple instance
$ grep ' test ' d*
Displays all rows that contain test in a file that begins with D.
$ grep ' test ' AA bb cc
Displays the line that matches test in the aa,bb,cc file.
$ grep ' [a-z]\{5\} ' AA
Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.
$ grep ' W esT.*\1′aa
If West is matched, es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. If you use Egrep or GREP-E, you do not have "\" number to escape, directly written as ' W (es) t.*\1′ on it.

5.grep commands use complex instances
Suppose you are searching for a file with the string ' magic ' in the '/usr/src/linux/doc ' directory:
$ grep magic/usr/src/linux/doc/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* How does I use the Magic sysrq key?
Where the file ' Sysrp.txt ' contains the string and discusses the functionality of SYSRQ.
By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' is listed in the following form:
Grep:sound:Is a Directory
This may make the output of ' grep ' difficult to read. There are two ways to solve this problem:
Explicit Requirements Search subdirectories: grep-r
or Ignore subdirectories: grep-d Skip
If you have a lot of output, you can go through the pipe to read it on ' less ':
$ grep magic/usr/src/linux/documentation/* | Less
This makes it easier for you to read.

One thing to note is that you need to provide a way to filter the file (search for all files in *). If you forget, ' grep ' will wait until the program is interrupted. If you are experiencing such a situation, press <ctrl c>, and then try again.

Here are some interesting command-line arguments:
Grep-i pattern Files: Search by case-insensitive. The default case is case-sensitive,
Grep-l pattern Files: Lists only the matching file names,
Grep-l pattern Files: Lists mismatched file names,
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
Grep-c number pattern files: matching contexts display [number] lines, respectively,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.

Grep-n pattern files to display line number information

Grep-c pattern files to find total rows

Here are some special symbols for searching:
\< and \> each mark the beginning and end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ', etc.
grep ' \<man ' matches ' manic ' and ' man ', but not ' Batman ',
grep ' \<man\> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to a matching string at the beginning of the line,
' $ ': refers to a matching string at the end of the line,



Grep command Usage Daquan 1, Parameters:
-I: Ignore case
-C: Print the number of matching rows
-L: Find included matches from multiple files
-V: Find rows that do not contain matches
-N: Print row and row labels that contain matches

2, RE (regular expression)
\ ignore the original meaning of special characters in regular expressions
^ matches the start line of the regular expression
$ matches the end line of a regular expression
\< starting from the line that matches the regular expression
\> to the end of the line that matches the regular expression
[] A single character, such as [a] that a meets the requirements
[-] range, such as [A-z], i.e. a,b,c to Z all meet the requirements
. All the individual characters
* All characters, length can be 0

3. Example
# Ps-ef | grep in.telnetd
Root 19955 181 0 13:43:53? 0:00 in.telnetd

# more Size.txt The contents of the size file
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# More Size.txt | grep ' [A-b] ' range, such as [A-z], i.e. a,b,c to Z all meet the requirements
b124230
b034325
a081016
a022021
a061048
b103303
A013386
b044525
# More Size.txt | grep ' [A-b] ' *
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# More Size.txt | grep ' B ' single character; if [a] is a compliance requirement
b124230
b034325
b103303
b044525
# More Size.txt | grep ' [BB] '
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345

# grep ' Root '/etc/group
Root::0:root
Bin::2:root,bin,daemon
Sys::3:root,bin,sys,adm
Adm::4:root,adm,daemon
Uucp::5:root,uucp
Mail::6:root
Tty::7:root,tty,adm
Lp::8:root,lp,adm
Nuucp::9:root,nuucp
Daemon::12:root,daemon

# grep ' ^root '/etc/group matches the start line of the regular expression
Root::0:root

# grep ' UUCP '/etc/group
Uucp::5:root,uucp
Nuucp::9:root,nuucp

# grep ' \&LT;UUCP '/etc/group
Uucp::5:root,uucp

# grep ' root$ '/etc/group matches the end line of the regular expression
Root::0:root
Mail::6:root

# More Size.txt | Grep-i ' B1. '-I: Ignore case

b124230
b103303
B103303

# More Size.txt | Grep-iv ' B1. '-V: Find rows that do not contain matches

b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
A013386
b044525
m8987131
B081016
M45678
BADc2345

# More Size.txt | Grep-in ' B1. "
1:b124230
9:b103303
15:b103303

# grep ' $ '/etc/init.d/nfs.server | Wc-l
128
# grep ' \$ '/etc/init.d/nfs.server | Wc–l ignores the original meaning of special characters in regular expressions

15
# grep ' \$ '/etc/init.d/nfs.server
Case "$" in
>/tmp/sharetab.
["x$fstype"! = Xnfs] &&
echo "$path \t$res\t$fstype\t$opts\t$desc"
>>/tmp/sharetab.
/usr/bin/touch-r/etc/dfs/sharetab/tmp/sharetab.
/USR/BIN/MV−F/TMP/saea. /etc/dfs/sharetab
If [-f/etc/dfs/dfstab] &&/usr/bin/egrep-v ' ^[]* (#|$) '
If [$STARTNFSD-eq 0-a-f/etc/rmmount.conf] &&
If [$startnfsd-ne 0]; Then
elif [!-n "$_init_run_level"]; Then
While [$wtime-GT 0]; Do
Wtime= ' Expr $wtime-1 '
If [$wtime-eq 0]; Then
echo "Usage: $ {Start | Stop} "

# more Size.txt

The test file
Their is files
The end

# grep ' the ' size.txt
The test file
Their is files

# grep ' \<the ' size.txt
The test file
Their is files

# grep ' the\> ' size.txt
The test file

# grep ' \<the\> ' size.txt
The test file

# grep ' \<[tt]he\> ' size.txt
The test file

==================================================================

1, Introduction
A multipurpose Text Search tool that uses regular expressions. This Php?name=%c3%fc%c1%ee "onclick=" Tagshow (event) "class=" T_tag "> Command was originally a php?name=% in the Ed line editor C3%fc%c1%ee "onclick=" Tagshow (event) "class=" T_tag "> Command/filter:
g/re/p--Global-regular expression-print.
Basic format
grep pattern [File ...]
(1) grep search string [filename]
(2) grep regular expression [filename]
Searches the file for all occurrences of the pattern, which can be either a string to be searched or a regular expression.
Note: It is best to use double quotation marks when entering a string to search for, and when using regular expressions in pattern matching, be careful to use single quotes

Options for 2,grep
-C outputs only the count of matching rows
-I is case insensitive (for single character)
-n Displays matching line numbers
-V does not display a line that does not contain matching text
-S does not display error messages
-e using extended regular expressions
For more options, see: Man grep

3, common grep instances

(1) Multiple file queries
grep "Sort" *.doc #见文件名的匹配

(2) Row matching: Output count of matching rows
Grep-c "Data.doc" #输出文档中含有48字符的行数

(3) Display of matching rows and rows
Grep-n "Data.doc" #显示所有匹配48的行和行号

(4) Show non-matching rows
Grep-vn "Data.doc" #输出所有不包含48的行

(4) Show non-matching rows
Grep-vn "Data.doc" #输出所有不包含48的行

(5) Case sensitive
Grep-i "AB" Data.doc #输出所有含有ab或Ab的字符串的行

4, the application of regular expressions

(1) Application of regular expressions (note: It is best to enclose regular expressions in single quotes)
grep ' [239]. ' Data.doc #输出所有含有以2, 3 or 9, and is a two-digit line

(2) Mismatch test
grep ' ^[^48] ' Data.doc #不匹配行首是48的行

(3) Using extended mode matching
Grep-e ' 219|216 ' Data.doc

(4) ...
This needs to be applied and summed up in practice, mastering the regular expression skillfully.

5, using the class name
You can use the class name that matches the international pattern:
[[: Upper:]] [A-z]
[[: Lower:]] [A-z]
[[:d Igit:]] [0-9]
[[: Alnum:]] [0-9a-za-z]
[[: Space:]] space or tab
[[: Alpha:]] [A-za-z]

(1) Use
grep ' 5[[:upper:]][[:upper:]] ' Data.doc #查询以5开头以两个大写字母结尾的行

The grep command in Linux-from cyber

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.