1. 程式人生 > >shell四劍客入門

shell四劍客入門

add 10.10 \n 文件系統 刪除 小寫字母 實用 echo p地址

一、 shell四劍客之(grep

grep 抓取行

正則表達式:

[0-9]表示字符匹配0-9

{1,3}表示匹配字符數量再3個以內。

例一:

[root@bogon 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

[root@bogon bash]# grep "^10" test.txt #表示以10開頭的行

10.28

[root@bogon bash]# grep "11$" test.txt #表示以11結尾的行

192.168.1.11

[root@bogon bash]# grep "[0-9]" test.txt #表示有匹配0-9字符的行

192.168.1.11

192.168.2.

10.28

172.16.22.34

[root@bogon bash]# grep "[0-9][0-9]" test.txt #表示匹配連續兩個0-9的字符所在的行

192.168.1.11

192.168.2.

10.28

172.16.22.34

[root@bogon bash]# grep "[0-9][0-9][0-9]" test.txt #便是匹配連續三個0-9的字符所在的行

192.168.1.11

192.168.2.

172.16.22.34

[root@bogon bash]# grep "[a-z]" test.txt #表示匹配所有小寫字母所在的行

my name is lingshu

MySQL install

hello word

[root@bogon bash]# grep "^[a-z]" test.txt #表示匹配以小寫字母開頭的行

my name is lingshu

hello word

[root@bogon bash]# grep "^[A-Z]" test.txt #表示匹配以大寫字母開頭的行

MySQL install

[root@bogon bash]# echo "168" >> !$

echo "168" >> test.txt

[root@bogon bash]# grep "168" test.txt

192.168.1.11

192.168.2.

168

[root@bogon bash]# grep "^168$" test.txt #以168開頭且結尾的行

168

[root@bogon bash]# cat test.txt | grep -E "[0-9]{1,3}" 連續一到三個數,並且是符合0-9

192.168.1.11

192.168.2.

10.28

172.16.22.34

168

[root@bogon bash]# cat test.txt | grep -E "[0-9]{1,3}\." #連續一到三個數,並且是符合0-9,且後跟小數點的數

192.168.1.11

192.168.2.

10.28

172.16.22.34

[root@bogon bash]# cat test.txt | grep -E "([0-9]{1,3}\.){3}" ##連續一到三個數,並且是符合0-9,且後跟小數點的數 ;這樣的數連續三次

192.168.1.11

192.168.2.

172.16.22.34

[root@bogon bash]# cat test.txt | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" #連續一到三個數,並且是符合0-9,且後跟小數點的數 ;這樣的數連續三次以後再加一個,一到三個0-9的數

192.168.1.11

172.16.22.34

[root@bogon bash]# grep -E "192|lingshu" test.txt #-E “參數1 |參數2” 表示匹配參數1或參數2的行。也可寫成egrep “192 | lingshu” test.txt

my name is lingshu

192.168.1.11

192.168.2.

二、 shell四劍客之(awk

awk抓取列

實例一:抓取IP

[root@bogon 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

[root@bogon bash]# awk ‘{print $2}‘ ip.txt #$2表示抓取第二列 $NF表示最後一列。

127.0.0.1

192.168.3.2

192.168.4.9

172.16.10.10

[root@bogon bash]# awk ‘{print "ip:"$2}‘ ip.txt #支持加註釋

ip:127.0.0.1

ip:192.168.3.2

ip:192.168.4.9

ip:172.16.10.10

例二:分割抓取用戶列表

[root@bogon 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

[root@bogon bash]# tail /etc/passwd | awk -F: ‘{print $1}‘ #-F:表示以冒號:做分割

setroubleshoot

pulse

gdm

gnome-initial-setup

sshd

avahi

postfix

ntp

tcpdump

lingshu

[root@bogon bash]# tail -n 2 /etc/passwd | awk -F: ‘{print $1}‘ > user.txt #抓去最後兩個並生成用戶列表

[root@bogon bash]# cat !$

cat user.txt

tcpdump

lingshu

三、 shell四劍客之(sed

sed更改字符

表示符:

/lingshu #表示匹配翎戍

s/lingshu #加s表示匹配所有翎戍

^ #表示行首

$ #表示行尾

/g #表示更改

/a #表示在行後

/i #表示在行前

\n #表示換行

grep 表示篩選。-v參數表示篩選相反值。

p [print] #表示打印

sort #表示排序。默認按首字符排序。-nr表示從大到小排

例一:

[root@bogon 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

[root@bogon bash]# sed ‘s/192.168/172.16/g‘ test.txt #表示將所有的192.168替換為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

[root@bogon bash]# sed ‘s/^/id /g‘ test.txt #表示將行首替換為id空格。(空行也添加)

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

[root@bogon bash]# sed ‘s/$/ id/g‘ test.txt #表示將在行尾添加空格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 id

[root@bogon bash]# sed ‘s/^/# /g‘ 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

[root@bogon bash]# sed ‘/lingshu/a 333333333333‘ test.txt #表示匹配到lingshu,然後在翎戍的行後添加一行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

[root@bogon bash]# sed ‘/lingshu/i 333333333333‘ test.txt #表示匹配到lingshu,然後在翎戍的行前添加一行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

[root@bogon 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

然而我們發現與源文件並沒有什麽不一樣。這些只是緩存生成,預修改。並沒有真正的更改。如果需要真正的修改,需要加-i 參數。

例二:-n+p 的方式打印輸出

[root@bogon bash]# sed -n ‘/lingshu/p‘ test.txt #打印帶有lingshu這一行

my name is lingshu

[root@bogon bash]# sed -n ‘1p‘ test.txt #1p表示打印第一行

my name is lingshu

[root@bogon bash]# sed -n ‘1,4p‘ test.txt #表示打印1-4行

my name is lingshu

this is my first sciptis!

192.168.1.11

[root@bogon bash]# sed -n ‘1p;4p‘ test.txt #分號表示打印第一行和第四行

my name is lingshu

192.168.1.11

例三:查找文件中最大和最小的數字

[root@bogon bash]# cat number.txt #源文件

123

345 45 5

1324

12 4354 253

24 4253

131 24

[root@bogon bash]# cat number.txt | sed ‘s/ /\n/g‘ #首先將空格替換成換行。

123

345

45

5

1324

12

4354

253

24

4253

131

24

[root@bogon bash]# cat number.txt | sed ‘s/ /\n/g‘ | grep -v "^$" #表示篩選出空格

123

345

45

5

1324

12

4354

253

24

4253

131

24

[root@bogon bash]# cat number.txt | sed ‘s/ /\n/g‘ | grep -v "^$" | sort sort表示排列。默認按首字符排

12

123

131

1324

24

24

253

345

4253

4354

45

5

[root@bogon bash]# cat number.txt | sed ‘s/ /\n/g‘ | grep -v "^$" | sort -nr #參數-nr表示從大到小排

4354

4253

1324

345

253

131

123

45

24

24

12

5

[root@bogon bash]# cat number.txt | sed ‘s/ /\n/g‘ | grep -v "^$" | sort -nr |sed -n ‘1p;$p‘排完再用sed取第一行和最後一行的值。

4354

5

綜合一:抓取網卡ens33的IP地址

[root@bogon 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 64 scopeid 0x20<link>

ether 00:0c:29:51:c5:f6 txqueuelen 1000 (Ethernet)

RX packets 1280778 bytes 95144158 (90.7 MiB)

RX errors 0 dropped 37 overruns 0 frame 0

TX packets 2276 bytes 141670 (138.3 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@bogon bash]# ifconfig ens33 |grep "netmask" #確定行

inet 172.16.200.99 netmask 255.255.248.0 broadcast 172.16.207.255

[root@bogon bash]# ifconfig ens33 |grep "netmask" |awk ‘{print $2}‘ 確定列

172.16.200.99

案例二:抓取跟目錄的使用率不要%

[root@bogon bash]# df -h

文件系統 容量 已用 可用 已用% 掛載點

/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

[root@bogon bash]# df -h | grep "/$" #確定行

/dev/mapper/centos-root 30G 13G 18G 41% /

[root@bogon bash]# df -h | grep "/$" | awk ‘{print $5}‘ #確定列

41%

[root@bogon bash]# df -h | grep "/$" | awk ‘{print $5}‘|sed ‘s/%//g‘ #將%替換成空格

41 #最終需要的數值。

四、 shell四劍客之(find

find查找文件

參數:

-maxdepth 表示目錄級別

-name 表示按文件名查找

-type 表示文件類型 -f普通文件 -d目錄

-mtime 按照修改時間 +30表示30天以前 -30表示30天以內

-size 表示按大小查找。+20M大於20M的文件。

承接命令:-exec

用法格式:

-exec [命令] {} \; 查找文件後跟-exec表示承接,後跟要操作的命令 {}:查找到的結果,\;反斜杠分號結尾。

例一:查找當前目錄下.txt結尾的文件並移到/document目錄下

[root@bogon bash]# find . -name "*.txt"

./number.txt

./test.txt

./ip.txt

./user.txt

[root@bogon bash]# find . -name "*.txt" -exec mv {} /document/ \;

[root@bogon bash]# ls /document/

bash ip.txt number.txt test test.txt user.txt yum.repo.d

[root@bogon bash]# ls

array.sh

例二:刪除當前目錄下30天以前的文件

[root@bogon bash]# find . -mtime +30 -exec rm -rf {} \;

xargs 與-exec類似;

區別,可能是一個用於目錄一個用於文件

[root@bogon test]# find . -maxdepth 1 -size +20M -exec cp {} / \;

[root@bogon test]# find . -maxdepth 1 -size +20M | xargs cp {} / \;

cp: 目標"./mysql-5.7.13.tar.gz" 不是目錄

shell四劍客入門