linux pipeline command grep
Function description: Find strings that meet the conditions in the file.
Syntax: grep [-abcEFGhHilLnqrsvVwxy][-A
Supplementary Note: The grep command is used to find files containing the specified template style. file, if the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name given is "-", the grep command will read data from the standard input device.
Parameters:
-a or --text Don’t ignore binary data.
-A
-b or --byte-offset Before displaying the column that matches the template style, mark the bit number of the first character of the column.
-B
-c or --count Count the number of columns that match the template style.
-C
-d
-e or --regexp= Specifies a string as the template style for searching file content.
-E or --extended-regexp Use the template style as extended ordinary notation.
-f or --file= specifies a template file whose content contains one or more template styles, allowing grep to find file contents that meet the template conditions. The format is one template style per column. .
-F or --fixed-regexp Treat template styles as a list of fixed strings.
-G or --basic-regexp Treat the template style as a normal representation.
-h or --no-filename does not indicate the file name to which the column belongs before displaying the column that matches the template style.
-H or --with-filename Before displaying the column that conforms to the template style, indicates the file name to which the column belongs.
-i or --ignore-case Ignore the difference between upper and lower case characters.
-l or --file-with-matches Lists the file names whose content matches the specified template style.
-L or --files-without-match List the names of files whose content does not match the specified template style.
-n or --line-number Before displaying the column that matches the template style, mark the column number of the column.
-q or --quiet or --silent does not display any information.
-r or --recursive This parameter has the same effect as specifying the "-d recurse" parameter.
-s or --no-messages Do not display error messages.
-v or --revert-match Reverse the search.
-V or --version Display version information.
-w or --word-regexp Only displays columns that match the full word.
-x or --line-regexp Only display columns that match all columns.
-y This parameter has the same effect as specifying the "-i" parameter.
--help Online help.
Usage of linux grep command
Use grep command to search text files from www.linuxso.com
If you want to find a string in several text files, you can use the ‘grep’ command. ‘grep’ searches text for a specified string.
Suppose you are searching for a file with the string 'magic' in the '/usr/src/linux/Documentation' directory:
$ grep magic /usr/src/linux/Documentation/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?
The file ‘sysrp.txt’ contains this string and discusses the function of SysRQ.
By default, ‘grep’ only searches the current directory. If there are many subdirectories under this directory, 'grep' will list it like this:
grep: sound: Is a directory
This may make the output of 'grep' difficult to read. There are two solutions here:
Explicitly ask to search the subdirectory: grep -r
or ignore the subdirectory: grep -d skip
Of course, if a lot of output is expected, you can pipe it to 'less' Read
$ grep magic /usr/src/linux/Documentation/* | less
This way, you can read 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
Here are some interesting command line parameters:
grep -i pattern files: Search case-insensitively. The default is case-sensitive,
grep -l pattern files: only match file names are listed,
grep -L pattern files: list unmatched file names,
grep -w pattern files: only match whole words, not Part of the string (such as matching 'magic', not 'magical'),
grep -C number pattern files: matching context displays [number] lines respectively,
grep pattern1 | pattern2 files: displays lines matching pattern1 or pattern2 ,
grep pattern1 files | grep pattern2: Display lines that match both pattern1 and pattern2.
There are also some special symbols for searching:
< and > mark the beginning and end of words respectively.
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,
If you are not used to command line parameters, you can try 'grep' in the graphical interface, such as reXgrep. This software provides syntax such as AND, OR, NOT, and beautiful buttons :-). If you just need clearer output, try fungrep .
.grep Search string
Command format:
grep string filename
There are many ways to find strings. For example, I want to find all lines starting with M. At this time, the concept of pattern must be introduced. Here are some simple ones □Example, and explanation:
^M Lines starting with M, ^ means the beginning
M$ Lines ending with M, $ means the end
^[0-9] Lines starting with a number, [] Can enumerate letters
^[124ab] Lines starting with 1, 2, 4, a, or b
^b.503 Period represents any letter
* Asterisk represents more than 0 letters (can be none)
+ Plus sign Represents more than 1 letter
. Slashes can remove special meanings
cat passwd | grep ^s Lists the list of exchange students who have applied for accounts
cat passwd | grep '^b.503' Lists all grades in the Department of Electrical Engineering...
grep '^.' myfile.txt Lists all lines starting with a period
Match a non-newline character. For example: 'gr.p' matches gr followed by any character, then p.
*
Match zero or more previous characters. For example: '*grep' matches all lines with one or more spaces followed by grep. .* used together represents any character.
[]
matches characters within a specified range, such as '[Gg]rep' matches Grep and grep.
[^]
matches a character that is not within the specified range, such as: '[^A-FH-Z]rep' matches a line starting with a letter that does not contain A-R and T-Z, followed by rep.
(..)
marks matching characters, such as '(love)', love is marked as 1.
<
Anchor the beginning of a word, like: '
>
Anchor the end of a word, like 'grep>' Matches lines that contain words ending with grep.
x{m}
Repeat the character x, m times, such as: '0{5}' matches lines containing 5 o's.
x{m,}
Repeat the character x, at least m times, such as: 'o{5,}' matches lines with at least 5 o's.
x{m,n}
Repeat the character x, at least m times and no more than n times. For example: 'o{5,10}' matches lines with 5--10 o's.
w
matches literal and numeric characters, that is, [A-Za-z0-9], for example: 'Gw*p' matches G followed by zero or more literal or numeric characters, then p. The inverted form of
W
w matches one or more non-word characters, such as period, period, etc.
b
Word lock character, such as: 'bgrepb' only matches grep.
3. Metacharacter extension set for egrep and grep -E
+
matches one or more previous characters. For example: '[a-z]+able', matches a string of one or more lowercase letters followed by able, such as loveable, enable, disable, etc.
?
Matches zero or more previous characters. For example: 'gr?p' matches lines with gr followed by one or no characters, then p.
a|b|c
matches a or b or c. For example: grep|sed matches grep or sed
()
grouping symbols, such as: love(able|rs)ov+ matches loveable or lovers, matches one or more ov.
x{m},x{m,},x{m,n}
functions the same as x{m},x{m,},x{m,n}
4. POSIX character class
for To maintain the same character encoding in different countries, POSIX (The Portable Operating System Interface) adds special character classes, such as [:alnum:] which is another way of writing A-Za-z0-9. They must be placed within [] signs to become regular expressions, such as [A- Za-z0-9] or [[:alnum:]]. Except for fgrep, grep under Linux supports POSIX character classes.
[:alnum:]
Alphanumeric characters
[:alpha:]
Literal characters
[:digit:]
Numeric characters
[:graph:]
Non-empty characters (non-space, Control characters)
[:lower:]
lowercase characters
[:cntrl:]
Control characters
[:print:]
Non-empty characters (including spaces)
[:punct:]
Punctuation
[:space:]
All whitespace characters (new lines, spaces, tabs)
[:upper:]
Uppercase characters
[:xdigit:]
Hex digits ( 0-9, a-f, A-F)
5. Grep command option
-?
Show matching lines above and below simultaneously? lines, such as: grep -2 pattern filename displays the upper and lower lines of the matching line at the same time.
-b, --byte-offset
Print the block number where the line is located before printing the matching line.
-c,--count
Only prints the number of matching lines and does not display the matching content.
-f File, --file=File
Extract template from file. An empty file contains 0 templates, so nothing matches.
-h, --no-filename
When searching for multiple files, do not show matching filename prefixes.
-i, --ignore-case
Ignore case differences.
-q, --quiet
Cancel the display and only return the exit status. 0 means a matching row was found.
-l, --files-with-matches
Print a list of files matching the template.
-L, --files-without-match
Print a list of files that do not match the template.
-n, --line-number
Print the line number in front of the matching line.
-s, --silent
Do not display error messages about non-existent or unreadable files.
-v, --revert-match
Reverse retrieval, only display unmatched lines.
-w, --word-regexp
If quoted by < and >, search the expression as a word.
-V, --version
Display software version information.
6. Examples
To use grep well, you actually need to write regular expressions well, so we will not explain all the functions of grep with examples here. We will only list a few examples to explain how to write a regular expression.
$ ls -l | grep '^a'
Filter the output of ls -l through the pipeline and only display lines starting with a.
$ grep 'test' d*
Display all lines containing test in files starting with d.
$ grep 'test' aa bb cc
Displays the lines matching test in the aa, bb, cc files.
$ grep '[a-z]{5}' aa
Displays all lines containing strings each with at least 5 consecutive lowercase characters.
$ grep 'w(es)t.*1' aa
If west is matched, es is stored in the memory and marked as 1, and then searches for any number of characters (.*). These characters are followed by another es (1). If found, the line is displayed. If you use egrep or grep -E, you don't need to escape with the "" sign, just write it directly as 'w(es)t.*1'.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use grep command for log analysis in Linux? Introduction: Logs are important records generated during system operation. For system operation, maintenance and troubleshooting, log analysis is an essential task. In the Linux operating system, the grep command is a powerful text search tool that is very suitable for log analysis. This article will introduce how to use the grep command commonly used for log analysis and provide specific code examples. 1. Introduction to grep command grep is a file in Linux system

This article mainly studies the related content of grep displaying several lines of information before and after under Linux, as follows. Grep under standard unix/linux controls the context through the following parameters. grep-C5foofile displays the line matching the foo string in the file file and the upper and lower 5 lines. grep-B5foofile displays foo and the first 5 lines. grep-A5foofile displays foo and the last 5 lines to view the grep version. The method of numbering is grep-V. If you want to upgrade, the upgrade method: the latest source code (google or Baidu search homepage), compile and install it somewhere, such as /home/aaa/bin/, then use / when using it in the future. home/aaa/

If you want to view the contents of a file in a LINUX system, how can you view the first and last contents of a specified file? Let's take a look at the tutorial on using grep to query the contents of the specified first and last files. 1. Open LINUX, here we can use the UBUNTU operating system. 2. Find TERMINAL on the left taskbar and open the terminal window. 3. Add ^ after grep to specify the beginning of a certain line. 4. Add $ after the content after grep to specify the end of a certain line. 5. ^$ can be used at the same time to specify the necessary content at the beginning and end of a certain line. 6. If a character in the middle is missing or incorrect, it will not be queried. 7. Use with -i to ignore the case rules. 8. You can also use -v

The Linuxgrep command is used to find strings that meet conditions in files. The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name is -, the grep command reads data from the standard input device. Syntax: grep parameters: -a or --text: Do not ignore binary data. -A or --after-context=: In addition to displaying the column that conforms to the template style, and display the content after the line. -b or --byte-offset: before displaying the line that matches the style

In Linux, grep is a very common and important tool. It is a command that every professional operation and maintenance engineer must master, because it can quickly find and filter the contents of files. So how to use grep in Linux system? The following is Let’s take a look at the common usage introduction. 1. Basic usage The grep command is mainly used to search for lines of a specified pattern in files. For example, to find the line containing "example" in the file file.txt, you can use the grep command. grep 'example' file.txt grep will output all lines containing 'example'.

The Linuxgrep command is used to find strings that meet conditions in files. The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name is -, the grep command reads data from the standard input device. Syntax: grep parameters: -a or --text: Do not ignore binary data. -A or --after-context=: In addition to displaying the column that conforms to the template style, and display the content after the line. -b or --byte-offset: before displaying the line that matches the style

The regular usage of grep includes: 1. Simple matching; 2. Basic regular expressions; 3. Use of metacharacters; 4. Use of anchor characters; 5. Use of character classes; 6. Use of quantifiers. Detailed introduction: 1. Simple matching, use the grep command followed by the string to be matched; 2. Basic regular expressions, use the -E option to enable extended regular expression functions; 3. The use of metacharacters, in regular expressions , you can use some metacharacters to represent specific characters or character sets; 4. The use of anchor characters, etc.

The usage of grep in Linux is to search for lines of text that match a specific pattern and output the matching lines. The grep command has a variety of options and usages, allowing flexible search and matching operations according to different needs. Commonly used options are: 1. -i, ignore the case of characters when searching; 2. -n, display matching line numbers; 3. -c, count the number of matching lines; 4. -r, recursively search in the specified directory Search files under; 5. -l, only display file names containing matching items; 6. -v, reverse matching, output lines that do not contain matching items, etc.
