In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain "the specific usage of Linux common instructions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the specific usage of Linux common instructions.
Linux common instructions-grep (search filtering) grep command in the Linux system is a powerful text search tool, it can use regular expressions to search text and print 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.
Grep works like this by searching for string templates in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as file names. The results of the search are sent to standard output without affecting the contents of the original file.
Grep can be used for shell scripts because grep returns a status value to indicate the status of the search, 0 if the template search is successful, 1 if the search is unsuccessful, and 2 if the searched file does not exist. We can use these return values to do some automated text processing.
1. Command format:
Grep [option] pattern file
2. Command function:
Specific characters used to filter / search. Regular expressions can be used in conjunction with a variety of commands, and they are very flexible in use.
3. Command parameters:
-a-- text # Don't ignore binary data.
-A-- after-context= # displays not only the column that matches the template style, but also the content after that line.
-b-- byte-offset # marks the number of the first character of the line before displaying the line that matches the style.
-B-- before-context= # displays not only the line that matches the style, but also the content before that line.
-c-- count # calculates the number of columns that match the style.
-C-- context= or-# displays not only the line that matches the style, but also the content before and after the line.
-d-- directories= # this parameter must be used when you specify that you are looking for a directory instead of a file, otherwise the grep instruction will return information and stop the action.
-e-- regexp= # specifies the string as the style for finding the contents of the file.
-E-- extended-regexp # uses the style as an extended common representation.
-f-- file= # specifies a rule file whose content contains one or more rule styles and lets grep find the content of the file that meets the rule criteria in the format of one rule style per line.
-F-- fixed-regexp # treats styles as a list of fixed strings.
-G-- basic-regexp # treats styles as common representations.
-h-- no-filename # does not indicate the name of the file to which the line belongs until the line that matches the style is displayed.
-H-- with-filename # indicates the name of the file to which the line belongs before displaying the line that matches the style.
-I-- ignore-case # ignores the difference in character case.
-l-- file-with-matches # lists the file names whose contents match the specified style.
-L-- files-without-match # lists file names whose contents do not match the specified style.
-n-- line-number # marks the column number of the row before displaying the row that matches the style.
-Q-- quiet or-- silent # does not display any information
-r-- recursive # this parameter has the same effect as specifying the "- d recurse" parameter.
-s-- no-messages # does not display an error message.
-v-- revert-match # displays all lines that do not contain matching text.
-V-- version # displays version information.
-w-- word-regexp # displays only columns that match the whole word.
-x-- line-regexp # displays only the columns that match the entire column.
-y # this parameter has the same effect as specifying the "- I" parameter.
4. Regular expression:
Regular expression for grep:
The beginning of ^ # anchored lines such as:'^ grep' matches all lines that begin with grep.
The end of the $# anchor line is: 'grep$' matches all lines ending in grep.
. # matches a character that is not a newline character such as: 'gr.p' matches gr followed by any character, followed by p.
* # matches zero or more previous characters such as:'* grep' matches all one or more spaces followed by the line of grep.
. * # together to represent any character.
[] # matches a specified range of characters, such as'[Gg] rep' matches Grep and grep.
[^] # matches a character that is not within the specified range, such as:'[^ A-FH-Z] rep' matches the beginning of a letter that does not contain Amurr and Tmurz, followed by a line of rep.
The\ (..\) # tag matches characters, such as'\ (love\)', and love is marked as 1.
\
< #锚定单词的开始,如:'\ #锚定单词的结束,如'grep\>'match the line that contains the word ending in grep.
X\ {m\} # repeats the character xMagol m times, for example:'0\ {5\} 'matches a line containing 5 os.
X\ {m,\} # repeat character x, at least m times, for example:'o\ {5,\} 'matches lines with at least 5 o.
X\ {mdirection n\} # repeat character x, at least m times, no more than n times, for example:'o\ {5jue 10\} 'matches the lines of 5 Murray 10 o.
\ w # matches literal and numeric characters, that is, [A-Za-z0-9], for example:'G\ wimpp 'matches G followed by zero or more literal or numeric characters, followed by p.
The inverted form of\ W #\ w that matches one or more non-word characters, such as periods, periods, etc.
\ b # word lock, such as:'\ bgrep\ b 'only matches grep.
POSIX characters:
In order to maintain consistency in character encodings in different countries, POSIX (The Portable Operating System Interface) adds special character classes, such as [: alnum:] is another word for [A-Za-z0-9]. You need to put them in the [] sign to become regular expressions, such as [A-Za-z0-9] or [: alnum:]. Grep under linux supports POSIX character classes except fgrep.
[: alnum:] # alphanumeric character
[: alpha:] # literal character
[: digit:] # numeric character
[: graph:] # non-empty character (non-space, control character)
[: lower:] # lowercase character
[: cntrl:] # control character
[: print:] # non-empty characters (including spaces)
[: punct:] # punctuation mark
[: space:] # all white space characters (new lines, spaces, tabs)
[: upper:] # uppercase character
[: xdigit:] # hexadecimal digits (0-9 mai mai f mai F)
5. Use examples:
Example 1: find the specified process
Command:
Ps-ef | grep svn
Output:
[root@localhost ~] # ps-ef | grep svn
Root 4943 1 0 Dec05? 00:00:00 svnserve-d-r / opt/svndata/grape/
Root 16867 16838 0 19:53 pts/0 00:00:00 grep svn
[root@localhost ~] #
Description:
The first record is the process found; the second result is the grep process itself, not the one you are really looking for.
Example 2: find the specified number of processes
Command:
Ps-ef | grep svn-c
Ps-ef | grep-c svn
Output:
[root@localhost ~] # ps-ef | grep svn-c
two
[root@localhost ~] # ps-ef | grep-c svn
two
[root@localhost ~] #
Description:
Example 3: read keywords from a file for search
Command:
Cat test.txt | grep-f test2.txt
Output:
[root@localhost test] # cat test.txt
Hnlinux
Peida.cnblogs.com
Ubuntu
Ubuntu linux
Redhat
Redhat
Linuxmint
[root@localhost test] # cat test2.txt
Linux
Redhat
[root@localhost test] # cat test.txt | grep-f test2.txt
Hnlinux
Ubuntu linux
Redhat
Linuxmint
[root@localhost test] #
Description:
Output lines in the test.txt file that contain keywords read from the test2.txt file
Example 4: read keywords from a file to search and display line numbers
Command:
Cat test.txt | grep-nf test2.txt
Output:
[root@localhost test] # cat test.txt
Hnlinux
Peida.cnblogs.com
Ubuntu
Ubuntu linux
Redhat
Redhat
Linuxmint
[root@localhost test] # cat test2.txt
Linux
Redhat
[root@localhost test] # cat test.txt | grep-nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test] #
Description:
Output lines in the test.txt file that contain keywords read from the test2.txt file, and display the line number of each line
Example 5: find keywords from the file
Command:
Grep 'linux' test.txt
Output:
[root@localhost test] # grep 'linux' test.txt
Hnlinux
Ubuntu linux
Linuxmint
[root@localhost test] # grep-n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test] #
Description:
Example 6: find keywords from multiple files
Command:
Grep 'linux' test.txt test2.txt
Output:
[root@localhost test] # grep-n 'linux' test.txt test2.txt
Test.txt:1:hnlinux
Test.txt:4:ubuntu linux
Test.txt:7:linuxmint
Test2.txt:1:linux
[root@localhost test] # grep 'linux' test.txt test2.txt
Test.txt:hnlinux
Test.txt:ubuntu linux
Test.txt:linuxmint
Test2.txt:linux
[root@localhost test] #
Description:
In the case of multiple files, when outputting the line of information content that has been queried, the name of the file is output at the beginning of the line and marked with ":"
Instance 7:grep does not show its own process
Command:
Ps aux | grep\ [s] sh
Ps aux | grep ssh | grep-v "grep"
Output:
[root@localhost test] # ps aux | grep ssh
Root 2720 0.0 0.0 62656 1212? Ss Nov02 0:00 / usr/sbin/sshd
Root 16834 0.0 0.0 88088 3288? Ss 19:53 0:00 sshd: root@pts/0
Root 16901 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh
[root@localhost test] # ps aux | grep\ [s] sh
Root 2720 0.0 0.0 62656 1212? Ss Nov02 0:00 / usr/sbin/sshd
Root 16834 0.0 0.0 88088 3288? Ss 19:53 0:00 sshd: root@pts/0
[root@localhost test] # ps aux | grep ssh | grep-v "grep"
Root 2720 0.0 0.0 62656 1212? Ss Nov02 0:00 / usr/sbin/sshd
Root 16834 0.0 0.0 88088 3288? Ss 19:53 0:00 sshd: root@pts/0
Description:
Example 8: find the content of the line that starts with u
Command:
Cat test.txt | grep ^ u
Output:
[root@localhost test] # cat test.txt | grep ^ u
Ubuntu
Ubuntu linux
[root@localhost test] #
Description:
Example 9: output lines that do not start with u
Command:
Cat test.txt | grep ^ [^ u]
Output:
[root@localhost test] # cat test.txt | grep ^ [^ u]
Hnlinux
Peida.cnblogs.com
Redhat
Redhat
Linuxmint
[root@localhost test] #
Description:
Example 10: output the contents of a line ending in hat
Command:
Cat test.txt | grep hat$
Output:
[root@localhost test] # cat test.txt | grep hat$
Redhat
Redhat
[root@localhost test] #
Description:
Example 11: output ip address
Command:
Ifconfig eth0 | grep-E "([0-9] {1pm 3}\.) {3} [0-9]"
Output:
[root@localhost test] # ifconfig eth0 | grep "[0-9]\ {1Magol 3\}\. [0-9]\ {1JEI 3\}\. [0-9]\ {1JE3\}. [0-9]\ {1JE3\}"
Inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test] # ifconfig eth0 | grep-E "([0-9] {1pr 3}\.) {3} [0-9]"
Inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test] #
Description:
Example 12: displays content lines that contain ed or at characters
Command:
Cat test.txt | grep-E "ed | at"
Output:
[root@localhost test] # cat test.txt | grep-E "peida | com"
Peida.cnblogs.com
[root@localhost test] # cat test.txt | grep-E "ed | at"
Redhat
Redhat
[root@localhost test] #
Description:
Example 13: displays all lines in files ending in .txt under the current directory that contain strings with at least seven consecutive lowercase characters for each string
Command:
Grep'[a Murz]\ {7\}'* .txt
Output:
[root@localhost test] # grep'[amurz]\ {7\}'* .txt
Test.txt:hnlinux
Test.txt:peida.cnblogs.com
Test.txt:linuxmint
[root@localhost test] #
Example 14: the log file is too large to view. We need to see what we want from it, or get the same kind of data, such as those without 404 days log information.
Command:
Grep'. Access1.log | grep-Ev '404' > access2.log
Grep'. Access1.log | grep-Ev'(404 | / photo/ | / css/)'> access2.log
Grep'. Access1.log | grep-E '404' > access2.log
Output:
[root@localhost test] # grep "." access1.log | grep-Ev "404" > access2.log
Description: the first two sentences of the above three commands are to look up the access1.log file in the current directory, find those lines that do not contain 404, put them into access2.log, and then remove 'vault, that is, put the lines with 404 into access2.log.
At this point, I believe you have a deeper understanding of "the specific usage of Linux common instructions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.