shell高级-----初识sed和gawk

sed编辑器

sed说明

sed是Linux下一款功能强大的非交互流式文本编辑器,能够对文本文件进行增、删、改、查等操做,支持按行、按字段、按正则匹配文本内容,灵活方便,特别适合于大文件的编辑。node

替换选项

使用s命令能够实现替换的做用。S命令会用斜线间指定的第二个文本字符串来替换第一个文本字符串模式;mysql

echo "this is a test" | sed 's/test/try/'  

若是要同事替换多个,中间yoga分号;隔开便可sql

一、替换标记docker

默认状况下,只会替换一行中的第一处。要想替换一行中不一样地方出现的文件必须使用替换标记。编程

s/pattern/replacement/flagscentos

有四种可用的替换标志:bash

  • 数字:代表新文件将替换第几处模式匹配的地方,好比2,替换每行中第二次出现的文本
  • g :代表新文件将会替换全部匹配的文本
  • p : 打印出匹配的内容 ,一般与sed的-n一块儿使用
  • w file :将替换的结果写入到文件中
[root@node3 ljy]# more ceshi.sh                  
this is one,one,one
[root@node3 ljy]# sed -i 's/one/two/2 ' ceshi.sh                                         
[root@node3 ljy]# more ceshi.sh                  
this is one,two,one
[root@node3 ljy]# sed -i 's/one/two/g ' ceshi.sh  
[root@node3 ljy]# more ceshi.sh                  
this is two,two,two

-n选项是禁止sed编辑器输出,-p会输出修改过的行。这两者配合使用的效果就是只输出被替换命令修改过的行。app

二、替换字符

替换文件中的特殊字符会比较麻烦,好比你要替换路径符号/ssh

sed容许其余字符来替换命令中的字符串分隔符。编程语言

echo "/ljy/ceshi" | sed 's!/ljy/ceshi!/ljy/test!'

 注意末尾的替换符。

使用地址

若是想要命令做用于特定的行或某些行,则必须使用行寻址。

一、以数字方式行寻址

在命令中指定的地址能够是单个行号,或者用起始行号,逗号以及结尾行号指定必定区间。

[root@node1 ljy]# sed '2s/cat/dog/' cat
this is a cat
this is a dog
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
[root@node1 ljy]# sed '2,3s/cat/dog/' cat
this is a cat
this is a dog
this is a dog
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
[root@node1 ljy]# sed '2,$s/cat/dog/' cat  
this is a cat
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog

二、使用文本模式过滤器

sed编辑器容许指定文本模式来过滤出命令要作用的行。

[root@node1 ljy]# more cat 
there is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
[root@node1 ljy]# sed '/there/s/cat/dog/' cat 
there is a dog
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat

三、组合命令

[root@node1 ljy]# sed '2,${
> s/cat/dog/
> s/this/there/
> }' cat
there is a cat
there is a dog
there is a dog
there is a dog
there is a dog
there is a dog
there is a dog
there is a dog

能够用花括号将多条命令组合在一块儿使用。

删除行

删除命令d

[root@node1 ljy]# sed '4d' num
this is 1
this is 2
this is 3
this is 5
[root@node1 ljy]# sed '3,$d' num 
this is 1
this is 2
[root@node1 ljy]# sed '/this/d' num

插入和附加文本

插入命令i会在指定行前增长一个新行

附加命令a会在指定行后增长一个新行。

[root@node1 ljy]# sed '2i\this is a test' num 
this is 1
this is a test
this is 2
this is 3
[root@node1 ljy]# sed '2a\this is a test' num  
this is 1
this is 2
this is a test
this is 3
[root@node1 ljy]# sed '$a\this is a test' num 
this is 1
this is 2
this is 3
this is a test

修改行

修改命令容许修改数据流中整行文本的内容。

[root@node1 ljy]# sed '3c\this is a test' num 
this is 1
this is 2
this is a test
[root@node1 ljy]# sed '/this is 3/c\this is a test' num    #文本模式也能够
this is 1
this is 2
this is a test

转换命令

转换命令(y)是惟一能够处理单个字符的sed编辑器命令。

[root@node1 ljy]# sed 'y/2/9/' num    
this is 1
this is 9
this is 3

回顾打印

