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 grep to find the contents of files under Linux

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

Share

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

This article will explain in detail how to use grep to find file content under Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

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

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, 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.

This is the end of this article on "how to use grep to find file content under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out 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

Development

Wechat

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

12
Report