shell文本处理与变量计算

一. grep 命令:它是支持正则表达式的多用途文本搜索工具,也是linux中使用使用最广泛的命令之一,grep 的模式可以是字符串,变量,或正则表达式。一般格式为 grep 【选项】【模式】【文件】
 1.  grep 命令常用选项及意义。
    -i    忽略字母大小写
    -v    条件取反
    -c    只匹配行数
    -q    无任何输出,以退出状态表示是否搜索成功
     -r  递归搜索,同时搜索其子目录

    -n    显示匹配结果所在的行号

[root@foundation77 mnt]# grep -i  'kiosk' /etc/passwd  #匹配内容有kiosk的内容,忽略大小写
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash
[root@foundation77 mnt]# grep -c '172.25.254.250' /etc/hosts  # 在/etc/host/查找172.25.254.250 的行数
1

2.grep 命令族

linux 下有三种形式的grep ,三种形式如下

grep : 标准grep 命令,支持基本正则表达式,

egrep :扩展grep 命令,支持基本与扩展正则表达式,

fgrep : 快速grep 命令,不支持正则表达式,按字符串字面意思匹配。

 

3.基本元字符:

^  表示行首

$表示行尾

[root@foundation77 mnt]# egrep    '^root' /etc/passwd  # 匹配行首为root的行
root:x:0:0:root:/root:/bin/bash
[root@foundation77 mnt]# egrep    'bash$' /etc/passwd   # 匹配行尾为 bash 的行
root:x:0:0:root:/root:/bin/bash
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash

 .   过滤非空行  | 匹配一个字符
 

[root@foundation77 mnt]# egrep    'daemon.' /etc/passwd  #匹配一个含有daemon 的行
daemon:x:2:2:daemon:/sbin:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
libstoragemgmt:x:992:990:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin

[root@foundation77 mnt]# egrep    'daemon.account' /etc/passwd   # 匹配一个前一个字符为daemon 后面跟account 的行
libstoragemgmt:x:992:990:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
[root@foundation77 mnt]# 

 egrep -v '.' /etc/passwd    过滤空行
egrep  '^$' /etc/passwd    过滤空行
 +    至少匹配一词,也可以多次

?   允许匹配一次,也可以不匹配

*     可以匹配任意次,可以不匹配

[root@foundation77 mnt]# egrep  "o+" sos
root
kiosk
[root@foundation77 mnt]# egrep  "f?" sos
root
kiosk
student
zhang
wang
feng
[root@foundation77 mnt]# egrep  "kios(k)?" sos  #匹配kios后含有k 的,也可以没有
kiosk
[root@foundation77 mnt]# egrep  "f*" sos
root
kiosk
student
zhang
wang
feng


 

{}  限定多选的结构范围

[root@foundation77 mnt]# egrep  "(k){2,3}" sos  #匹配两个到三个k的行
kisok  kkisok 
kiosy kkk
kkkk
[root@foundation77 mnt]# egrep  "(k){4}" sos    #匹配4个 k的行
kkkk
[root@foundation77 mnt]# egrep  "(k){3,}" sos  #匹配含有3个以上k的行
kiosy kkk
kkkk

【】 匹配【】内任意一个字符

[root@foundation77 mnt]# egrep  "[ff]" sos
feng kkkk
feng
dfff
feng
[root@foundation77 mnt]# egrep  "[a-e]" sos
wang
feng kkkk
feng
abc
dfff
feng
[root@foundation77 mnt]# egrep  "(f)[e]" sos
feng kkkk
feng
feng

 

二. sed命令的用法

       sed 是一个非交互式文本编辑器,它可以对文本文件和标准输入进行编辑,一次处理一行内容,处理时,把当前的行存储在临时缓冲区(原始文件副本),处理完后,输送到屏幕,所以当需要保存改动内容时,需要重定向到另一个文件。

            sed [参数] '命令' file
   常用参数  -n  多点编辑

                     -i   可使用正则表达式

                      -r  将输入文件视为各个独立文件                                          

     字符处理         p     显示
                            d    删除
                             a    添加
                            c    替换
                             i    插入      

p 显示

[root@foundation77 mnt]# sed  -n '/etc/p' /etc/fstab  # 打印/etc/fstab 中有/etc的行
# /etc/fstab
[root@foundation77 mnt]# sed  -n '/UUID/p' /etc/fstab  #打印/etc/fstab 中含有UUID的行
UUID=26571035-5e83-4ec0-8a38-45c67957b9af /                       xfs     defaults        0 0
UUID=ad94710e-657b-4dfb-9c3d-7fd469703239 /boot                   xfs     defaults        0 0
UUID=000F-6C2B          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
UUID=5da4d9a9-04a5-4df8-b182-2c9146078b87 swap                    swap    defaults        0 0
[root@foundation77 mnt]# sed -n '2,5p' /etc/fstab    # 打印2-5 行
#
# /etc/fstab
# Created by anaconda on Wed Sep 26 23:29:38 2018
#

