Shell编程--shell编程一

基础正则表达式  |  字符截取命令  |   字符处理命令  

1、正则表达式与通配符

正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配。grep、awk、sed等命令可以支持正则表达式

通配符用来匹配符合条件的文件名的,通配符是完全匹配。ls、find、cp这些命令不支持正则表达式,所以只能使用shell自己的通配符来进行匹配了。

2、基础正则表达式

元字符 作用
* 前一个字符匹配0次或任意次数
. 匹配除了换行外任意一个字符
^ 匹配行首
$ 匹配行尾
[]  
[^] 匹配不是中括号内容
\ 转义符
\{n\} 匹配前面字符n次
\{n,\} 匹配前面字符最少n次
\{n,m\} 匹配前面字符最低n次,最高m次

#################################################
字符截取命令

cut字段提取命令

[root@localhost ~]# cut [选项] 文件名
选项:
-f 列号           提取第几列
-d 分隔符         按照指定分隔符分隔列


(中间是制表符,不是空格)
[root@localhost ~]# vi student.txt
ID		Name	gender		Mark
1		Liming	M			86
2		Sc		M			90
3		Gao		M			83

提取第二列

[root@localhost ~]# cut -f 2 student.txt
Name
Liming
Sc
Gao

####指定用:作为分隔符,提取第1、3列

[root@localhost ~]# cut -d ":" -f 1,3 /etc/passwd
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
libstoragemgmt:998
abrt:173
rpc:32
sshd:74
postfix:89
ntp:38
chrony:997
tcpdump:72
hc:1000
[root@localhost ~]# cat /etc/passwd | grep '/bin/bash'
root:x:0:0:root:/root:/bin/bash
hc:x:1000:1000:hc:/home/hc:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
user4:x:1004:1004::/home/user4:/bin/bash

####提取出来不包含root的行

[root@localhost ~]# cat /etc/passwd | grep '/bin/bash' | grep -v root
hc:x:1000:1000:hc:/home/hc:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
user4:x:1004:1004::/home/user4:/bin/bash

提取除/etc/passwd的第一列

[root@localhost ~]# cat /etc/passwd | grep '/bin/bash' | grep -v root | cut -d ':' -f 1
hc
user1
user2
user3
user4

-------------------------------------------------
printf命令

printf '输出类型输出格式' 输出内容
输出类型:
   %ns:            输出字符串。n是数字指代输出几个字符
   %ni:             输出整数。n是数字指代输出几个数字
   %m.nf:         输出浮点数。m和n是数字,指代输出的整数位数和小数位数。如%8.2f代表共输出8位数,其中2位是小数,6位是                         整数

---------------------------------------------------------------------------------------------------------------------------
awk命令

[root@localhost ~]# awk '条件1{动作1} 条件2{动作2}...' 文件名
条件(Pattern):
    一般使用关系表达式作为条件
    x>10        判断变量x是否大于10
    x>=10       大于等于
    x<=10        小于等于
动作(Action)
    格式化输出
    流程控制语句

(中间是制表符,不是空格)
[root@localhost ~]# vi student.txt
ID		Name	gender		Mark
1		Liming	M			86
2		Sc		M			90
3		Gao		M			83

提取除第二列和第四列 用\t  、\n格式化

[root@localhost ~]# awk '{printf $2 "\t" $4 "\n"}' student.txt
Name    Mark
Liming  86
Sc      90
Gao     83

####用cut是没有办法输出的,用awk可以输出

[root@localhost ~]# df -h | awk '{printf $1 "\t" $5 "\t" $6 "\n"}' 
Filesystem      Use%    Mounted
/dev/sda2       3%      /
devtmpfs        0%      /dev
tmpfs   0%      /dev/shm
tmpfs   2%      /run
tmpfs   0%      /sys/fs/cgroup
/dev/sda5       1%      /data
/dev/sda1       25%     /boot
tmpfs   0%      /run/user/0

####提取sda2硬盘使用率

[root@localhost ~]# df -h | grep 'sda2' | awk '{print $5}' | cut -d "%" -f 1
3

BEGIN:再执行命令之前,进行执行的动作

[root@localhost ~]# awk 'BEGIN{print "test!!!"} {print $2 "\t" $4}' student.txt     
test!!!
Name    Mark
Liming  86
Sc      90
Gao     83


