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

The usage of linux grep search command

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "the usage of linux grep search command". In daily operation, I believe many people have doubts about the usage of linux grep search command. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the usage of linux grep search command"! Next, please follow the editor to study!

Search for text files with 'grep'

If you want to find a string in several text files, you can use the 'grep' command. 'grep' searches the text for the specified string.

Suppose you are searching for files 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 the string and discusses the functionality 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 may make the output of 'grep' difficult to read. There are two solutions:

Explicitly request search subdirectory: grep-r

Or omit the subdirectory: grep-d skip

Of course, if you expect a lot of output, you can pipe it to 'less' to read:

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

In this way, you can read it more easily.

It is important to note 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 such a situation, press and try again.

Here are some interesting command line parameters:

Grep-i pattern files: search insensitive to case. The default is case sensitive

Grep-l pattern files: lists only matching file names

Grep-L pattern files: lists mismatched file names

Grep-w pattern files: matches only the whole word, not part of the string (such as matching 'magic', instead of' magical')

Grep-C number pattern files: the matching context displays [number] lines respectively.

Grep pattern1 | pattern2 files: displays lines that match pattern1 or pattern2

Grep pattern1 files | grep pattern2: displays lines that match both pattern1 and pattern2.

There are also some special symbols for search:

\

< 和 \>

Mark the beginning and end of the word respectively.

For example:

Grep man * will match 'Batman',' manic', 'man', etc.

Grep'\ 'size.txt

The test file

# grep'\ 'size.txt

The test file

# grep'\ 'size.txt

The test file

1, introduction

A multi-purpose text search tool that uses regular expressions. The php?name=%C3%FC%C1%EE "onclick=tagshow (event)" class= "t_tag" > command is originally a php?name=%C3%FC%C1%EE "onclick=tagshow (event)" class= "t_tag" > command / filter in the ed line editor:

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

Basic format

Grep pattern [file...]

(1) grep search string [filename]

(2) grep regular expression [filename]

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

Note: it is best to use double quotation marks when entering strings to search and single quotation marks when using regular expressions in pattern matching

2 options for GREP

-c outputs only the count of matching rows

-I is case-insensitive (for single characters)

-n displays a matching line number

-v does not display lines that do not contain matching text

-s does not display error message

-E uses extended regular expressions

For more options, please see: man grep

3. Commonly used grep instances

(1) multiple file queries

Grep "sort" * .doc # see matching of file names

(2) Row matching: count of output matching rows

Grep-c "48" data.doc # output document contains 48-character lines

(3) display matching rows and number of rows

Grep-n "48" data.doc # displays all lines and line numbers that match 48

(4) display mismatched lines

Grep-vn "48" data.doc # outputs all lines that do not contain 48

(4) display mismatched lines

Grep-vn "48" data.doc # outputs all lines that do not contain 48

(5) case sensitivity

Grep-I "ab" data.doc # outputs all lines containing strings of ab or Ab

4, the application of regular expression

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

Grep '239.' Data.doc # outputs all lines that start with 2BI 3 or 9 and are two digits

(2) mismatch test

Grep'^ [^ 48] 'data.doc # does not match lines that start with 48

(3) using extended pattern matching

Grep-E '219 | 216' data.doc

(4).

This requires continuous application and summary in practice and proficiency in regular expressions.

5, use the class name

You can use international pattern matching class names:

[[: upper:]] [Amurz]

[[: lower:]] [amurz]

[[: digit:]] [0-9]

[[: alnum:]] [0-9a-zA-Z]

[[: space:]] spaces or tab

[[: alpha:]] [a-zA-Z]

(1) use

Grep'5 [[: upper:]] [[: upper:]] 'data.doc # query lines beginning with 5 and ending with two uppercase letters

At this point, the study on the use of the linux grep search command is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report