d 删除

[root@foundation77 mnt]# sed '/^UUID/d' /etc/fstab   #打印没有UUID的行

#
# /etc/fstab
# Created by anaconda on Wed Sep 26 23:29:38 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
[root@foundation77 mnt]# sed '/^$/d' /etc/fstab   #打印非空行
#
# /etc/fstab
# Created by anaconda on Wed Sep 26 23:29:38 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=26571035-5e83-4ec0-8a38-45c67957b9af /                       xfs     defaults        0 0
UUID=ad94710e-657b-4dfb-9c3d-7fd469703239 /boot                   xfs     defaults        0 0
UUID=000F-6C2B          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
UUID=5da4d9a9-04a5-4df8-b182-2c9146078b87 swap                    swap    defaults        0 0

[root@foundation77 mnt]# sed '1,8d' /etc/fstab      #不打印1-8 行
UUID=26571035-5e83-4ec0-8a38-45c67957b9af /                       xfs     defaults        0 0
UUID=ad94710e-657b-4dfb-9c3d-7fd469703239 /boot                   xfs     defaults        0 0
UUID=000F-6C2B          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
UUID=5da4d9a9-04a5-4df8-b182-2c9146078b87 swap                    swap    defaults        0 0

a 添加

[root@foundation77 mnt]# sed '/0345/a\westos' cos  #添加westos 到0345 行的后一行
12312;12421:12443
0345:5234:2343
westos
5436:6546:6567
7543:7523:9
7238:333:135
[root@foundation77 mnt]# sed '/7238/awestos' cos   #添加westos到7238 行的后一行
12312;12421:12443
0345:5234:2343
5436:6546:6567
7543:7523:9
7238:333:1355
westos
[root@foundation77 mnt]# sed '4a\westos' cos   在第四行后下一行添加westos
12312;12421:12443
0345:5234:2343
5436:6546:6567
7543:7523:9
westos
7238:333:1355

c  替换

[root@foundation77 mnt]# sed '/2/c\数字' cos    #将带有2的行,替换为“数字”
数字
数字
5436:6546:6567
数字
数字



[root@foundation77 mnt]# sed 's/0345/数字/g' cos  将0345 替换为“数字"
12312;12421:12443
数字:5234:2343
5436:6546:6567
7543:7523:9
7238:333:1355

i 插入

[root@foundation77 mnt]# sed '/6462/isos' sos  # 在6462行首插入
sos
6462:2342:4374
8213:4782:3489
7687:3893:1898
1289:5898:4938
3427:4732:4837
6478:7968:7438
6478:7968:7438
8213:4782:3489

-I  改变源文件内容

[root@foundation77 mnt]# sed -i 's/4/h/' sos  
[root@foundation77 mnt]# cat sos
6h62:2342:4374
8213:h782:3489
7687:3893:1898
1289:5898:h938
3h27:4732:4837
6h78:7968:7438
6h78:7968:7438
8213:h782:3489

三. awk 工具

awk处理机制:根据模式一次从文件中抽取一行文本,对这行文本执行指定操作。(默认使用空白字符作为分隔符)

记录字段:一词记录一条字段储存在$0中,并将一条字段分割成为 $1  $2  $3   ......

[root@foundation77 mnt]# cat test
linux redhat root student
[root@foundation77 mnt]# awk '{print $0}' test
linux redhat root student
[root@foundation77 mnt]# awk '{print $1}' test
linux
[root@foundation77 mnt]# awk '{print $4}' test
student
[root@foundation77 mnt]# awk '{print $3,$4}' test
root student


指定分隔符

[root@foundation77 mnt]# awk -F ":" '{print $1,$3}' /etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6

常用变量

   NR 当前记录编号

    NF 记录字段个数

[root@foundation77 mnt]# awk '{print FILENAME,NR}' sos  #输出文件名和编号
sos 1
sos 2
sos 3
sos 4
sos 5
sos 6
sos 7
sos 8
[root@foundation77 mnt]# awk '{print FILENAME,NR,NF}' sos   # 输出文件名,编号和字段个数
sos 1 1 
sos 2 1
sos 3 1
sos 4 1
sos 5 1
sos 6 1
sos 7 1
sos 8 1


BEGIN{}:读入第一行文本之前执行的语句,一般用来初始化操作
END{}:处理完最后以行文本后执行,一般用来处理输出结果

{}:逐行处理

常见用法:

