First,
Shell
Four The Swordsman (
grep
)
grep fetch rows
Regular Expressions:
[0-9] denotes character match 0-9 {1,3} indicates that the number of matching characters is less than 3. |
Example one:
[email protected] bash]# cat Test.txt #源文件 My name is Lingshu MySQL Install 192.168.1.11 192.168.2. 10.28 172.16.22.34 Hello Word [[email protected] bash]# grep "^10" Test.txt #表示以10开头的行 10.28 [[email protected] bash]# grep "11$" Test.txt #表示以11结尾的行 192.168.1.11 [[email protected] bash]# grep "[0-9]" test.txt #表示有匹配0-line of 9 characters 192.168.1.11 192.168.2. 10.28 172.16.22.34 [[email protected] bash]# grep "[0-9][0-9]" Test.txt #表示匹配连续两个0-9 characters in the line 192.168.1.11 192.168.2. 10.28 172.16.22.34 [[email protected] bash]# grep "[0-9][0-9][0-9]" Test.txt #便是匹配连续三个0-9 characters in the line 192.168.1.11 192.168.2. 172.16.22.34 [Email protected] bash]# grep "[A-z]" Test.txt #表示匹配所有小写字母所在的行 My name is Lingshu MySQL Install Hello Word [[email protected] bash]# grep "^[a-z]" Test.txt #表示匹配以小写字母开头的行 My name is Lingshu Hello Word [[email protected] bash]# grep "^[a-z]" Test.txt #表示匹配以大写字母开头的行 MySQL Install [Email protected] bash]# echo "168" >>!$ echo "168" >> test.txt [[email protected] bash]# grep "168" test.txt 192.168.1.11 192.168.2. 168 [[email protected] bash]# grep "^168$" Test.txt #以168开头且结尾的行 168 [email protected] bash]# Cat Test.txt | Grep-e "[0-9]{1,3}" is one to three consecutive numbers, and is in accordance with 0-9 192.168.1.11 192.168.2. 10.28 172.16.22.34 168 [email protected] bash]# Cat Test.txt | Grep-e "[0-9]{1,3}\.] #连续一到三个数, and is a number that matches 0-9 and is followed by a decimal point 192.168.1.11 192.168.2. 10.28 172.16.22.34 [email protected] bash]# Cat Test.txt | Grep-e "([0-9]{1,3}\.) {3} "# #连续一到三个数, and is 0-9 followed by the number of decimal points; Such a number is three consecutive times 192.168.1.11 192.168.2. 172.16.22.34 [email protected] bash]# Cat Test.txt | Grep-e "([0-9]{1,3}\.) {3} [0-9] {1,3} "is #连续一到三个数 and is 0-9 followed by the number of decimal points, so that the number is three consecutive times plus one, one to three 0-9 of the number 192.168.1.11 172.16.22.34 [[email protected] bash]# grep-e "192|lingshu" test.txt #-e "parameter 1 | parameter 2" represents the line matching parameter 1 or parameter 2. Can also be written as Egrep "192 | Lingshu "Test.txt My name is Lingshu 192.168.1.11 192.168.2. |
Second,
Shell
Four The Swordsman (
awk
)
awk Fetch Column
Example one: Crawling IP
[email protected] bash]# cat Ip.txt localhost 127.0.0.1 24 Guanli 192.168.3.2 24 Yewu 192.168.4.9 24 MySQL 172.16.10.10 24 [[email protected] bash]# awk ' {print $} ' ip.txt #$2 means that the second column is crawled $NF represents the last column. 127.0.0.1 192.168.3.2 192.168.4.9 172.16.10.10 [[email protected] bash]# awk ' {print ' IP: ' $ $ ' ip.txt #支持加注释 ip:127.0.0.1 ip:192.168.3.2 ip:192.168.4.9 ip:172.16.10.10 |
Example two: Split crawl user list
[Email protected] bash]# TAIL/ETC/PASSWD Setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin Pulse:x:171:171:pulseaudio System Daemon:/var/run/pulse:/sbin/nologin Gdm:x:42:42::/var/lib/gdm:/sbin/nologin Gnome-initial-setup:x:992:987::/run/gnome-initial-setup/:/sbin/nologin Sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologin Avahi:x:70:70:avahi MDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin Postfix:x:89:89::/var/spool/postfix:/sbin/nologin Ntp:x:38:38::/etc/ntp:/sbin/nologin Tcpdump:x:72:72::/:/sbin/nologin Lingshu:x:1000:1000:lingshu:/home/lingshu:/bin/bash [Email protected] bash]# TAIL/ETC/PASSWD | Awk-f: ' {print '} ' #-f: Denotes a colon: split Setroubleshoot Pulse Gdm Gnome-initial-setup Sshd Avahi Postfix Ntp Tcpdump Lingshu [Email protected] bash]# Tail-n 2/etc/passwd | Awk-f: ' {print $ ' > User.txt #抓去最后两个并生成用户列表 [email protected] bash]# cat!$ Cat User.txt Tcpdump Lingshu |
Third,
Shell
Four The Swordsman (
sed
)
Sed change character
notation:
/lingshu #表示匹配翎戍 S/lingshu #加s表示匹配所有翎戍 ^ #表示行首 $ #表示行尾 /g #表示更改 /A #表示在行后 /I #表示在行前 \ n #表示换行 grep represents the filter. The-v parameter indicates the filter opposite value. p [Print] #表示打印 Sort #表示排序. The default is sorted by first character. -nr from large to small rows |
Example one:
[email protected] bash]# cat Test.txt #源文件 My name is Lingshu This is my first sciptis! 192.168.1.11 192.168.2.23 172.16.22.34 Hello Word [[Email protected] bash]# sed ' s/192.168/172.16/g ' test.txt #表示将所有的192.168 replaced by 172.16 My name is Lingshu This is my first sciptis! 172.16.1.11 172.16.2.23 172.16.22.34 Hello Word [Email protected] bash]# sed ' s/^/id/g ' test.txt #表示将行首替换为id空格. (empty lines are also added) ID My name is Lingshu Id ID This is my first sciptis! ID 192.168.1.11 ID 192.168.2.23 Id ID 172.16.22.34 ID Hello Word [[Email protected] bash]# sed ' s/$/id/g ' test.txt #表示将在行尾添加空格id (same blank line added) My name is Lingshu ID Id This is my first sciptis! Id 192.168.1.11 ID 192.168.2.23 ID Id 172.16.22.34 ID Hello Word ID [[Email protected] bash]# sed ' s/^/#/g ' test.txt #实用: All comments # My name is Lingshu # # This is my first sciptis! # 192.168.1.11 # 192.168.2.23 # # 172.16.22.34 # Hello Word [[Email protected] bash]# sed '/lingshu/a 333333333333 ' Test.txt #表示匹配到lingshu, then add a line after the line 333333333333333 My name is Lingshu 333333333333 This is my first sciptis! 192.168.1.11 192.168.2.23 172.16.22.34 Hello Word [[Email protected] bash]# sed '/lingshu/i 333333333333 ' Test.txt #表示匹配到lingshu, then add a line before the line of the Ling 333333333333333 333333333333 My name is Lingshu This is my first sciptis! 192.168.1.11 192.168.2.23 172.16.22.34 Hello Word [email protected] bash]# cat Test.txt My name is Lingshu This is my first sciptis! 192.168.1.11 192.168.2.23 172.16.22.34 Hello Word However, we find that there is nothing different from the source file. These are just cache generation, pre-modified. There is no real change. If you need a real modification, you need to add the-I parameter. |
Example two: The way of-n+p print output
[Email protected] bash]# sed-n '/lingshu/p ' test.txt #打印带有lingshu这一行 My name is Lingshu [Email protected] bash]# sed-n ' 1p ' test.txt #1p表示打印第一行 My name is Lingshu [[email protected] bash]# sed-n ' 1,4p ' test.txt #表示打印1-4 rows My name is Lingshu This is my first sciptis! 192.168.1.11 [Email protected] bash]# sed-n ' 1p;4p ' test.txt #分号表示打印第一行和第四行 My name is Lingshu 192.168.1.11 |
Example three: Finding the largest and smallest numbers in a file
[email protected] bash]# cat Number.txt #源文件 123 345 45 5 1324 12 4354 253 24 4253 131 24 [email protected] bash]# Cat Number.txt | Sed ' s//\n/g ' #首先将空格替换成换行. 123 345 45 5 1324 12 4354 253 24 4253 131 24 [email protected] bash]# Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" #表示筛选出空格 123 345 45 5 1324 12 4354 253 24 4253 131 24 [email protected] bash]# Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" | The sort sort indicates an arrangement. Default by First character row 12 123 131 1324 24 24 253 345 4253 4354 45 5 [email protected] bash]# Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" | Sort-nr #参数-nr from big to small rows 4354 4253 1324 345 253 131 123 45 24 24 12 5 [email protected] bash]# Cat Number.txt | Sed ' s//\n/g ' | Grep-v "^$" | Sort-nr |sed-n ' 1p; $p ' and then use SED to take the first and last line values. 4354 5 |
Integrated one: Grab the IP address of the NIC Ens33
[Email protected] bash]# ifconfig ens33 Ens33:flags=4163<up,broadcast,running,multicast> MTU 1500 inet 172.16.200.99 netmask 255.255.248.0 broadcast 172.16.207.255 Inet6 fe80::21cb:60a3:1caa:d4c9 Prefixlen ScopeID 0x20<link> Ether 00:0c:29:51:c5:f6 Txqueuelen (Ethernet) RX packets 1280778 Bytes 95144158 (90.7 MiB) RX Errors 0 dropped Notoginseng overruns 0 frame 0 TX Packets 2276 Bytes 141670 (138.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [Email protected] bash]# ifconfig ens33 |grep "netmask" #确定行 inet 172.16.200.99 netmask 255.255.248.0 broadcast 172.16.207.255 [[email protected] bash]# ifconfig ens33 |grep "netmask" |awk ' {print $} ' OK column 172.16.200.99 |
Case two: Crawl and directory usage rate do not%
[Email protected] bash]# df-h File system capacity has been used with available% mount points /dev/mapper/centos-root 30G 13G 18G 41%/ Devtmpfs 2.0G 0 2.0G 0%/dev Tmpfs 2.0G 0 2.0G 0%/dev/shm Tmpfs 2.0G 9.1M 2.0G 1%/run Tmpfs 2.0G 0 2.0G 0%/sys/fs/cgroup /dev/mapper/centos-home 5.8G 39M 5.8G 1%/Home /DEV/SDA1 197M 139M 59M 71%/boot Tmpfs 394M 36K 394M 1%/run/user/0 [Email protected] bash]# Df-h | grep "/$" #确定行 /dev/mapper/centos-root 30G 13G 18G 41%/ [Email protected] bash]# Df-h | grep "/$" | awk ' {print $} ' #确定列 41% [Email protected] bash]# Df-h | grep "/$" | awk ' {print $} ' |sed ' s/%//g ' #将% replaced with spaces #最终需要的数值. |
Four,
Shell
Four The Swordsman (
Find
)
Find File
Parameters: -maxdepth indicates the directory level -name means finding by file name -type represents the file type-F normal file-D directory -mtime according to modified time +30 30 days ago-30 means 30 days or less The-size represents a lookup by size. +20m files larger than 20M. |
Take command:-exec
Usage format:
-exec [command] {} \; After locating the file, follow the-exec, followed by the command {}: Finding the result, \; backslash-semicolon ending.
Example one: Find the file at the end of the current directory and move to the/document directory
[[email protected] bash]# find. -name "*.txt" ./number.txt ./test.txt ./ip.txt ./user.txt [[email protected] bash]# find. -name "*.txt"-exec mv {}/document/\; [Email protected] bash]# ls/document/ Bash ip.txt number.txt test test.txt user.txt YUM.REPO.D [[email protected] bash]# ls array.sh |
Example two: Delete Files 30 days before the current directory
[[email protected] bash]# find. -mtime +30-exec rm-rf {} \;
Xargs is similar to-exec;
The difference may be one for the directory one for the file
[[email protected] test]# find. -maxdepth 1-size +20m-exec cp {}/\; [[email protected] test]# find. -maxdepth 1-size +20m | Xargs cp {}/\; CP: Target "./mysql-5.7.13.tar.gz" is not a directory |
Shell Four Swordsman Primer