shell scripts - regular, grep, sed, awk

Keywords: Linux network SELinux vim

-- regular --

Basic regularity

^word     ##Search for word initial vi/vim in ^ The beginning of a line
word$     ##Search for word Ending vi/vim in $ The end of a line
^$        ##Blank line
.         ##Represents and can only represent any character
\         ##Example:  Represents only the point itself, escapes the symbol, lets the character which has the special status significance, takes off the waistcoat, restores
\n        ##Newline character
\r        ##Matching carriage return
\w         ##Match any character and number
*         ##Repeat the previous character 0 or more times
.*        ##Match all characters. Example:^.* Starting with any number of characters,.*$End with any number of characters
[abc]     ##Match any character in the character set
[^abc]    ##Matching does not include ^ The content of any character after that. In brackets ^ In order to reverse
[1-9]     ##Represents any character within matching brackets
a\{n,m\}  ##repeat n reach m The previous repeated character. If used egrep,sed -r The diagonal line can be removed.
\{n,\}    ##Repeat at least n The previous repeated character. If used egrep,sed -r The diagonal line can be removed.
\{n\}     ##repeat n The previous repeated character. If used egrep,sed -r The diagonal line can be removed.
\{,m}\    ##Repeat less than m times

Note: egrep, grep-E or sed-r filter general special characters can not be escaped

Extended Regularity (egrep or grep-E)

+## Repeat one or more previous characters
Repeat 0 times or a character before one time
|## Or, to find multiple matching strings by or
() Find the string in parentheses
^linux         ##Start with linux
linux$         ##End with linux
linuxfan.      ##Match Linux fans, etc.
coo[kl]        ##Match cool or cook
9[^5689]       ##Matching 91,92 etc., but not 95,96,98,99
[0-9]          ##Match any number
[a-z]|[A-Z]    ##Match any upper and lower case letter, | belongs to extended regular grep-E support
colou?r        ##Match color or colour, but not colouur
rollno-9+      ##Match rollno-9, rollno-99, rollno-999, but not rollno-
co*l           ##Match cl, col, cool, coool, etc.
ma(tri)x       ##Match matrix
[0-9]{3}       ##Match any three digits, equal to [0-9] [0-9] [0-9]
[0-9]{2,}      ##Match any two or more digits
[0-9]{2,5}     ##Match any number from two to five digits
Oct (1st|2nd)  ##Match Oct 1st or Oct 2nd
a\.b           ##Match a.b, but not ajb
[a-z0-9_]+\@[a-z0-9_]+\.[a-z]{2,4}              ##Match a mailbox address
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}  ##Matching IP Address

 

--grep--

Syntax: grep [option] [conditional expression] target file

 

cat /proc/meminfo |grep  -e Mem -e Cache -e Swap         ##Viewing system memory, caching, swapping partitions - e matches multiple expressions
grep -R -o -n -E   '[a-z0-9_]+\@[a-z0-9_]+\.[a-z]{2,4}' /etc/         ##Find e-mail addresses in all files in the / etc directory; - R recursion, - n represents matched line numbers, - o only outputs matched content, - E supports extended regular expressions.
grep -R -c 'HOSTNAME' /etc/ |grep -v "0$"          ##Find the number of "HOSTNAME" in the file under / etc / directory, - c counts the number of matches, - v reverses
grep -R -l 'HOSTNAME' /etc/                      ##Find the file name that contains "HOSTNAME", - L shows the matching file name, and - L shows the mismatching file name.
dmesg | grep -n --color=auto 'eth'              ##Find the line of eth in the kernel log, display the color and line number
dmesg | grep -n -A3 -B2 --color=auto 'eth'          ##List the core information with dmesg, then use grep to find the line containing eth, and find the first two lines and the last three lines of the line where the key word is located.
cat /etc/passwd |grep -c bash$                  ##Number of logged-in users in the statistical system
touch /tmp/{123,123123,456,1234567}.txt      ##Create a test file, the following three commands are the same effect, matching file name 123, can contain one or more
ls |grep -E '(123)+' 
ls |grep '\(123\)\+'
ls |egrep  '(123)+'
ps -ef |grep -c httpd                          ##Statistics the number of httpd processes
grep -C 4 'games' --color /etc/passwd              ##Show 4 lines before and after'-C'matching games
grep ^adm /etc/group                          ##View adm group information
ip a |grep -E '^[0-9]' |awk -F : '{print $2}'          ##Get the name of the network card
ifconfig eth0 |grep -E -o 'inet addr:[^ ]*' |grep  -o '[0-9.]*'      ##Intercept the ip address, [^]* denotes a non-empty character as the terminator, [0-9.]* denotes a combination of numbers and points.
ip a |grep inet |grep eth0 |grep -o "inet[^/]*" |grep -o "[0-9.]*"    ##Intercepting 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:]{2}$"            ##Intercept MAC address