p命令用来打印文本行

=命令用来打印行号

l命令用来列出行

[root@node1 ljy]# sed -n '/is 2/p' num 
this is 2
[root@node1 ljy]# sed '=' num            
1
this is 1
2
this is 2
3
this is 3
[root@node1 ljy]# sed 'l' num  
this is 1$
this is 1
this is 2$
this is 2
this is 3$
this is 3

使用sed处理文件

一、写入文件

w命令用来向文件写入行

[root@node1 ljy]# sed '1,2w ceshi' num 
this is 1
this is 2
this is 3
[root@node1 ljy]# more ceshi 
this is 1
this is 2

把num的前两行写入到了ceshi文件中

二、从文件读取数据

读取命令r容许你将一个独立文件中的数据插入到另外一个数据流中。

[root@node1 ljy]# more ceshi1
this is a
this is b
[root@node1 ljy]# more ceshi2
hello
hi
[root@node1 ljy]# sed '2r ceshi1' ceshi2
hello
hi
this is a
this is b

gawk程序

gawk提供了一种编程语言而不仅是编程命令。

一、命令格式

gawk options program file*

options的可用选项有:

 -F fs 指定行中分隔数据字段的字段分隔符。我的不建议使用这个选项,在BEGIN块中设置FS更好。这个选项只是提供了一个简洁的设置方式。
 -f file:指定读取程序的文件名
 -v var=value 定义gawk程序中的一个变量及其默认值。我的不建议使用这个选项,在BEGIN块中设置更好。
 -mf N 指定要处理的数据文件中的最大字段数
 -mr N 指定数据文件中的最大数据行数
 -W keyword 指定gawk的兼容模式或警告等级

二、从命令行读取脚本

[root@node1 ~]# awk '{print "hello"}'
asd
hello
adf
hello
asd
hello
qqq
hello
[root@n

要终止这个gawk程序,你必须代表数据流已经结束,

ctrl+D组合键能够在bash中产生一个EOF字符。

三、使用数据字段变量

默认状况下,gawk会将以下变量分配给它在文本中发现的数据字段:

$0  表明整个文本行
$1  表明文本行的第一个数据段
$n  表明文本行的第n个数据段
$NF  表明文本行的最后一个数据段

gwak中默认的字段分隔符书任意的空白字符。

[root@node1 ~]# df -h | gawk '{print $5}'
已用%
5%
0%
0%
1%
0%
14%
0%
[root@node1 ~]# df -h | gawk '{print $NF}'
挂载点
/
/dev
/dev/shm
/run
/sys/fs/cgroup
/boot
/run/user/0
[root@node1 ~]# df -h | gawk '{print $0}' 
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   42G  2.1G   40G    5% /
devtmpfs                 908M     0  908M    0% /dev
tmpfs                    920M     0  920M    0% /dev/shm
tmpfs                    920M  8.8M  911M    1% /run
tmpfs                    920M     0  920M    0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M   14% /boot
tmpfs                    184M     0  184M    0% /run/user/0

四、在程序脚本中使用多个命令

[root@node1 ~]# echo 'this is sam' | gawk '{$4="lisi";print $0}'  
this is sam lisi

五、从文件中读取程序

gawk编辑器容许将程序存储到文件中,而后在命令行中引用。

[root@node1 ljy]# more script.gawk 
{print $1 "'s home directory is " $6}
[root@node1 ljy]# gawk -F: -f script.gawk /etc/passwd
root's home directory is /root
bin's home directory is /bin
daemon's home directory is /sbin
adm's home directory is /var/adm
lp's home directory is /var/spool/lpd
sync's home directory is /sbin
shutdown's home directory is /sbin
halt's home directory is /sbin
mail's home directory is /var/spool/mail
operator's home directory is /root
games's home directory is /usr/games
ftp's home directory is /var/ftp
nobody's home directory is /
systemd-network's home directory is /
dbus's home directory is /
polkitd's home directory is /
sshd's home directory is /var/empty/sshd
postfix's home directory is /var/spool/postfix
chrony's home directory is /var/lib/chrony
mysql's home directory is /var/lib/mysql
dockerroot's home directory is /var/lib/docker
ljy's home directory is /home/ljy
相关文章
相关标签/搜索