shell高级-----初识sed和gawk
sed编辑器
sed说明
sed是Linux下一款功能强大的非交互流式文本编辑器,可以对文本文件进行增、删、改、查等操作,支持按行、按字段、按正则匹配文本内容,灵活方便,特别适合于大文件的编辑。
替换选项
使用s命令可以实现替换的作用。S命令会用斜线间指定的第二个文本字符串来替换第一个文本字符串模式;
echo "this is a test" | sed 's/test/try/'
如果要同时替换多个,sed -e 中间用分号;隔开即可
1、替换标记
默认情况下,只会替换一行中的第一处。要想替换一行中不同地方出现的文件必须使用替换标记。
s/pattern/replacement/flags
有四种可用的替换标志:
- 数字:表明新文件将替换第几处模式匹配的地方,比如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会输出修改过的行。这二者配合使用的效果就是只输出被替换命令修改过的行。
2、替换字符
替换文件中的特殊字符会比较麻烦,比如你要替换路径符号/
sed允许其他字符来替换命令中的字符串分隔符。
echo "/ljy/ceshi" | sed 's!/ljy/ceshi!/ljy/test!'
注意末尾的替换符。
使用地址
如果想要命令作用于特定的行或某些行,则必须使用行寻址。
1、以数字方式行寻址
在命令中指定的地址可以是单个行号,或者用起始行号,逗号以及结尾行号指定一定区间。
[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
2、使用文本模式过滤器
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
3、组合命令
[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处理文件
1、写入文件
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文件中
2、从文件读取数据
读取命令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提供了一种编程语言而不只是编程命令。
1、命令格式
gawk options program file*
options的可用选项有:
-F fs 指定行中分隔数据字段的字段分隔符。个人不建议使用这个选项,在BEGIN块中设置FS更好。这个选项只是提供了一个简洁的设置方式。
-f file:指定读取程序的文件名
-v var=value 定义gawk程序中的一个变量及其默认值。个人不建议使用这个选项,在BEGIN块中设置更好。
-mf N 指定要处理的数据文件中的最大字段数
-mr N 指定数据文件中的最大数据行数
-W keyword 指定gawk的兼容模式或警告等级
2、从命令行读取脚本
[root@node1 ~]# awk '{print "hello"}'
asd
hello
adf
hello
asd
hello
qqq
hello
[root@n
要终止这个gawk程序,你必须表明数据流已经结束,
ctrl+D组合键可以在bash中产生一个EOF字符。
3、使用数据字段变量
默认情况下,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
4、在程序脚本中使用多个命令
[root@node1 ~]# echo 'this is sam' | gawk '{$4="lisi";print $0}'
this is sam lisi
5、从文件中读取程序
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
shell高级-----初识sed和gawk的更多相关文章
- [shell编程]初识sed和gawk
一.sed编辑器 shell脚本最常见的用途就是处理文本文件,sed和gawk能够极大的简化需要进行的数据处理任务.sed编辑器是流编辑器,跟普通交互式文本编辑器(如vim)不同.流编辑器 ...
- 《Linux命令行与shell脚本编程大全》第十九章 初识sed和gawk
这两个工具能够极大简化需要进行的数据处理任务. 19.1 文本处理 能轻松实现自动格式化.插入.修改或删除文本元素的简单命令行编辑. sed和gawk就具备上述功能 19.1.1 sed编辑器 被称为 ...
- shell学习记录----初识sed和gawk
Linux命令行与shell脚本编程大全中关于sed和gawk的介绍合在一起,而且结构有点乱. 不像之前的命令写的很清楚.所以这次我需要写下来整理一下. 一.sed部分 1.1 sed命令格式如下: ...
- shell编程之sed编辑器&gawk程序
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/560372 s ...
- Shell编程—sed和gawk
1文本处理 1.1sed 编辑器 sed编辑器被称作流编辑器(stream editor),和普通的交互式文本编辑器恰好相反.在交互式文本编辑器中(比如vim),你可以用键盘命令来交互式地插入.删除或 ...
- Shell高级编程视频教程-跟着老男孩一步步学习Shell高级编程实战视频教程
Shell高级编程视频教程-跟着老男孩一步步学习Shell高级编程实战视频教程 教程简介: 本教程共71节,主要介绍了shell的相关知识教程,如shell编程需要的基础知识储备.shell脚本概念介 ...
- 跟着老男孩一步步学习Shell高级编程实战
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://oldboy.blog.51cto.com/2561410/1264627 本sh ...
- 在shell中使用sed命令替换/为\/
sed命令相关: https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html https://www.cnblogs.com/D ...
- (转)跟着老男孩一步步学习Shell高级编程实战
原文:http://oldboy.blog.51cto.com/2561410/1264627/ 跟着老男孩一步步学习Shell高级编程实战 原创作品,允许转载,转载时请务必以超链接形式标明文章 原 ...
随机推荐
- Python 入门基础
第一章 计算机基础 1.1 硬件 CPU:处理和运算 内存:临时存储数据 硬盘:永久存储系统 操作系统:是一个软件(特殊), 调度每个硬件之间的数据传输 1.2 操作系统 Windows:xp/7/8 ...
- s:iterator的多层迭代
struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:s:iterator 标签有3个属性: value:被迭代的集合 id :指定集 ...
- activiti整合开发实例总结
参考手册:http://www.mossle.com/docs/activiti/ 一.applicationContext.xml中引入activiti相关配置的xml文件 <!-- begi ...
- javaEE(17)_邮件原理与JavaMail开发
一.Java邮件开发介绍 为什么要学习javamail开发 •现在很多WEB应用在开发时都需要集成邮件发送功能,例如: •给新注册的用户自动发送一封包含其注册信息的欢迎E-Mail. •给过生日的注册 ...
- C++系统学习之八:IO库
新的C++标准中有三分之二的内容都是描述标准库.接下来重点学习其中几种核心库设施,这些是应该熟练掌握的. 标准库的核心是很多容器类(顺序容器和关联容器等)和一簇泛型算法(该类算法通常在顺序容器一定范围 ...
- HashMap允许将null用作键 也允许将null作为值
HashMap不能保证元素的顺序,HashMap能够将键设为null,也可以将值设为null. 与之对应的是Hashtable,(注意大小写:不是HashTable),Hashtable不能将键和值设 ...
- DateFormat的format()方法线程不安全的问题分析
最近看到<侦探剧场:堆内存神秘溢出事件>https://my.oschina.net/u/2368090/blog/1628720,于是自己也想测试了解一下DateFormat的多线程安全 ...
- FIFO设计思考之一
不管同步FIFO还是异步FIFO,设计难点是full/empty状态flag的正确性. 要保证任何情况 FULL时NO WRITE,EMPTY时NO READ.overflow / underflow ...
- PHP开发中涉及到emoji表情的几种处理方法!
emoji表情 处理 一般Mysql表设计时,都是用UTF8字符集的.把带有emoji的昵称字段往里面insert一下就没了,整个字段变成了空字符串.这是怎么回事呢? 原来是因为Mysql的utf8字 ...
- Python9-模块2-序列化-day20
序列化 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 序列就是字符串 序列化的目的1.以某种存储形式使自定义对象持久化:2.将对象从一个地方传递到另一个地方.3.使程序 ...