grep "^m" oldboy.log             ##Filter output lines beginning with m
grep "m$" oldboy.log 
grep -vn "^$" oldboy.log             ##Filtered blank lines
grep -o "0*" oldboy.log 
grep -o "oldb.y" oldboy.log 
grep "\.$" oldboy.log             ##Lines ending with.
grep "0\{3\}" oldboy.log             ##Repeat three times.

 

--sed--

Syntax: sed [options]'command'file (s)
Options:
- N suppresses automatic print pattern space, sed default output is all, - n is used to cancel default output
- i Edit Documents
- r supports extended regular expressions

1. changes:
Syntax: sed'/ Regular Matching Conditions / s/old/new/g'file
Sed's/dhcp/static/g'/etc/sysconfig/network-scripts/ifcfg-eth1 # is only displayed, not modified
Sed-i's/dhcp/static/g'/etc/sysconfig/network-scripts/ifcfg-eth1# only modified, not displayed
Sed-i's/dhcp/static/g'ip # Replace all DHCP with static
Sed-i'/^IP1/s/static/dhcp/g'ip # Replaces the line at the beginning of IP1
Sed-i'2s/static/dhcp/g'ip # # Specifies 2 line substitution for a specific line number
Cat-n/etc/selinux/config \ View and display line numbers
Sed-i'7s/disabled/enforcing/g'/etc/selinux/config# Open SELinux

2. delete:
Syntax: sed'/expression/d'file
VIM IP Add blank lines
Sed'/^$/d'IP # Deletes empty lines and displays them on the screen
Sed-i'/IP1/d'IP Delete rows containing IP1
Sed-i'/^IP2/d'IP Delete rows starting with IP2
Sed-i'2d'IP Delete the second line

3. increase:
Syntax: sed'/ expression / a "Text to be added" file
Sed'a IP3=static'IP \ Add IP3=static after each line
Sed'3a IP3=static'IP # Add IP3=static only after line 3 and show no modification
Sed'3i IP3=static'IP # Add IP3=static only before line 3, showing no modification
Sed-i'3a IP3 = static'ip # modification, not displayed
Sed-i'/^IP3/a "test add"'ip"# is added after the line beginning 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 parsed records containing www
Sed-n'/.100$/p'/var/named/chroot/var/named/linuxfan.cn.zone# View lines ending with.100
Sed-n'2~2p'IP \ from the second line, displayed every two lines

ifconfig eth0|sed -n '2p'|sed 's#.*dr:##g'|sed 's# Bc.*##g'        ##Note: when sed If the command processes more than one line of content, the/As a separation of expressions, if sed The command handles a single line of content as interception.#Number as delimiter;
10.0.0.9 
ifconfig eth0|sed -n '2p'|sed -r 's#(.*dr:)(.*)(Bc.*$)#\2#g'        ##- r supports extended regularity,  2 escapes 2 and prints out the second range (. *)
10.0.0.9  
ifconfig 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.9
ifconfig eth0|sed -nr '1s#^.*dr (.*)#\1#gp'
00:0C:29:33:C8:75  
ifconfig eth0|sed -n '1p'|sed -r 's#(^.*dr )(.*)#\2#g'
00:0C:29:33:C8:75
ifconfig eth0|sed -n '1p'|sed 's#^.*dr ##g'            
00:0C:29:33:C8:75
ifconfig eth0|sed -nr '1s#^.*t (.*) 00.*$#\1#gp'
HWaddr
stat /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'
0644
stat /etc/hosts|sed -n '4p'|sed -r 's#^.*s: \((.*)/-.*$#\1#g'
0644
stat /etc/hosts|sed -nr '4s#^.*s: \((.*)/-.*$#\1#gp'
0644
stat /etc/hosts|sed -nr '4s#(^.*s: \()(.*)(/-.*$)#\2#gp'
0644

 

