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 grep command in linux looks up the contents of the file

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Today, what the editor shares with you is the grep command in linux to find the contents of the file. I believe many people don't know much about it. In order to make you understand better, I summed up the following contents for you. Let's look down together. I'm sure you'll get something.

Grep command to find the contents of the file

Grep is a command commonly used to find the contents of filtered text files under the Linux command line. The simplest usage is:

Grep apple fruitlist.txt

Find out what contains apple in the fruitlist.txt file.

If you want to ignore case, you can use the-I parameter:

Grep-I apple fruitlist.txt

If you want to search for all files in the directory, including subdirectories, and display the line number in the results, you can use the following command:

Grep-nr apple *

The syntax of grep supports regular expressions, which are a bit complex, which will be explained later. Here are some useful parameters:

-A num,-- after-context=num: output the num line after the matching line in the result at the same time

-B num,-- before-context=num: output the num line before the matching line in the result at the same time, and sometimes we need to display several lines of context.

-I,-- ignore-case: ignore case

-n,-- line-number: displays the line number

-R,-r,-- recursive: recursively search subdirectories

-v,-- invert-match: the output has no matching lines

We can make grep more powerful through pipeline operation, which takes the output of the previous command as the input of the latter command, thus combining many simple commands to complete complex functions. For example, if we want to find a line that contains apple, but want to filter out pineapple, we can use the following command:

Grep apple fruitlist.txt | grep-v pineapple

If we want to save the search results, we can redirect the standard output of the command to a file:

Grep apple fruitlist.txt | grep-v pineapple > apples.txt

The difference between the redirect symbol > and the pipe operation symbol | is that the redirection is followed by a file, which cannot be followed by any files or commands, while the pipe operation is followed by commands, which can be continued indefinitely. If you want to append to a file, you can use > >. Pipeline operation is a philosophy of Linux command line, and it is one of the rare computer technologies that can be used for decades. Through the pipeline operation, one-line command can complete the text processing function that thousands of lines of programs under Windows cannot do.

On the linux grep command search file content to share here, I hope that the above content can have a certain reference value, you can learn to apply. If you like this article, you might as well share it for more people to see.

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