[shell] cut、awk、sed命令

字符串截取替换的一个例子:

#!/bin/bash
thisIP=$(grep 'serverIP=' swsys.conf | cut -f 2 -d "=")
#thisIP=`grep 'serverIP=' swsys.conf | cut -f 2 -d "="`
#echo $thisIP
sed -i "6s/clientIP=/clientIP=$thisIP/g" iplist.conf

thisIP=$(grep 'serverIP=' swsys.conf | cut -f 2 -d "=")     
# grep命令从配置文件 swsys.conf 中找到包含serverIP的字段, 即severIP=x.x.x.x ; 传给cut命令,以"="分隔截取第2个字符,即一个IP ; 然后把这个IP存到变量thisIP中 

sed -i "6s/clientIP=/clientIP=$thisIP/g" iplist.confserverIP=x.x.x.x   
# 把thisIP中存的值添加到配置文件 iplist.conf 中的参数clientIP,这里的方法是替换第6行的"clientIP="为"clientIP=$thisIP" 
# 注意这里sed -i 后因为要引用到变量所以用双引号括起来,而非使用单引号

1.cut

命令格式:
cut [选项] 文件名
选项:
-f 列号,多列用逗号分隔
-d 分隔符

例子:
grep "/bin/bash" /etc/passwd | grep -v "root" |cut -f 1 -d ":"    #截取普通用户名,在第1列

cut -f 1,3 -d ":" /etc/passwd    #截取用户名和UID,即第1列和第3列

说明:1)grep是行提取命令,cut是列提取命令  2)cut适合截取规律文档,复杂则使用awk

 

2.awk

预备知识:标准输出命令
******************************
命令格式:
printf  '输出格式'  输出内容

其中输出格式中要指定输出类型:
%ns: 输出字符串、n个字符
%ni:输出整数、n个数字
%m.nf:输出浮点数、%9.3代表共输出9位数,其中3位是小数/6位是整数

例1:

[root@xxx ~]# printf %s 1 2 3 4 5 6
123456[root@xxx ~]#

[root@xxx ~]# printf '%s' 1 2 3 4 5 6
123456[root@xxx ~]#

[root@xxx ~]# printf '%s %s %s' 1 2 3 4 5 6
1 2 34 5 6[root@xxx ~]#

[root@xxx ~]# printf '%s\t%s\t%s\n' 1 2 3 4 5 6
1       2       3
4       5       6
[root@xxx ~]#

printf '%s %s %s' 1 2 3 4 5 6   #输出:1 2 34 5 6 ,输出3个,再输出3个

例2:
student104.txt文件内容如下(哈哈,阿尔敏和尤弥尔的排名数据没找到)

注意:每列之间是制表符分隔,直接复制可能会变成空格,可以在Linux环境用Tab键编辑一个。

[root@xxx shell]# cat student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
2       Mikasa  F       10      9       8       6       No.1
3       Armin   M       2       6       10      8       No.?
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10

[root@xxx shell]# printf  '%s'  $(cat student104.txt)
  ID Name gender Combat Proact Brain Coordi Rank 1 Eren M 9 10 7 5 No.5 2 Mikasa F 10 9 8 6 No.1 3 Armin M 2 6 10 8 No.? 4 Jean M 9 6 5 5 No.6 5 Connie M 6 7 3 7 No.8 6 Sasha F 6 3 5 6 No.9 7 Ymir F 7 10 7 2 No.? 8 Reiner M 9 7 7 10 No.2 9 Bertolt M 9 4 6 9 No.3 10 Annie F 10 7 7 3 No.4 11 Christa F 6 6 4 8 No.10[root@xxx shell]#
[root@xxx shell]#
[root@xxx shell]# printf  '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n'  $(cat student104.txt)
  ID    Name    gender  Combat  Proact  Brain   Coordi  Rank
 1      Eren    M       9       10      7       5       No.5
 2      Mikasa  F       10      9       8       6       No.1
 3      Armin   M       2       6       10      8       No.?
 4      Jean    M       9       6       5       5       No.6
 5      Connie  M       6       7       3       7       No.8
 6      Sasha   F       6       3       5       6       No.9
 7      Ymir    F       7       10      7       2       No.?
 8      Reiner  M       9       7       7       10      No.2
 9      Bertolt M       9       4       6       9       No.3
 10     Annie   F       10      7       7       3       No.4
 11     Christa F       6       6       4       8       No.10

printf  '%s'  $(cat student104.txt)    #不调整输出格式,连在一起打印

printf  '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n'  $(cat student104.txt)    #调整格式输出(格式要根据文件而定)

print命令和printf都可以给awk用,print会在每个输出后自动加换行符
注意:系统中没有print命令。
******************************

awk '条件1{动作1} 条件2{动作2}…' 文件名

