Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Linux grep command

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the Linux grep command, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Linux grep command article. Let's take a look at it.

The Linux common command grep is a powerful text search tool that uses regular expressions to search for text and print matching lines. Specific characters used to filter / search. Regular expressions can be used with a variety of commands and are very flexible in use.

Grep's powerful text search tool

Option-a-- text # do not ignore binary data. -A-- after-context= # displays the content after that line in addition to the line that matches the template style. -b-- byte-offset # displays the line that matches the template style and displays the contents before the line. -B-- before-context= # displays not only the line that matches the style, but also the content before that line. -c-- count # calculates the number of columns that match the template style. -C-- context= or-# displays the contents of the column before and after the column, in addition to the column that matches the template style. -d-- directories= # this parameter must be used when you specify that you are looking for a directory instead of a file, otherwise the grep command will return information and stop the action. -e-- regexp= # specifies the string as the template style for finding the contents of the file. -E-extended-regexp # uses the template style as an extended normal representation, which means that you can use extended regular expressions. -f-- file= # specifies a template file whose content has one or more template styles and lets grep find the content of the file that meets the criteria of the template in the format of each column of the template style. -F-- fixed-regexp # treats the template style as a list of fixed strings. -G-- basic-regexp # uses the template style as a common representation. -h-- no-filename # does not indicate the name of the file to which the column belongs until the column that matches the template style is displayed. -H-- with-filename # marks the file name of the column before displaying the column that matches the template style. -I-- ignore-case # ignores the difference in character case. -l-- file-with-matches # lists the file names whose contents match the specified template style. -L-- files-without-match # lists file names whose contents do not match the specified template style. -n-- line-number # marks the column number before displaying the column that matches the template style. -P-- perl-regexp # PATTERN is an Perl regular expression-Q-- quiet or-- silent # that does not display any information. The effect of this parameter is the same as that of specifying the "- d recurse" parameter. -s-- no-messages # does not display an error message. -v-- revert-match # reverse lookup. -V-- version # displays version information. -w-- word-regexp # displays only columns that match the whole word. -x-- line-regexp # displays only the columns that match the entire column. -y # this parameter has the same effect as "- I". -o # outputs only those parts of the file that match. -m-- max-count= # stops searching after finding the num row result, which is used to limit the number of matching rows the rule expression ^ # anchors the beginning of a line such as:'^ grep' matches all lines that begin with grep. The end of the $# anchor line is: 'grep$' matches all lines ending in grep. . # matches a character that is not a newline character such as: 'gr.p' matches gr followed by any character, followed by p. * # matches zero or more previous characters such as:'* grep' matches all one or more spaces followed by the line of grep. . * # together to represent any character. [] # matches a specified range of characters, 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 the beginning of a letter that does not contain Amurr and Tmurz, followed by a line of rep. The\ (..\) # tag matches characters, such as'\ (love\)', and love is marked as 1. \ # Anchor the end of the word, such as' grep\ > 'matches the line that contains the word ending in grep. X\ {m\} # repeats the character xMagol m times, for example:'0\ {5\} 'matches a line containing 5 os. X\ {m,\} # repeat character x, at least m times, for example:'o\ {5,\} 'matches lines with at least 5 o. X\ {mdirection n\} # repeat character x, at least m times, no more than n times, for example:'o\ {5jue 10\} 'matches the lines of 5 Murray 10 o. \ w # matches literal and numeric characters, that is, [A-Za-z0-9], for example:'G\ wimpp 'matches G followed by zero or more literal or numeric characters, followed by p. The inverted form of\ W #\ w that matches one or more non-word characters, such as periods, periods, etc. \ b # word lock, such as:'\ bgrep\ b 'only matches grep. Common usage of the grep command searches for a word in a file, and the command returns a line of text that contains "match_pattern":

The grep match_pattern file_name grep "match_pattern" file_name looks in multiple files:

Grep "match_pattern" file_1 file_2 file_3... Output all lines except the-v option:

Grep-v "match_pattern" file_name tag matching color-color=auto option:

Grep "match_pattern" file_name-- color=auto uses the regular expression-E option:

Grep-E "[1-9] +" or egrep "[1-9] +" uses the regular expression-P option:

Grep-P "(\ d {3}\ -) {2}\ d {4}" file_name outputs only the-o option that matches in the file:

Echo this is a test line. | | grep-o-E "[amurz] +\." line.echo this is a test line. | | egrep-o "[amurz] +\." line. Count the number of lines in the file or text that contain matching strings-c option:

The grep-c "text" file_name output contains the number of lines matching the string-n option:

Grep "text"-n file_name# or cat file_name | grep "text"-n # multiple files grep "text"-n file_1 file_2 print style matches the character or byte offset:

Echo gun is not unix | grep-b-o "not" characters in a 7:not# line are calculated cheaply from the first character of the line, with a starting value of 0. The option * *-b-oval * is always used in conjunction with others. Search for multiple files and find which files match the text:

Grep-l "text" file1 file2 file3...grep Recursive search File Recursive search for text in a multi-level directory:

Grep "text". -r-n #. Represents the current directory. Ignore the character case in the matching style:

Echo "hello world" | grep-I "HELLO" # hello option-e brake multiple matching styles:

Echo this is a text line | grep-e "is"-e "line"-oisline# can also use the * *-flip * option to match multiple styles and write out the characters you want to match line by line in the style file. Cat patfileaaabbbecho aaabbb ccc ddd eee | grep-f patfile-o includes or excludes specified files in grep search results:

# only recursively searches for the character "main ()" grep "main ()" in all .php and .html files in the directory. -r-- include *. {php,html} # excludes all README files grep "main ()" from the search results. -r-- exclude "README" # excludes grep "main ()" from the list of filelist files in search results. -r-- exclude-from filelist uses grep and xargs with the 0 value byte suffix:

# Test file: echo "aaa" > file1echo "bbb" > file2echo "aaa" > file3grep "aaa" file*-lZ | xargs-0 rm# deletes file1 and file3,grep output after execution. Use the-Z option to specify the 0-value byte as the Terminator file name (\ 0), xargs-0 reads the input and separates the file name with the 0-value byte Terminator, and then deletes the matching file.-Z is usually used in conjunction with-l. Grep silent output:

The grep-Q "test" filename# does not output any information, and returns a non-zero value if the command runs successfully or fails. It is generally used for conditional testing. The line before or after the matching text is printed:

# display the three lines after matching a result, use the-An option: seq 10 | grep "5"-A 3567 to display the three lines before matching a result, use the-B option: seq 10 | grep "5"-B 3234lines to display the first three lines and the last three lines that match a result, use the-C option: seq 10 | grep "5"-C 3234567 if there are multiple matching results "- -" will be used as the separator between the matching results: echo-e "a\ nb\ nc\ na\ nb\ nc" | grep a-A 1ab--ab on "how to use the Linux grep command" this article is introduced here, thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Linux grep commands". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report