--awk--

Syntax:
awk [Options]'Mode {action}'File 1 File 2 ____________
Option: -F Specifies an input separator, which can be a string or regular expression
Common actions: print, printf

chkconfig --list |grep 3:Enable |awk '{print $1}'
tail -1 /etc/passwd |awk -F ':' 'BEGIN{OFS="---"}{print $1,$6,$7}'   ##OFS Specifies Output Separator
ifconfig eth1 |awk -F '[ :]+' 'NR==2 {print $4}'
ifconfig eth1 |awk -F '[ :]+' 'NR==2 {print "eth1_ip="$4}'  ##Display content can be added
awk 'BEGIN {print "line one \nline two\nline three"}'

 

ranges: The specified range of matches, in the form of part1,part2
awk -F : '$3==3,$3==10{print $1,$3,$7}' /etc/passwd
awk -F : '$1=="root",$1=="adm"{print $1,$3,$7}' /etc/passwd
awk -F : '/^r/,/^a/{print $1,$3,$7}' /etc/passwd

awk block principle:
Regional composition:
BEGIN {Action} # # Begins the operation before processing the first line of text
Processing operations for each line of text
Operation after END {Action} # # has processed the last line of text
Execution process:
First, the initialization operation in BEGIN {} block is executed.
Then read a row of data from the specified data file (automatically update NF, NR, $0, $1... The value of the built-in variable is equal to the value of the built-in variable, and the'mode or condition {action}'is executed.
Finally, follow-up processing operations in END {} blocks are performed.
Case study:
Awk-F:'BEGIN {printf'%-10s%-10s%-20s\ n','UserName','ID','Shell'} {printf'%-10s%-20s n', $1, $3, $7}'/ etc/passwd \ print the head BEGIN {before awk processing.

ifconfig eth0 |awk -F':' 'NR==2{print $2,$4}'|awk 'BEGIN{OFS=" / "}{print "IP="$1,"MASK="$3}'

The awk variable:
awk variables:
FS: Column separator, default bit blank
RS: Line delimiter, default byte newline character
OFS: Output column separator
ORS: Output line Separator
awk built-in variables:
NR: Number of rows in processing
FNR: Number of rows per file
NF: Number of columns
Case study:
ifconfig eth1 |awk '{print NR}'
ifconfig eth1 |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...
Features:
a. Format must be specified to specify the output format of the subsequent item (list)
b.printf statements do not print line breaks automatically: \n
c.format format with% plus one character, as follows:
% c: ASCII code for displaying characters
% d,% i: decimal integers
% f: Display floating point numbers (decimal)
% s: Display string
% u: Unsigned integers
%%: display%
d. modifier: N: display width, -: left alignment, +: display numeric symbols, such as%-c (left alignment)
Case study:
chkconfig --list |grep 3: Enable | awk'{printf'%-10s', $1}'# to be displayed on a single line
awk -F : '{printf "%-15s %-10d %-10s\n",$1,$3,$7}' /etc/passwd

The awk operator:
Arithmetic operators: x^y, x/y, x+y, x-y, x%y
The comparison operator: >,<,>=,<=,=,<=,=,!!=
Logical Operator: &amp,|,!

Common pattern types of awk
Regular expression (regexp): awk-F:'/^u/{print $1}'/etc/passwd
Expressions: Value bit non-zero or bit non-empty is satisfied, such as $1 ~/foo/or $1 == "root"
Case study:
Awk-F:'$3 >= 500 {print $1, $3, $7}'/ etc/passwd # # Print Ordinary User
Awk-F:'$3 + 1 <== 100 & &$3 + 1 >= 10 {print $1, $3, $7}'/ etc / passwd # UID users between 10 and 100
Awk-F:'$2=="!"{print $1, $2}'/ etc/shadow # Checks users with uninitialized passwords
passwd -d u01
Awk-F:'$2="{print $1}'/ etc/shadow # # User whose Print Password is Empty ____________
Awk-F:'$7 ~"bash $"{print $1, $3, $7}'/ etc/passwd # matching $7 is the end line of Bash
awk -F : '$7!~"bash$"{print $1,$3,$7}' /etc/passwd

Posted by Nicholas Reed on Thu, 09 May 2019 10:28:40 -0700