FS内置变量(awk分隔符),加BEGIN的作用是在执行命令之前,先执行分隔符操作,因为awk默认是先读入数据再执行,所以会第一列没有执行awk

[root@localhost ~]# awk '{FS=":"} {print $1 "\t" $3}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin     1
daemon  2
adm     3
lp      4
sync    5
shutdown        6
halt    7
mail    8
operator        11
games   12
[root@localhost ~]# awk 'BEGIN{FS=":"}  {print $1 "\t" $3}' /etc/passwd     
root    0
bin     1
daemon  2
adm     3
lp      4
sync    5
shutdown        6
halt    7
mail    8
operator        11
games   12

END:再命令执行完成后,再执行的动作

[root@localhost ~]# awk 'BEGIN{print "test!!!"} {print $2 "\t" $4}' student.txt     
Name    Mark
Liming  86
Sc      90
Gao     83
test!!!

关系运算符(读取student,不要标题列,然后筛选出Mark大于85的人的Name)

[root@localhost ~]# cat student.txt | grep -v Name | awk '$4>=85 {print $2}'
Liming
Sc

-------------------------------------------------------------------------------
sed命令

sed是一种几乎包括在所有UNIX平台(包含Linux)的轻量级流编辑器。sed主要是用来将数进行选取、替换、删除、新增的命令

[root@localhost ~]# sed [选项] '[动作]' 文件名
选项:
   -n:   一般sed命令胡吧所有数据都输出到屏幕, 如果加入此选项,则只会把经过sed命令处理的行输出到屏幕
   -e:      允许对输入数据应用多条sed命令编辑
   -i:      用sed的修改结果直接修改读取数据的文件,而不是由屏幕输出
动作:
     a \:    追加,在当前行后添加一行或多行。添加多行时,除最后一行外,每行末尾需要用"\"代表数据未完结。
     c \:    行替换,用c后面的字符串替换原数据行,替换多行时,除最后一行外,每行末尾需用"\"代表数据未完结。
     i \:    插入,在当前行前插入一行或多行。插入多行时,除最后一行外,每行末尾需用"\"代表数据未完结。
     d:      删除,删除指定的行
     p:      打印,输出指定的行
     s:      字符串替换,用一个字符串替换另一个字符串。格式为"行范围s/旧字串/新字串/g"

示例:

#输出文件第二行,p动作一般都加-n使用


[root@localhost ~]# sed '2p' student.txt
ID      Name    gender  Mark
1       Liming  M       86
1       Liming  M       86
2       Sc      M       90
3       Gao     M       83
[root@localhost ~]# sed -n '2p' student.txt
1       Liming  M       86

###删除第二行到第四行

[root@localhost ~]# sed '2,4d' student.txt
ID      Name    gender  Mark
[root@localhost ~]# sed '3,4d' student.txt
ID      Name    gender  Mark
1       Liming  M       86

###在第二行后追加hello

[root@localhost ~]# sed '2a hello' student.txt
ID      Name    gender  Mark
1       Liming  M       86
hello
2       Sc      M       90
3       Gao     M       83

###在第二行前插入hello

[root@localhost ~]# sed '2i hello world' student.txt
ID      Name    gender  Mark
hello world
1       Liming  M       86
2       Sc      M       90
3       Gao     M       83

###第二行数据替换成No such person

[root@localhost ~]# sed '2c No such person' student.txt 
ID      Name    gender  Mark
No such person
2       Sc      M       90
3       Gao     M       83

###字符串替换(把第四行83替换成55)

[root@localhost ~]# sed '4s/83/55/g' student.txt
ID      Name    gender  Mark
1       Liming  M       86
2       Sc      M       90
3       Gao     M       55

###永久生效,写入到文本中,会修改文件

[root@localhost ~]# sed -i '4s/83/55/g' student.txt
[root@localhost ~]# cat student.txt 
ID      Name    gender  Mark
1       Liming  M       86
2       Sc      M       90
3       Gao     M       55

###同时执行多条动作,同时把'Liming'和'Gao'替换成空,s前没有数字表示整篇文档

[root@localhost ~]# sed -e 's/Liming//g;s/Gao//g' student.txt
ID      Name    gender  Mark
1               M       86
2       Sc      M       90
3               M       55

猜你喜欢

转载自blog.csdn.net/qq_28710983/article/details/81676713