[root@foundation77 mnt]# awk -F: 'BEGIN{print "数字"} {print NR;print} END {print "westos"}' sos   #sos文件首加数字,文件中每一行前加行数,末尾加westos
数字
1
6h62:2342:4374
2
8213:h782:3489
3
7687:3893:1898
4
1289:5898:h938
5
3h27:4732:4837
6
6h78:7968:7438
7
6h78:7968:7438
8
1909?1230?4985
westos
[root@foundation77 mnt]# awk -F: '/bash$/{print}' /etc/passwd   #输出以bash结尾的行
root:x:0:0:root:/root:/bin/bash
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash

[root@foundation77 mnt]# awk -F: 'NR % 2 == 0  {print NR;prin }{print}' sos ##s输出偶数行
6h62:2342:4374
2
8213:h782:3489
7687:3893:1898
4
1289:5898:h938
3h27:4732:4837
6
6h78:7968:7438
6h78:7968:7438
8
1909?1230?4985
[root@foundation77 mnt]# awk -F: 'NR % 2 == 1  {print NR;prin }{print}' sos ##s输出奇数行
1
6h62:2342:4374
8213:h782:3489
3
7687:3893:1898
1289:5898:h938
5
3h27:4732:4837
6h78:7968:7438
7
6h78:7968:7438
1909?1230?4985

