In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the Linux system grep command how to find keywords in the file content, the editor thinks it is very practical, so I share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
The find command can find the file accurately but not the keywords in the file. What if you want to find the contents of the file?
Check the number of lines of the file in the linux system is not as convenient as the click of the mouse in the windows system to manipulate the file, all kinds of operations on the file must be completed with a variety of commands. For example, sometimes we need to know how many lines the file has without looking at the contents of the file. At this point, we can complete our idea through the wc command of linux. The purpose and usage of the wc command are described in detail below.
The purpose of the wc command is to count the number of bytes, words, and lines in the specified file, and display the results to output.
Command syntax: wc [options] file
The command option parameters are as follows:
-c Statistics Bytes
-l count the number of rows
-w count words
-m statistics of characters
The above options can be used alone or in combination. Note that when used in combination, the order and number of columns that output results are not affected by the order and number of options. The output is always displayed in the following order.
Line count, word count, byte count, file name
For example:
Wc-lcw file1 file2
The output is as follows:
9 36 file1
8 62 file2
8 24 96 total
Omit any option-the execution result of the lcw,wc command is the same as above.
The above options can also be used separately, for example:
Number of rows of wc-l filename output filename
Number of bytes of wc-c filename output filename
Number of characters of wc-m filename output filename
Wc-w filename outputs the number of words in filename
How many characters is the longest line in the wc-L filename output filename file?
Grep file content search command 1. Action
Grep command is a powerful text search tool in Linux system. It can use regular expressions to search for text and print out matching lines. The full name of grep is Global Regular Expression Print, which represents the global regular expression version, and its permission is for all users.
two。 Format
Grep [options]
3. Main parameters
[options] main parameters:
-c: outputs only the count of matching rows.
-I: case-insensitive (for single characters only).
-h: the file name is not displayed when querying multiple files.
-l: when querying multiple files, only the file names that contain matching characters are output.
-n: displays matching lines and line numbers.
-s: does not display error messages that do not exist or have no matching text.
-v: displays all lines that do not contain matching text.
The main parameters of the pattern regular expression:
\: ignore the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: matches the end line of the regular expression.
To the end of the line that matches the regular expression
[]: single character, such as [A], that is, A meets the requirements.
[-]: range, such as [Amurz], that is, A, B, C all the way up to Z meet the requirements.
.: all single characters.
There are characters and the length can be 0.
The 4.grep command uses a simple example
$grep 'test' d*
Displays lines that contain test in all files that begin with d.
$grep 'test' aa bb cc
Displays the lines that match the test in the aa,bb,cc file.
$grep'[a murz] {5}'aa
Displays all lines that contain at least five consecutive lowercase characters for each string.
$grep'w (es) t.*\ 1' aa
If the west is matched, the es is stored in memory and marked as 1, and then any character (.) is searched, followed by another es (\ 1), and the line is displayed when found. If you use egrep or grep-E, instead of using the "\" sign for escape, you can just write'w (es) t.\ 1'.
The 5.grep command uses complex instances
Suppose you are searching for files with string 'magic' in the' / usr/src/Linux/Doc' directory:
$grep magic / usr/src/Linux/Doc/*
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
If you have 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 arguments:
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.
Line number information can be displayed by grep-n pattern files
You can find the total number of rows by grep-c pattern files
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
Grep matches only man, not other strings such as Batman or manic.
^: refers to the matching string at the beginning of the line
$: refers to the matching string at the end of the line
Practice searching for the contents of a file index that meets the requirements in a .txt text file that contains a large number of file indexes:
Grep. 19Murray. Files_choice.txt
The above is the Linux system grep command how to find keywords in the contents of the file, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
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.