[Linux command] grep command

1. Function

The grep command in Linux is a powerful text search tool that uses regular expressions to search for text and print out the matching lines. The full name of grep is Global Regular Expression Print, which means the global regular expression version, and its use rights are all users.

2. Format

grep [options]

3. Main parameters

[options] main parameters:

-c: Print only the count of matching lines.
-I: Case insensitive (only applies to single characters).
-n: Display matching lines and line numbers.
-v: Display all lines that do not contain matching text.
-o: Only output the matched part of the file.
-s: Do not display error messages for nonexistent or no matching text.
-h: Do not display file names when querying multiple files.
-l: When querying multiple files, only output file names that contain matching characters.

The main parameters of the pattern regular expression:

\: Ignore the original meaning of special characters in regular expressions.
^: Matches the starting line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts on a line matching the regular expression.
\>: to end of line matching regular expression.
[ ]: A single character, such as [A] that A meets the requirements.
[ - ]: Range, such as [AZ], that is, A, B, C up to Z all meet the requirements.
. : All single characters.
* : There are characters, the length can be 0.

4. The grep command uses a simple example
$ grep 'test' d*
to display all lines containing test in files starting with d.
$ grep 'test' aa bb cc
shows lines matching test in aa,bb,cc files.
$ grep '[az]\{5\}' aa
displays all lines containing strings with at least 5 consecutive lowercase characters per string.
$ grep 'w\(es\)t.*\1' aa
If west is matched, then es is stored in memory, marked as 1, then searched for any number of characters (.*) followed by Follow another es(\1), and display the line when found. If you use egrep or grep -E, you don't need to escape the "\" symbol, and you can write it directly as 'w(es)t.*\1'.

5. The grep command uses complex examples
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 do I use the magic SysRQ key?
Where the file 'sysrp.txt' contains this string, discussing the functionality of SysRQ.
By default, 'grep' only searches the current directory. If this directory has many subdirectories, 'grep' will be listed as follows:
grep: sound: Is a directory
This may make the output of 'grep' difficult to read. There are two workarounds here:
explicitly ask to search subdirectories: grep -r
or ignore subdirectories: grep -d skip
If there is a lot of output, you can pipe it to 'less' to read:
$ grep magic / usr/src/Linux/Documentation/* | less
This way, you can read more easily.

One thing to note, you must provide a file filter (* to search all files). If you forget, 'grep' will wait until the program is interrupted. If this happens to you, press <CTRL c> and try again.

Here are some interesting command-line arguments:
grep -i pattern files : search case-insensitively. The default is case sensitive,
grep -l pattern files : only list matching filenames,
grep -L pattern files : list unmatched filenames,
grep -w pattern files : only match whole words, not strings part (eg match 'magic', not 'magical'),
grep -C number pattern files : show [number] lines respectively for matched context,
grep pattern1 | pattern2 files : show lines matching pattern1 or pattern2,
grep pattern1 files | grep pattern2 : Display lines that match both pattern1 and pattern2.

grep -n pattern files to display line number information

grep -c pattern files to find the total number of lines

There are also some special symbols for searching:
\< and \> mark the beginning and end of a word, respectively.
For example:
grep man * will match 'Batman', 'manic', 'man', etc,
grep '\<man' * will match 'manic' and 'man', but not 'Batman',
grep '\<man\>' Matches only 'man', not other strings such as 'Batman' or 'manic'.
'^': means the matched string is at the beginning of the line,
'$': means the matched string is at the end of the line,

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325304328&siteId=291194637