条件1满足则执行,条件2满足则执行……

注意:awk是首先读取第一行数据、然后判断条件,直到读完所有行

例1(无条件运行):

[root@xxx shell]# awk '{printf $2 "\t" $4 "\n"}' student104.txt
Name    Combat
Eren    9
Mikasa  10
Armin   2
Jean    9
Connie  6
Sasha   6
Ymir    7
Reiner  9
Bertolt 9
Annie   10
Christa 6
[root@xxx shell]#

awk '{printf $2 "\t" $4 "\n"}' student104.txt   #提取第2个字段和第4个字段,注意打印内容中的转义字符加双引号

awk '{print $2 "\t" $4 }' student104.txt     #与上一条命令输出一样

 

例2:磁盘空间信息截取,例如截取某个FileSystem的使用率数字部分

[root@xxx ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_rhel7-lv_root
                       50G   11G   37G  22% /
tmpfs                  13G   76K   13G   1% /dev/shm
/dev/sda1             477M   42M  410M  10% /boot
/dev/mapper/vg_rhel7-lv_home
                      431G   71M  409G   1% /home

[root@xxx ~]#  df -h | grep "/dev/sda1"
/dev/sda1             477M   42M  410M  10% /boot
[root@xxx ~]#
[root@xxx ~]# df -h | grep "/dev/sda1" | awk '{print $5}'
10%
[root@xxx ~]#
[root@xxx ~]# df -h | grep "/dev/sda1" | awk '{print $5}' | cut -d "%" -f 1
10

但是观察 df -h 的结果,有几行的FileSystem的 名字太长,剩下的内容就被挤到了第二行,这样使用grep是得不到一条完整记录的。可以使用 df 的 -P 选项调整格式,

-P, --portability     use the POSIX output format

[root@xxx ~]# df -h | grep root
/dev/mapper/vg_rhel7-lv_root
[root@xxx ~]#
[root@xxx ~]# df -h | grep root | awk '{print $5}'

[root@xxx ~]# 


[root@xxx ~]# df -hP
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/vg_rhel7-lv_root   50G   11G   37G  23% /
tmpfs                          13G  168K   13G   1% /dev/shm
/dev/sda1                     477M   42M  410M  10% /boot
/dev/mapper/vg_rhel7-lv_home  431G   71M  409G   1% /home
[root@xxx ~]# 


[root@xxx ~]# df -hP | grep root
/dev/mapper/vg_rhel7-lv_root   50G   11G   37G  23% /
[root@xxx ~]#
[root@xxx ~]# df -hP | grep root | awk '{print $5}'
23%
[root@xxx ~]#

 

BEGIN 和 END

例子:

[root@xxx shell]# awk 'BEGIN {printf "The Rank: \n"} {printf $2 "\t" $8 " \n"}' student104.txt
The Rank:
Name    Rank
Eren    No.5
Mikasa  No.1
Armin   No.?
Jean    No.6
Connie  No.8
Sasha   No.9
Ymir    No.?
Reiner  No.2
Bertolt No.3
Annie   No.4
Christa No.10
[root@xxx shell]# awk 'END{printf "The End \n"} {printf $2 "\t" $8 " \n"}' student104.txt
Name    Rank
Eren    No.5
Mikasa  No.1
Armin   No.?
Jean    No.6
Connie  No.8
Sasha   No.9
Ymir    No.?
Reiner  No.2
Bertolt No.3
Annie   No.4
Christa No.10
The End
[root@xxx shell]

awk 'BEGIN {printf "The Rank: \n"} {printf $2 "\t" $8 " \n"}' student104.txt   #开头打印一行 "The Rank:",BEGIN后可无空格

awk 'END{printf "The End \n"} {printf $2 "\t" $4 " \n"}' student104.txt    #结尾打印一行 "The End",END后可有空格

 

FS内置变量

awk默认用空格或制表符做分隔符,可以使用FS设置分隔符

[root@xxx ~]# cat /etc/passwd | grep "/bin/bash" | \
> awk 'BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}'
root    0
mysql   27
sftpuser        500
[root@xxx ~]#
[root@xxx ~]# cat /etc/passwd | grep "/bin/bash" | awk '{FS=":"} {printf $1 "\t" $3 "\n"}'
root:x:0:0:root:/root:/bin/bash
mysql   27
sftpuser        500
[root@xxx ~]#

# cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}'  

# cat /etc/passwd | grep "/bin/bash" | awk '{FS=":"} {printf $1 "\t" $3 "\n"}'    #与前者比较,没有"BEGIN",第一行没操作

说明:换行但不执行命令可以使用"\",比如一行命令太长可以这样写

 

关系运算符

例子:筛选出Combat值(第4列)大于8的士兵

[root@xxx shell]# cat student104.txt | grep -v Name | awk '$4>=8{print $2 "\n"}'
Eren

Mikasa

Jean

Reiner

Bertolt

Annie

[root@xxx shell]#

cat student104.txt | grep -v Name | awk '$4>=8{print $2 "\n"}'   # 用grep -v Name先排除表头

 

3.sed

sed是字符串替换命令

命令格式:
sed [选项] '[动作]' 文件名

选项:
-n:一般sed命令会把所有数据都输出到屏幕,如果加入该选项只会输出经过sed命令处理的行。
-e:允许对输入数据应用多条sed命令编辑
-i:用sed的修改结果直接修改读取数据的文件,而不是由屏幕输出

动作:

a: 追加,在当前行后添加一行或多行
c: 行替换,用c后面的字符串替换原数据行
i: 插入,在当前行插入一行或多行。
d: 删除指定行
p: 打印输出指定行
s: 字符串替换

例1(打印、删除):

[root@xxx shell]# sed -n '2p' student104.txt
1       Eren    M       9       10      7       5       No.5
[root@xxx shell]# sed -n '2,4p' student104.txt
1       Eren    M       9       10      7       5       No.5
2       Mikasa  F       10      9       8       6       No.1
3       Armin   M       2       6       10      8       No.?
[root@xxx shell]#
[root@xxx shell]# sed '3d' student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
3       Armin   M       2       6       10      8       No.?
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]# sed '3,5d' student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]#

