In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Linux grep command". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to use Linux grep command" together.
grep is a command-line tool originally used for Unix operating systems. Given a list of files or standard input, grep searches for text that matches one or more regular expressions and outputs only the lines or text that match (or do not match). Grep is an app originally written by Ken Thompson. grep was originally an application under ed, named g/re/p (globally search a regular expression and print).
Install grep
Linux has grep installed by default. MacOS has the BSD version of grep installed by default. The BSD version of grep is slightly different from the GNU version, so if you want to refer to this article completely, install the GNU version of grep using Homebrew or MacPorts.
Basic grep
All versions of grep have the same underlying syntax. The input parameter is the matching pattern and the file you need to search for. It will output each matched line to your terminal.
$ grep gnu gpl-3.0.txtalong with this program. If not, see ... The grep command is case sensitive by default, so "gnu,""GNU," and "Gnu" are three different values. You can ignore case using the--ignore-case option.$ grep --ignore-case gnu gpl-3.0.txt
You can also output all lines that don't match with the-invert-match option:
$ grep --invert-match \--ignore-case gnu gpl-3.0.txtVersion 3, 29 June 2007Copyright (C) 2007 Free Software Foundation, Inc. [... 648 lines...] Public License instead of this License. But first, please read Pipeline
It's useful to be able to search the text content of a file, but the real power of POSIX is the ability to connect multiple commands via "pipes." I find that the best way I can use grep is to combine it with other tools such as cut, tr, or curl. Suppose I have a file, and each line in the file is a technical paper that I want to download. I can open files manually by clicking on each link and then clicking on Firefox's option to save each file to my hard drive, but it takes multiple clicks and a long time. I can also search for links in a file and print out only the matching strings with the-only-matching option.
$ grep --only-matching http\:\/\/.* pdf example.htmlhttp://example.com/linux_whitepaper.pdfhttp://example.com/bsd_whitepaper.pdfhttp://example.com/important_security_topic.pdf
The output is a series of URLs, one per line. This fits perfectly with how Bash handles data, so instead of printing URLs to the terminal, I pipe them to curl:
$ grep --only-matching http\:\/\/.* pdf \example.html | curl --remote-name
This command downloads each file and saves it to my hard drive under its own remote filename. My search pattern in this example may be obscure. That's because it uses regular expressions, a "wildcard" language useful for fuzzy searches in large amounts of text.
regular expression
No one thinks regular expression ("regex" for short) is easy. However, I find that its reputation is often worse than it deserves. Admittedly, many people use regular expressions "too brazenly" until they become unreadable, large and comprehensive, and complex enough to wrap, but you don't have to overuse regular expressions. Here's a brief introduction to the way I use regular expressions. First, create a file called example.txt and enter the following:
AlbaniaAlgeriaCanada01311
The most basic elements are the unremarkable. Character. It represents a character.
$ grep Can.da example.txtCanada
The pattern Can.da successfully matches Canada because. A character represents any character. You can use the following symbols to make. Wildcards represent multiple characters:
? Match previous pattern zero or once * Match previous pattern zero or more times + Match previous pattern one or more times {4} Match previous pattern 4 times (or whatever number you write in parentheses)
With this knowledge in mind, you can practice in example.txt with all the patterns you think are interesting. Some may succeed, some may not. It is important that you analyze the results so you know why. For example, the following command does not match any country:
$ grep A.a example.txt
because. Characters can match only one character unless you increase the number of matches. Use the * character to tell grep to match a character zero or as many times as necessary until the end of the word. Because you know what you're dealing with, zero is unnecessary in this case. There must be no single-letter country in this list. So you can match a character with + at least once and any number of times until the end of the word:
$ grep A.+ a example.txtAlbaniaAlgeria
You can use square brackets to provide a series of letters:
$ grep [A,C].+ a example.txtAlbaniaAlgeriaCanada
It can also be used to match numbers. The results may shock you:
$ grep [1-9] example.txt1311
Were you surprised to see 11 appear in the results of a search for numbers 1 to 9? If you add 13 to your search list, what results do you get? These numbers are matched because they contain 1's, and 1's are among the numbers to be matched. You can find regular expressions confusing at times, but with experience and practice, you can master them and use them to improve your ability to search data.
Thank you for reading, the above is "Linux grep command how to use" the content, after the study of this article, I believe that everyone on Linux grep command how to use this problem has a deeper experience, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.