[root@foundation77 mnt]# awk -F: 'NR >=3 && NR <=5 {print }' sos.sh   #显示sos.sh 3-5行
 do
     echo -e "
  \033[32m status  显示httpd运行状态 

[root@foundation77 mnt]# awk 'BEGIN{i=0}{i+=NF}END{print i}' sos  #总字段个数
8

列出uid > 1000 的用户

[root@foundation77 mnt]# awk -F: '$3 > 1000 {print $1,$3}' /etc/passwd
nfsnobody 65534
yao 1001

列出ip值

[root@foundation77 mnt]# ifconfig br0 | grep "inet " | awk '{print $2}'
172.25.254.77

 

四.cut 命令

cut 命令用于从标准输入或文本文件中按域或行提取文件,基本格式为 cut 【选项】【文件】,其选新仅有三个。

-d  指定分隔符

-c 指定提取的字符数或范围

-f 提取的域或域范围

[root@foundation77 mnt]# cut -c 1 /etc/passwd  #提取/etc/passwd文件中每行的第一个字符
r
b
d
a
l
s
s
h
m
o
g
f
n
......

[root@foundation77 mnt]# cut -c 1,3 /etc/passwd  #提取每行第一和第三个字符
ro
bn
de
am
l:
sn
su
hl
mi
oe
gm
fp
......

[root@foundation77 mnt]# cut -d : -f 1-2 /etc/passwd  # 提取分隔符为: 的行,显示1-2 列
root:x
bin:x
daemon:x
adm:x
lp:x
sync:x
shutdown:x
halt:x
mail:x

获取ip

[root@foundation77 mnt]# ifconfig br0 | grep "inet " | cut -d " "  -f 10
172.25.254.77

五 sort 命令

sort 命令是一类使用广泛的文件排序工具,是shell 脚本编程常用工具之一,基本格式为 sort 【选项】【文件】

 选项  -c  监测文件是否已经排序

            -n  按数字大小排序

            -r   将排序结果逆向显示

            -u   去掉重复行

             -k  指定排序的列

             -o   输出到指定文件

              -t  指定分隔符

              -m  合并两个已排序文件

[root@foundation77 mnt]# sort sos
123
123
124
23532
324
34546
35
432
5
656
7547655
[root@foundation77 mnt]# sort -n sos
5
35
123
123
124
324
432
656
23532
34546
7547655
[root@foundation77 mnt]# sort -u sos
123
124
23532
324
34546
35
432
5
656
7547655

[root@foundation77 mnt]#  sort -t : -k 2  sos   #根据第二列对sos 排序,指定分隔符为: 

6462:2342:4374
7687:3893:1898
7687:3893:1898
3427:4732:4837
3427:4732:4837
8213:4782:3489
8213:4782:3489
1289:5898:4938
1289:5898:4938
6478:7968:7438
 
[root@foundation77 mnt]# sort -rt : -k 3  cos  #根据第三列倒叙显示排序
7543:7523:9
7238:333:1355
5436:6546:6567
12312;12421:12443
0345:5234:2343


[root@foundation77 mnt]# cat sos                 
6462:2342:4374
8213:4782:3489
7687:3893:1898
1289:5898:4938
3427:4732:4837
6478:7968:7438
8213:4782:3489
7687:3893:1898
1289:5898:4938
3427:4732:4837
[root@foundation77 mnt]# sort -k2 sos -o passwd   # 将sos中的第二列排序后导入passwd
[root@foundation77 mnt]# cat passwd 
1289:5898:4938
1289:5898:4938
3427:4732:4837
3427:4732:4837
6462:2342:4374
6478:7968:7438
7687:3893:1898
7687:3893:1898
8213:4782:3489
8213:4782:3489

六. uniq 命令: 用于除去文本文件中的重复行,类似于 sort -u 。

命令选项  -u 显示没有重复的行

                   -c  每行显示一次,并统计次数

                   -d   只显示重复的行

[root@foundation77 mnt]# uniq -c sos    #显示所有并统计次数
      1 6462:2342:4374
      1 8213:4782:3489
      1 7687:3893:1898
      1 1289:5898:4938
      1 3427:4732:4837
      2 6478:7968:7438
      1 8213:4782:3489
      1 7687:3893:1898
      2 1289:5898:4938
      1 3427:4732:4837
[root@foundation77 mnt]# uniq -d sos   #只显示重复行
6478:7968:7438
1289:5898:4938
[root@foundation77 mnt]# uniq -u sos    #只显示不重复行
6462:2342:4374
8213:4782:3489
7687:3893:1898
1289:5898:4938
3427:4732:4837
8213:4782:3489
7687:3893:1898
3427:4732:4837

[root@foundation77 mnt]# sort -n sos | uniq -d   #排序并显示重复行
1289:5898:4938
3427:4732:4837
6478:7968:7438
7687:3893:1898
8213:4782:3489

七.变量的计算

整数运算

1.expr 命令

通常用于整数计算

[root@foundation77 mnt]# a=666
[root@foundation77 mnt]# expr $a
666
[root@foundation77 mnt]# expr $a + 333
999
[root@foundation77 mnt]# expr $a - 333
333
[root@foundation77 mnt]# expr $a \* 333  # 乘法要加\
221778
[root@foundation77 mnt]# expr $a / 333
2
[root@foundation77 mnt]# expr $a % 333 
0

2 $[] 和 (())

[root@foundation77 mnt]# a=9
[root@foundation77 mnt]#  echo $[a+10]
19
[root@foundation77 mnt]#  echo $[a-10]
-1
[root@foundation77 mnt]#  echo $[a*10]
90
[root@foundation77 mnt]#  echo $[a/10]
0
[root@foundation77 mnt]#  echo $[a%10]
9
[root@foundation77 mnt]#  echo $((a%10))
9
[root@foundation77 mnt]#  echo $((a+10))
19

3.let (执行后保存新值)

[root@foundation77 mnt]# a=9
[root@foundation77 mnt]# let a+=10
[root@foundation77 mnt]# echo $a
19
[root@foundation77 mnt]# let a+=10
[root@foundation77 mnt]# echo $a
29
[root@foundation77 mnt]# let a*=10
[root@foundation77 mnt]# echo $a
290
[root@foundation77 mnt]# let a/=10
[root@foundation77 mnt]# echo $a
29
[root@foundation77 mnt]# let a%=10
[root@foundation77 mnt]# echo $a
9
[root@foundation77 mnt]#  echo $[a**10]
3486784401

小数运算 bc

  scale 后加的是保留的位数

[root@foundation77 mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[root@foundation77 mnt]# echo "scale=1.2+1.2" | bc
[root@foundation77 mnt]# echo "s=1.2+1.2" | bc
[root@foundation77 mnt]# echo "scale=2;1.23*4.56" | bc
5.60
[root@foundation77 mnt]# echo "scale=4;1.2222-0.2" | bc
1.0222
[root@foundation77 mnt]# echo "scale=8;10/3" | bc
3.33333333

编辑一个脚本计算整数的 加减乘除

[root@foundation77 mnt]# cat test.sh 
#!/bin/bash 
read -t 5 -p "请输入两个整数:" a b

echo "a+b=$[a+b]"
echo "a-b=$[a-b]"
echo "a*b=$[a*b]"
echo "a/b=$[a/b]"
echo "a**b=$[a**b]"

[root@foundation77 mnt]# sh test.sh 
请输入两个整数:6 8
a+b=14
a-b=-2
a*b=48
a/b=0
a**b=1679616
[root@foundation77 mnt]# 

teset 命令

[ "$a" = "$b" ]            等于
[ "$a" != "$b" ]           不等于
[ "$a" -eq "$b" ]         等于
[ "$a" -ne "$b" ]         不等于
[ "$a" -le "$b" ]          小于等于
[ "$a" -ge "$b" ]        大于等于
[ "$a" -gt "$b" ]         大于
[ "$a" -lt "$b" ]           小于
[ "$a" -ne "$b" -a "$a" -gt "$b" ]    -a必须条件都满足
[ "$a" -ne "$b" -o"$a" -gt "$b" ]     -a条件至少满足一个
[ -z "$a" ]        是否为空
[ -e "file" ]       是否存在
[ -f "file" ]        普通文件
[ -b "file" ]        块设备
[ -S "file" ]       套接字
[ -c "file" ]        字符设备
[ -L "file" ]        软链接

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值