In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about regular expressions of shell scripts, grep, sed, and awk. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
-regular--
Basic regularity
^ word # # search for the beginning of ^ line in vi/vim that begins with word
Word$ # # search for the end of $line in vi/vim ending with word
^ $# # indicates a blank line
. # # represents and can only represent any character
\ example:\. Only represents the point itself, escapes the symbol, lets the character with the special identity meaning, take off the vest, restore
\ n # # newline character
\ r # # match enter
\ w # # matches any character and number
Repeat the preceding character 0 or more times
. * # # matches all characters. Example: ^. * begins with any number of characters, and. * $ends with any number of characters.
[abc] # # matches any character in the character set
[^ abc] # # matches content that does not contain any characters after ^. The ^ in square brackets is reversed.
[1-9] # # means to match any character within the range of parentheses
A\ n ~ m\} # # repeat the previous repeated character n times to m times. If you use egrep or sed-r, you can remove the slash
\ {n,\} # # repeat the previous repeated character at least n times. If you use egrep or sed-r, you can remove the slash
\ {n\} # # repeat the previous repeated character n times. If you use egrep or sed-r, you can remove the slash
\ {, m}\ # # repeat less than m
Note: egrep,grep-E or sed-r filter general special characters without escape
Extended regularization (egrep or grep-E)
Repeat the preceding character one or more times
? # # repeat the first character 0 times or at a time
| | # # or means to find multiple matching strings using or |
() # # find the string in parentheses
^ linux # # starts with linux linux$ # # ends linuxfan with linux. # # match linuxfans and other coo [kl] # # match cool or cook9 [^ 5689] # match 91Magi 92, etc., but do not match 95Magi 96pc98Power99 [0-9] # # match any one of the digits [Amurz] | [Amurz] # # match any one of all uppercase and lowercase letters, | belong to extended regular grep-E support colou?r # match color or colour, but not match colouurrollno-9+ # # match rollno-9 or rollno-99 Rollno-999, but does not match rollno-co*l # # match ma (tri) x # # such as cl,col,cool,coool match matrix [0-9] {3} # match any three digits Equal to [0-9] [0-9] [0-9] [0-9] {2,} # # match any two-digit or more digits [0-9] {2 # 5} # # match any number from two to five digits Oct (1st | 2nd) # # match Oct 1st or Oct 2nda\ .b # match A.B But it cannot match ajb [a-z 0-9] +\ @ [a-z0-9] +\. [aqizz] {2pm 4} # # match an email address [0-9] {1Mague 3}\. [0-9] {1jue 3}\. [0-9] {1pm 3}\. [0-9] {1pm 3} # match IP address-- grep--
Syntax: grep [option] [conditional expression] object file
Cat / proc/meminfo | grep-e Mem-e Cache-e Swap # # View system memory, cache, swap partition-e is used to match multiple expressions grep-R-o-n-E'[a-z0-9] +\ @ [a-z0-9 _] +. [Amurz] {2jue 4}'/ etc/ # find email addresses in all files in the / etc directory -R recursion,-n indicates matching line number,-o only outputs matching content,-E supports extended regular expressions, grep-R-c 'HOSTNAME' / etc/ | grep-v "0 $" # # find the number of files in the etc/ directory containing "HOSTNAME",-c count the number of matches,-v reverse grep-l' HOSTNAME' / etc/ # # find the file name containing "HOSTNAME" -l displays matching file names, and-L displays mismatched file names dmesg | grep-n-- color=auto 'eth' # # find the lines of eth in the kernel log, display color and line number dmesg | grep-n-A3-B2-- color=auto' eth' # # list core information with dmesg Then use grep to find the line containing eth, and in the first two lines and the last three lines of the keyword line, also find out to display cat / etc/passwd | grep-c bash$ # # count the number of users who can log in in the system touch / tmp/ {123 123jue 456 1234567} .txt # # create a test file, the following three commands have the same effect, matching the file name 123 Can contain one or more ls | grep-E'(123) +'ls | grep'\ (123\)\ +'ls | egrep'+'ps-ef | grep-c httpd # # count the number of httpd processes grep-C 4 'games'-- color / etc/passwd # # display four lines grep ^ adm / etc/group # # before and after the games match "- C" to view the adm group information ip a | grep-E '^ [0-9]' | awk-F:'{print $2}'# # get the Nic name ifconfig eth0 | grep-E-o 'inet addr: [^] *' | grep-o'[0-9.] * # # intercept the ip address [^] * means to use a non-empty character as a Terminator [0-9.] * indicates the combination of numbers and points ip a | grep inet | grep eth0 | grep-o "inet [^ /] *" | grep-o "[0-9.] *" # # intercept ip address ifconfig eth0 | grep-I hwaddr | awk'{print $5}'# # intercept MAC address ip a | grep-A 3 "eth0" | grep link/ether | grep-o "ether [^ r] *" | grep-o-E "[0-9a-f:] +" | grep-E "[0-9a-f] [0 -9a-f:] {2} $"# # intercept MAC address grep" ^ m "oldboy.log # # filter output line starting with m grep" m $"oldboy.log grep-vn" ^ $"oldboy.log # # filter blank line grep-o" 0* "oldboy.log grep-o" oldb.y "oldboy.log grep". $"oldboy.log # # to. The line at the end of grep "0\ {3\}" oldboy.log # # is repeated three times
-- sed--
Syntax: sed [options] 'command' file (s)
Options:
-n suppresses automatic printing pattern space,sed default output all,-n is used to cancel default output
-I edit the file
-r supports extended regular expressions
1. Change:
Syntax: sed'/ regular match condition / sqqqr OldUniverse new _ G' file
Sed's etc/sysconfig/network-scripts/ifcfg-eth2 dhcplash / etc/sysconfig/network-scripts/ifcfg-eth2 # # only shows and does not modify it
Sed-I's etc/sysconfig/network-scripts/ifcfg-eth2 DHCP _ DHCP _
Sed-I's dhcp DHCP dhcp DHCP sed G'HCP # replace all DHCP with DHCP
Sed-I'/ ^ IP1/s/static/dhcp/g' ip # # replace the line beginning with IP1
Sed-I'2s _
Cat-n / etc/selinux/config # # View and display the line number
Sed-I'7s Universe sed / etc/selinux/config # # turn on selinux
two。 Delete:
Syntax: sed'/ expression / d' file
Vim ip # # add blank lines
Sed'/ ^ $/ d'ip # # delete the blank line and display it on the screen
Sed-I'/ IP1/d' ip # # delete the line containing IP1
Sed-I'/ ^ IP2/d' ip # Delete lines starting with IP2
Sed-I '2d' ip # # delete the second line
3. Increase:
Syntax: sed'/ expression / a "text to be added" file
Sed'an IP3=static' ip # # add IP3=static after each line
Sed'3a IP3=static' ip # # only adds IP3=static after line 3 and shows no modification
Sed'3i IP3=static' ip # # only adds IP3=static before line 3, showing no modification
Sed-I'3a IP3=static' ip # # modified, do not show
Sed-I'/ ^ IP3/a "test add"'ip # # add after the line that begins with IP3
4. Check:
Syntax: sed-n'/ expression / p 'file sed-n' 2p' / etc/hosts # # View the second line sed-n'/ www/p' / var/named/chroot/var/named/linuxfan.cn.zone # # View the resolution record sed-n'/ .100 $/ p'/ var/named/chroot/var/named/linuxfan.cn.zone # # View the line sed-n'2p'ip # # ending with .100 Ifconfig eth0 is displayed every two lines | sed-n '2p' | sed's contents. Sed's # Bc.*##g' # # Note: when the content processed by the sed command is multi-line content, the expression is separated by /. If the content processed by the sed command is single-line content, it is used for interception, and the # sign is used as the separator. 10.0.0.9 ifconfig eth0 | sed-n '2p' | sed-r's # (. * dr:) (. *) (Bc.*$) #\ 2 Extensible regular,\ 2 escape 2 Print out the second range (. *) 10.0.0.9ifconfig eth0 | sed-n '2p' | sed-r' s#.*dr: (. *) Bc.*$#\ 1#g'10.0.0.9 ifconfig eth0 | sed-nr '2s# ^. * dr: (. *) Bc.*$#\ 1#gp'10.0.0.9ifconfig eth0 | sed-nr' 1s# ^. * dr (. *) #\ 1#gp'00:0C:29:33:C8: 75ifconfig eth0 | sed-n '1p' | sed-r' s # (^. * dr) (. *) #\ 2#g'00:0C:29:33:C8:75ifconfig eth0 | sed-n '1p' | sed's # ^. * dr # # g'00:0C:29:33:C8:75ifconfig eth0 | sed-nr' 1s# ^. * t (. *) 00.subscription #\ 1#gp'HWaddrstat / etc/hosts | sed-n '4p' Access: (0644) /-rw-r--r--) Uid: (0 / root) Gid: (0 / root) stat / etc/hosts | sed-n '4p' | sed's # ^. * ss: (# # g' | sed' s#/-.*$##g'0644stat / etc/hosts | sed-n '4p' | sed-r's # ^. * s:\ ((. *) /. * $#\ 1#g'0644stat / etc/hosts | sed-nr' 4s# ^ . * s:\ ((. *) / -. * $#\ 1#gp'0644stat / etc/hosts | sed-nr '4s# (^. * s:\ () (. *) (/ -. * $) #\ 2
Syntax:
Awk [options] 'Mode {Action (action)}' File 1 File 2.
Option:-F specifies the input delimiter, which can be a string or a regular expression
Common actions: print, printf
Chkconfig-- list | grep 3: enabled | awk'{print $1} 'tail-1 / etc/passwd | awk-F': 'BEGIN {OFS= "- -"} {print $1 $7}'# # OFS specifies the output separator ifconfig eth2 | awk-F'[:] + 'NR==2 {print $4}' ifconfig eth2 | awk-F'[:] +''NR==2 {print "eth2_ip=" $4}' # # you can add the display awk 'BEGIN {print "line one\ nline two\ nline three"}'
Match range (ranges): the specified match range, formatted as part1,part2
Awk-F: awk-F:'$3pm / etc/passwdawk-F:'$1pm = "root", $1pm = "adm" {print $1pm / etc/passwdawk-F:'/ ^ r lead / ^ r lead / ^ a / {print $1m 3mom 7}'/ etc/passwd
Awk block principle:
Regional composition:
BEGIN {actions} # # actions before starting to process the first line of text
{Action} # processing actions for each line of text
END {actions} # # actions after processing the last line of text
Perform the process:
First perform the initialization operation in the BEGIN {} block
Then cycle through a row of data from the specified data file (automatically update NF, NR, $0, $1... Wait for the value of the built-in variable), and execute 'mode or condition {action}'
Finally, the subsequent processing operation in the END {} block is performed.
Case study:
Awk-F: 'BEGIN {printf "%-10s%-10s%-20s\ n", "UserName", "ID", "Shell"} {printf "%-10s%-10s%-20s\ n", $1J / etc/passwd # # print header BEGIN {} ifconfig eth0 before awk processing | awk-fuzz' NR==2 {print $2 'NR==2 {print $2}' | awk 'BEGIN {OFS= "/"} {print "IP=" $1, "MASK=" $3}'
Variables for awk:
Awk variable:
FS: column delimiter, default bit white space
RS: line delimiter, default bit newline character
OFS: output column delimiter
ORS: output line delimiter
Awk built-in variables:
NR: number of rows in process
FNR: the number of lines in a single file
NF: number of columns
Case study:
Ifconfig eth2 | awk'{print NR} 'ifconfig eth2 | awk' {print NF}'
Custom variable case:
Awk 'BEGIN {test= "www.linuxfan.cn"; print test}' awk-v test= "linuxfan.cn" BEGIN {print test}'
Use of printf:
Format: printf "format", listing 1, listing 2.
Characteristics:
a. You must specify format (format), which is used to specify the output format of the subsequent item (list)
The b.printf statement does not automatically print newline characters:\ n
The c.format format is in% plus one character, as follows:
% c: ASCII code that displays characters
% dline% I: decimal integer
% f: show floating point number (decimal)
% s: display string
% u: unsigned integer
%%: show%
d. Modifier: n: display width, -: left alignment, +: display numeric symbols, such as%-c (left alignment)
Case study:
Chkconfig-- list | grep 3: enabled | awk'{printf "%-10s", $1}'# # is displayed on a unified line
Awk-F:'{printf "%-15s%-10d%-10s\ n", $1 etc/passwd
Operator for awk:
Arithmetic operators: X ^ y, x _ gamy, x _ r _ y, x _ m _ y, x% y
Comparison operators: >, =,
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.