sed -n '2p' student104.txt        #打印第2行

sed -n '2,4p' student104.txt     #注意逗号分隔表示范围,打印第2到4行

sed '3d'student104.txt             #删除第3行,注意没带 "-i" 不会保存到文件,只是打印在屏幕上

sed '3,5d'student104.txt          #删除第3到第5行

 

例2(追加、插入、替换行):

[root@xxx shell]# sed '2a Append line after line2.' student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
Append line after line2.
2       Mikasa  F       10      9       8       6       No.1
3       Armin   M       2       6       10      8       No.?
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]#
[root@xxx shell]# sed '2i Insert line before line2' student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
Insert line before line2
1       Eren    M       9       10      7       5       No.5
2       Mikasa  F       10      9       8       6       No.1
3       Armin   M       2       6       10      8       No.?
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]#

[root@xxx shell]# sed '4c Line 4 is hided.' student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
2       Mikasa  F       10      9       8       6       No.1
Line 4 is hided.
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]#

sed '2a someline' student104.txt     #在第2行之后追加一行

sed '2i someline' student104.txt      #在第2行之前插入一行

sed '4c new content' student104.txt     #替换第4行内容

注意:以上没加-i,所以并没有修改文件本身

 

例3(字符串替换):

sed 's/旧字符串/新字符串/g' 文件名

[root@xxx shell]# sed '3s/10/100/g' student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
2       Mikasa  F       100     9       8       6       No.1
3       Armin   M       2       6       10      8       No.?
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]#
[root@xxx shell]# sed -i '3s/10/100/g' student104.txt
[root@xxx shell]#
[root@xxx shell]# cat student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
2       Mikasa  F       100     9       8       6       No.1
3       Armin   M       2       6       10      8       No.?
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7       Ymir    F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9       Bertolt M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]#
[root@xxx shell]# sed -i '3s/100/10/g' student104.txt

sed '3s/10/100/g' student104.txt      #在第3行中,把10换成100

sed -i '3s/10/100/g' student104.txt     #sed -i 操作的数据直接写入文件

还可以批量操作

[root@xxx shell]# sed -e 's/Ymir//g;s/Bertolt//g;' student104.txt
ID      Name    gender  Combat  Proact  Brain   Coordi  Rank
1       Eren    M       9       10      7       5       No.5
2       Mikasa  F       10      9       8       6       No.1
3       Armin   M       2       6       10      8       No.?
4       Jean    M       9       6       5       5       No.6
5       Connie  M       6       7       3       7       No.8
6       Sasha   F       6       3       5       6       No.9
7               F       7       10      7       2       No.?
8       Reiner  M       9       7       7       10      No.2
9               M       9       4       6       9       No.3
10      Annie   F       10      7       7       3       No.4
11      Christa F       6       6       4       8       No.10
[root@xxx shell]#

sed -e 's/string1//g;s/string2//g' student104.txt    #同时把string1和string2替换为空

注意:没加行号则搜索整篇文档

替换一个文件夹下所有文件中的特定字符串 => sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径`
eg:
sed -i 's/"grade">100/"grade">150/g' `grep grade -rl test/`

将当前目录下的所有文件中的"March"替换成"April"
sed -i "s/March/April/g" `ls`

 

参考资料:http://www.imooc.com/learn/378 第2章

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值