sed编辑工具
sed是一个做交互式 面向字符流的编辑器,它和许多unix程序同样,被认为是面向字符流的,这是由于输入流经过
程序并将输出直接输送到标准输出端,输入能够来自文件,也能够来自键盘,输出默认在屏幕上,也能够输出到
文件中,sed 经过解释脚原本工做,这一脚本指定了将要执行的工做
sed能够做为编辑过滤器使用,换句话说,你能够处理输入文件并将输出结果发送到另一个程序
sed 很容易被堪称为交互式编辑相反的程序,sed程序与如何手动的应用编辑是很接近的,sed限制了
你利用在文本编辑器中使用的方法,awk为处理文本提供了更通常的模型。
awk程序的典型实例是将数据格式化吧的报表,这些数据
多是unix产生的日志文件,并且报表以一种对系统管理员有用 的格式化数据汇总,全部这些的关健是他具备
某一种数据结构。
由于,当数据拥有某一种数据结构时,最能体现awk的好处,文本文件的结构或松或紧,咱们将看到一个脚本,用于提取章节标题并将它们编号,
由制表符分割的列项目所组成的表结构是高度结构化的,能够利用awk脚本对数据从新排序,甚至能够把它们的行变列,裂变行
awk能够完成的功能
1.将文本文件看作由记录和字段组成的文本数据库
2.使用变量操做数据库
3.使用算数和字符串操做符
4.使用普通的程序结构,列如循环和条件
5.生成格式化报告
6.定义函数
7.从脚本中执行unix命令
8.更加巧妙的处理命令行参数
9.更容易的处理多个输入流
sed 和awk都基于行编辑器
基本操做
$ed 文件名
若机器不理解就显示问号
$ ed <- 激活 ed 命令
a <- 告诉 ed 我要编辑新文件
My name is Titan. <- 输入第一行内容
And I love Perl very much. <- 输入第二行内容
. <- 返回 ed 的命令行状态
i <- 告诉 ed 我要在最后一行以前插入内容
I am 24. <- 将“I am 24.”插入“My name is Titan.”和“And I love Perl very much.”之间
. <- 返回 ed 的命令行状态
c <- 告诉 ed 我要替换最后一行输入内容
I am 24 years old. <- 将“I am 24.”替换成“I am 24 years old.”(注意:这里替换的是最后输的内容)
. <- 返回 ed 的命令行状态
w readme.text <- 将文件命名为“readme.text”并保存(注意:若是是编辑已经存在的文件,只须要敲入 w 便可)
q <- 彻底退出 ed 编辑
输入p当前的行内容
输入行号移动到第几行
删除命令是d
1d删除第几行
删除包含regular的行斜杠之间是正则表达式,删除这一行,下行移到当前行
/regular/d删除当前行
g/regular/d删除全部行 这时的g是全局global
替代文件
[addrress]s /pattern/replacement/flag
s/regular/replacement/替换当前行第一次出现[regular]单词
s/regular/replacement/g同一行的屡次出现,若没找到会出现?这时的g指的是屡次
/regular/s/regular/replacement/g这个命令影响文件中与这个地址匹配的第一行,地址和模式没必要相同
/T00000000200/s/T00000000200/22/g第一个是地址,第二个是模式
1命令
22
g/T00000000200/s/T00000000200/22/g
sed和ed不一样,他不能交互式使用,sed与ed不一样的是他是面向字符流的,默认状况下,到sed的全部全部输入命令都会相应处理,
并转为标准输出
ed只做用于当前行,而sed会遍历全部行,awk是做为可编程的编辑器开发的,同sed同样,他也是面向字符流的,而且解释编辑命令的脚本
/regular/{print}
awk最独特的特征是分拆或者拆分每个输入行,并生成每个可用于脚本处理的独立单词
命令行语法
command [option] scripts filename
sed和awk共同的选项
sed -f scriptsfile inputfile
$cat list
Yesterday was my birthday, so some of my classmates sent me presents.Mother prepared a tea
party for me. I invited all of them to come and take part in it.
The tea party began at half past six. There were cold drinks and refreshments.
We ate, talked and laughed. We felt that we were the happiest men in the worldI like the Chinese
new year better than any other festival. This is a time especially for rest and joy.I need not study.
I wear good clothes and eat good food. I have a good time from morning till night.I am as happy
as a king.No sooner had the witer vacation begun than I returned to my native town. Of course Imust make
good use of it;.In the morning I reviewed my lessons and read newspapers or magazines.
In the afternoon I played ball games with my friends or went fishing in the river.
At night I watched television with my family. hardly had the clock on the wall struck ten when I went to
bed.
I live very happily today! In the morning, it is very fine! Then I climb the mountain with family,
the air on the mountain is very fresh, the flowers plants and trees on the mountain all seem extremely
beautiful. Coming back home in the evening, family and I sat and watched TV together, we are returning and
eating the fruit while chatting, the whole family is happy and harmonious!
sed命令
sed 's/my/gdc/g' list 全部行的my 替换为gdc
有三种方式指定命令行上的多重指令
1.用分号
sed 's/my/gdc/g;s/and/"##"/g' list
2.用-e选项
sed -e 's/my/gdc/g' -e 's/and/"##"/g' list
3。使用shell的分行指令功能
$ sed '
> s/my/gdc/g
> s/and/"##"/g list
若是出现了错误,就用gsed调试
4.脚本文件
$cat tt
s/my/gdc/g
s/and/"##"/g
$sed -f tt list
经常使用命令
阻止输入行的自动提示
sed -n -f tt list 这时就看不了输出的内容了
sed -f tt list操做替换后的输出
使用awk
awk和sed结构相同,但过程不一样awk不像编辑器而像一种语言,语句和函数取代了使用一个或者两个字符组成的序列,列如使用
print 打印表达式的值或打印当前输入行的内容,一般状况下awk将每行输入解释为输入记录,而将上一行输入的单词解释为一个字段
由空格和tab健分割
$awk '{print $1}' list ##输出第一个单词
$awk '/Yesterday/' list ##匹配输出第一行的数据Yesterday was my birthday, so some of my classmates sent me presents.Mother prepared a tea
$awk '/Yesterday/ {print $1}' list #Yesterday
5.
awk -F, '/Yesterday/ {print $1}' list #将分隔符变为,Yesterday was my birthday
将每一个字段单独打印在这一行上,多重命令由;隔开
awk -F, '/Yesterday/ {print $1;print $2;print $3}' list
awk -F, '/Yesterday/ {print $1;print $2}' list
#Yesterday was my birthday
#so some of my classmates sent me presents.Mother prepared a tea
awk命令行选项
-f 文件跟随脚本名
-F分隔符
-v var=value
同时使用sed和awk
sed -f tt list |awk -F, '{print $1}'
正则表达式