Elaborate on the linux grep command

  • 2020-05-13 04:18:58
  • OfStack

This article describes the linux grep command as follows:

1. The role

The grep command is a powerful text search tool that USES regular expressions to search text and print out matching lines. The full name of grep is Global Regular Expression Print, which stands for global regular expression version, and it has permission for all users.

2. The format

grep [options]

3. Main parameters

[options] main parameters:

-c: outputs only a count of matching rows.
-I: does not distinguish between large and small case (only applicable to single characters).
-h: query multiple files without showing the file name.
-l: when querying multiple files, only output file names containing matching characters.
-n: displays the matching line and line number.
-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:

\ : ignore the original meaning of special characters in regular expressions.
^ : matches the beginning line of the regular expression.
$: matches the end line of the regular expression.
\ < : start with a line that matches the regular expression.
\ > : to the end of the line that matches the regular expression.
[] : a single character such as [A] or A.
[-] : range, such as [A-Z], A, B, C1 up to Z.
. : all single characters.
* : there are characters, the length can be 0.

4. The grep command USES a simple instance


$ grep  ' test' d*

Displays all lines containing test in files beginning with d.


$ grep  ' test' aa bb cc

Lines matching test are displayed in aa, bb, cc files.


$ grep  ' [a-z]\{5\}' aa

Displays all lines containing strings with at least five consecutive lowercase characters per string.


$ grep  ' w\(es\)t.*\1 '  aa

If west is matched, es is stored in memory and marked as 1, then any character (.*) is searched, followed by another es(\1), and the line is displayed when found. If you use egrep or grep-E, escape the "\" sign and write 'w(es)t.*\1 'will do.

5. The grep command USES complex instances

Assume that you are '/ usr src/Linux/Doc directory search with a character string' magic 'file:


$ grep magic /usr/src/Linux/Doc/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?

This string is contained in the file 'sysrp.txt', which discusses the capabilities of SysRQ.

By default, 'grep' searches only the current directory. If there are many subdirectories under this directory, 'grep' is listed as follows:


grep: sound: Is a directory

This can make the output of 'grep' difficult to read. Here are two solutions:

Specify the search subdirectory: grep-r

Or ignore subdirectories: grep-d skip

If you have a lot of output, you can pipe it to 'less' to read:


$ grep magic /usr/src/Linux/Documentation/* | less

This makes it easier for you to read.

One thing to note is that you must provide a file filtering method (* for searching all files). If you forget, 'grep' will wait until the program is interrupted. If you encounter such a situation, press < CTRL c > "And then try again.

Here are some more interesting command-line arguments:

grep-i pattern files: case insensitive search. Case sensitive by default, grep-l pattern files: lists only the matched file names, grep-L pattern files: lists mismatched file names, grep-w pattern files: matches only the whole word, not the first part of the string (for example, matches 'magic' instead of 'magical'), grep-C number pattern files: the matching context displays the [number] line, grep pattern1 | pattern2 files: displays lines matching pattern1 or pattern2, grep pattern1 files | grep pattern2: displays lines that match both pattern1 and pattern2. grep-n pattern files displays line number information grep-c pattern files can find the total number of rows

Here are some special symbols for searching:

\ < And \ > Mark the beginning and end of a word.

Such as:

grep man * will match 'Batman', 'manic', 'man', etc.,
grep '\ < man' * matches 'manic' and 'man', but not 'Batman',
grep '\ < man\ > 'only matches 'man', not other strings like 'Batman' or 'manic'.
'^' : matches the string at the beginning of the line,
'$' : refers to the end of the line of the string that matches,

Grep command usage guide

1. Parameters:

-I: ignore case
-c: print the number of matched lines
-l: look for contained matches from multiple files
-v: find a row that does not contain a match
-n: prints the line and line label containing the match

2. RE (regular expression)

Ignore the original meaning of special characters in regular expressions
^ matches the beginning line of the regular expression
$matches the end line of the regular expression
\ < Start with the line that matches the regular expression
\ > To the end of the line that matches the regular expression
[] single character; If [A], A meets the requirements
[-] scope; For example [A-Z], A, B, C1 up to Z all meet the requirements
.all single characters
* all characters, length can be 0

3, for example,


# ps -ef | grep in.telnetd 
root 19955 181 0 13:43:53 ? 0:00 in.telnetd 

# more size.txt size Content of file  
b124230 
b034325 
a081016 
m7187998 
m7282064 
a022021 
a061048 
m9324822 
b103303 
a013386 
b044525 
m8987131 
B081016 
M45678 
B103303 
BADc2345 

# more size.txt | grep '[a-b]'  The scope of   ; Such as [A-Z] namely A . B . C1 until Z All fit the bill  
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; Such as [A]  namely A Meet the requirements  
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 opening line of the regular expression  
root::0:root 

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

# grep '\<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..*3' -i  : ignore case  

b124230 
b103303 
B103303 

# more size.txt | grep -iv 'b1..*3' -v  : finds a row that does not contain a match  

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

# more size.txt | grep -in 'b1..*3' 
1:b124230 
9:b103303 
15:B103303 

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

15 
# grep '\$' /etc/init.d/nfs.server 
case "$1" 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/sharetab.$$ /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: $0 { start | stop }" 

# more size.txt 

the test file 
their are files 
The end 

# grep 'the' size.txt 
the test file 
their are files 

# grep '\<the' size.txt 
the test file 
their are 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 multi-purpose text search tool using regular expressions. C1 FC name C3 = % % % % EE "onclick =" tagshow (event) "class =" t_tag" > The command was originally an php in the ed line editor. C1 FC name C3 = % % % % EE "onclick =" tagshow (event) "class =" t_tag" > Command/filter:


g/re/p -- global - regular expression - print.

The basic format

grep pattern [file...]

(1)grep search string [filename]

(2)grep regular expression [filename]

Search the file for all the locations where pattern occurs. pattern can be either the string to search for or a regular expression.

Note: it is best to use double quotation marks/when entering the string to be searched and single quotation marks when using regular expressions for pattern matching

2, grep options

-c outputs only a count of matched rows
-i case insensitive (for single character)
-n displays the matching line number
-v does not display lines that do not contain matching text
-s does not display error messages
-E USES extended regular expressions

For more options, see: man grep

3. Common grep instances

(1) multiple file queries


  grep "sort" *.doc    # See file name matching 

(2) line matching: output matching line count


$ grep  ' test' aa bb cc
0

(3) display matching rows and the number of rows


$ grep  ' test' aa bb cc
1

(4) display the mismatched rows


$ grep  ' test' aa bb cc
2

(5) case sensitivity


grep -i "ab" data.doc    # Output all containing ab or Ab The line of the string 

4. Application of regular expressions

(1) application of regular expressions (note: it is best to enclose regular expressions in single quotes)


$ grep  ' test' aa bb cc
4

(2) mismatch test


$ grep  ' test' aa bb cc
5

(3) use extended pattern matching


$ grep  ' test' aa bb cc
6

(4)... This requires constant application and summary in practice to master regular expressions.

5. Use the class name

You can use the international pattern matching class name:

[[:upper:]] [A-Z]
[[:lower:]] [a-z]
[[:digit:]] [0-9]
[[:alnum:]] [0-9a-zA-Z]
[[:space:]] space or tab
[[:alpha:]] [a-zA-Z]

(1) is used


$ grep  ' test' aa bb cc
7

Related articles: