Home>Article>Operation and Maintenance> A complete list of grep commands in Linux
The grep command in Linux system is a powerful text search tool. It can use regular expressions to search text and print out the matching lines. The full name of grep is Global Regular Expression Print, which means the global regular expression version. Its usage permissions are for all users.
1. Function
The grep command in Linux system is a powerful text search tool. It can use 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. Its usage permissions are for all users.
The grep family includes grep, egrep and fgrep. The egrep and fgrep commands are only slightly different from grep. egrep is an extension of grep, supporting more re metacharacters. fgrep is fixed grep or fast grep, which treats all letters as words. That is to say, the metacharacters in regular expressions represent their own literal meanings. , no longer special. Linux uses the GNU version of grep. It is more powerful and can use the functions of egrep and fgrep through the -G, -E, and -F command line options.
2. Format and main parameters
grep [options]
Main parameters: grep --help can be viewed
-c: Only output the count of matching lines.
-i: Case-insensitive.
-h: Do not display file names when querying multiple files.
-l: When querying multiple files, only the file names containing matching characters are output.
-n: Display matching lines and line numbers.
-s: Do not display error messages that do not exist or have no matching text.
-v: Display all lines that do not contain matching text.
--color=auto: You can add color to the found keywords.
Main parameters of pattern regular expression:
\: Ignore the original meaning of special characters in the regular expression.
^: Matches the starting line of the regular expression.
$: Matches the end line of the regular expression.
\<: Start from the line matching the regular expression.
\>: To the end of the line matching the regular expression.
[ ]: A single character, such as [A] means A meets the requirements.
[-]: Range, such as [A-Z], that is, A, B, C to Z all meet the requirements.
.: All single characters.
*: All characters, length can be 0.
3. Simple example of using grep command
itcast$ grep 'test' d*
Display all lines containing test in files starting with d
itcast $ grep 'test' aa bb cc
Displays the lines matching test in the aa, bb, and cc files.
itcast $ grep ‘[a-z]\{5\}’ aa
Displays all lines containing strings with at least 5 consecutive lowercase characters in each string.
itcast $ grep 'w\(es\)t.*\1′ aa
If west is matched, es is stored in memory and marked as 1, and then searches for any number of characters (.* ), these characters are followed by another es(\1), and the line is displayed when found. If you use egrep or grep -E, there is no need to escape with the "\" sign, just write it directly as 'w(es)t.*\1'.
4. Grep command uses complex examples
Explicitly request to search subdirectories:
grep -r
Or ignore subdirectories
grep -d skip
If there is a lot of output, you can Pipe it to 'less' for reading:
itcast$ grep magic /usr/src/Linux/Documentation/* | less
This way, you can read it more conveniently.
One thing to note is that you must provide a file filtering method (use * to search all files). If you forget, 'grep' will wait until the program is interrupted. If you encounter this, press and try again.
There are some interesting command line parameters below:
grep -i pattern files: Search case-insensitively. By default, it is case-sensitive,
grep -l pattern files: Only matching file names are listed,
grep -L pattern files: Unmatched file names are listed,
grep -w pattern files: Only Match the entire word, not part of the string (such as matching 'magic', not 'magical'),
grep -C number pattern files: The matching context displays [number] lines respectively,
grep pattern1 | pattern2 files: Displays lines matching pattern1 or pattern2,
For example: grep "abc\|xyz" testfile means filtering lines containing abc or xyz
grep pattern1 files | grep pattern2: Displays lines matching both pattern1 and pattern2 OK.
grep -n pattern files can display line number information
grep -c pattern files can find the total number of lines
There are also some special symbols used for searching: \< and \> mark words respectively Beginning and end.
For example:
grep man * will match 'Batman', 'manic', 'man', etc.
grep '\
'^': means that the matched string is at the beginning of the line,
'$': means that the matched string is at the end of the line,
Use grep to find the structure grep -R "struct task_struct { " /usr/src Add -n to display the line number
PS1=$ Enter the home directory in .bashrc
[Course Recommendation:Linux Video Tutorial]
The above is the detailed content of A complete list of grep commands in Linux. For more information, please follow other related articles on the PHP Chinese website!