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 > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Grep command Basic regular expression examples Find specific characters:
Here we take the/etc/passwd file that stores all users of this machine as an example
Demo1:[root@localhost ~]# grep -n "root" /etc/passwd //-n indicates the display line number 1:root:x:0:0:root:/root:/bin/bash10:operator:x:11:0:operator:/root:/sbin/nologin The basic regular expression instance lookup set character:
When there are duplicate characters, use "[ ]" for set matching, matching only one character in "[ ]" at a time.
Demo2:[root@localhost ~]# grep -n "[fn]tp" /etc/passwd12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin27:ntp:x:38:38:::/etc/ntp:/sbin/nologin Reverse selection of basic regular expression instances:
Add a "^" to the square brackets in "[ ]" to indicate reverse selection (friends with a certain foundation must know that "^[ ]" indicates positioning the beginning of the line, where the meaning of the "^" inside and outside will be completely different).
Demo3:[root@localhost ~]# grep -n "^[^root]" /etc/passwd //matches all options except those starting with root 2:bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin... 42:named:x:25:25:Named:/var/named:/sbin/nologin Escape characters for basic regular expression instances:
A metacharacter in a regular expression, so here you need to use the escape character "\" to convert a character with special meaning to a normal character.
Demo4:[root@localhost ~]# grep -n '\.$ ' test.txt 1:he was short and fat.2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online.5:google is the best tools for search keyword. Basic regular expression examples--find any character & find duplicate characters:
decimal point (.) in regular expressions Also a metacharacter, representing any character.
Demo5:[root@localhost ~]# grep -n "r.. t" /etc/passwd //(.)decimal point here represents any character 1:root:x:0:0:root:/root:/bin/bash10:operator:x:11:0:operator:/root:/sbin/nologin12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
In the above results, the "root" string "r.. t"matches the rule. To query oo, oooo, ooooo, etc., you need to use the asterisk (*) metacharacter. Note, however, that the "*" represents a repetition of zero or more preceding single characters. "o*" indicates a character with zero (i.e. null) or greater than or equal to one "o"
Demo5:[root@localhost ~]# grep -n "oo*" /etc/passwd1:root:x:0:0:root:/root:/bin/bash2:bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:adm:x:3:4:adm:/var/adm:/sbin/nologin5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin...... Basic Regular Expression Examples Find Consecutive Character Ranges:
For example, to find consecutive characters of three to five o's, you need to use the scoped character "{ }" in the base regular expression. Because "{ }" has a special meaning in Shell, when using "{ }" character, you need to use escape character "\" to convert "{ }" character to ordinary character.
Demo6:[root@localhost ~]# grep -n "0\{2,\}" /etc/passwd //indicates a string containing more than 2 o's in the middle 11:games:x:12:100:games:/usr/games:/sbin/nologin41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bashEgrep command
In addition, the grep command only supports basic regular expressions, and if you use extended regular expressions, you need to use the egrep or awk command. The awk command is explained later, here we use the egrep command directly. The egrep command is similar to the grep command. (The grep command can be used with egrep as well)
Extended regular expression metacharacter effect + effect: repeat one or more previous characters? Role: zero or the previous character of one| Role: Find multiple characters using or (or)() Role: Find "group" string ()+ Role: Identify multiple duplicate groups Demo7:[root@localhost ~]# egrep -n "10+" /etc/passwd //use "+" extension metacharacter 11:games:x:12:100:games:/usr/games:/sbin/nologin31:qemu:x:107:107:qemu user:/:/sbin/nologin41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash[root@localhost ~]# egrep -n "10? " /etc/passwd //use "? "Extended metonym 2:bin:x:1:1:bin:/bin:/sbin/nologin9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10:operator:x:11:0:operator:/root:/sbin/nologin11:games:x:12:100:games:/usr/games:/sbin/nologin[root@localhost ~]# egrep -n 'root| zhy' /etc/passwd //use "|"Extended metonym 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash[root@localhost ~]# egrep -n '(f| n)tp' /etc/passwd //Use "()" extension metacharacters to match "|"Use 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin27:ntp:x:38:38::/etc/ntp:/sbin/nologinSed command together:
sed is a very good file processing tool, itself is a pipeline command, mainly to conduct processing in units of behavior, you can replace data lines, delete, add, select and other specific work
The workflow of sed mainly includes three processes: reading, executing and displaying:
1. Read: sed reads a line from an input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space).
2. Execution: By default, all sed commands are executed sequentially in the pattern space, and the sed command is executed sequentially on all lines unless a line address is specified.
3. Display: Send modified content to output stream. After sending data again, the pattern space will be cleared. This process is repeated until all the file content has been processed.
By default, all sed commands are done in schema space and are not saved. Sed command format:
sed [options] 'action' parameter
sed [options] -f scriptfile parameter// scriptfile indicates script file
Common options:
-e: Indicates that the input text file is processed with a specified command or script.
-f: Indicates that the input text file is processed with the specified script file.
H: Show help.
-n: indicates that only processed results are displayed.
-i: Edit text files directly.
Common "action" parameters:
a: Add, add a line below the current line.
c: Replace, replaces the selected row with the specified content.
d: Delete, delete the selected row
i: Insert, inserts a specified line above the selected line.
p: Print, which is usually used with the "-n" option
s: Replace, replace the specified character.
Y: Character conversion.
Basic usage example: Output all, the effect is equivalent to cat command: [root@localhost ~]# sed -n 'p' /etc/passwd //Equivalent cat command root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin... Output a specific line, or a segment of a line: [root@localhost ~]# sed -n '10p' /etc/passwd //Output line 10 operator:x:11:0:operator:/root:/sbin/nologin[root@localhost ~]# sed -n '2,4p' /etc/passwd //Output 2~4 lines bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin Output odd lines: [root@localhost ~]# sed -n 'n;p' /etc/passwd //Output odd lines, even behavior p;nbin:x:1:1:bin:/bin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/synchalt:x:7:0:halt:/sbin:/sbin/halt... Similarly, in addition to the basic usage, sed can also be used in conjunction with regular expressions to output lines containing specific content (like grep,^,$can be used to locate the beginning and end of a line):[root@localhost ~]# sed -n '/root/p' /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin Output lines containing specific words: [root@localhost ~]# sed -n '/\/p' /etc/passwd //\
< \>Represents word boundary root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin Replace eligible text:
sed 's/the/THE/' test. txt//Replace the first the in each line with THE
sed 's/l/L/3' test. txt//Replace the 3rd l in each line with L
sed 's/the/THE/g' test. txt//Replace all the the in the file with THE
sed 's/o//g' test. txt//Delete all o's in the file (replace them with empty strings)
sed 's/^/#/' test. txt//Insert #: at the beginning of each line
sed '/the/s/^/#/' test. txt//Insert #at the beginning of each line containing the
sed 's/$/EOF/' test. txt//Insert the string EOF at the end of each line
sed '3,5s/the/THE/g' test. txt//Replace all the the in lines 3 - 5 with THE
sed '/the/s/o/O/g' test. txt//Replace o with O in all lines containing the
Move text:
sed '/the/{H;d};$G' test. txt//Move the line containing the to the end of the file,{;} for multiple operations
sed '1 ~ 5 {H;d};17G' test. txt//Transfer lines 1 - 5 to line 17
sed '/the/w out.file' test. txt//save the line containing the as a file out.file
sed '/the/r /etc/hostname' test. txt//Add the contents of the file/etc/hostname after each line containing the
sed '3aNew' test. txt//Insert a new line after line 3, which reads New
sed '/the/aNew' test. txt//Insert a new line after each line containing the, saying New
sed '3aNew1\nNew2' test. txt//Insert multiple lines after line 3,\n in the middle indicates newline
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.