shell基础十二篇
来源:互联网 发布:少女前线数据库 编辑:程序博客网 时间:2024/05/11 16:26
编者按:由 wingger 整理的 shell基础十二篇 以及L_kernel补充的第十三——二十篇,涉及shell 编程及使用的各个方面,又附有大量的例子,极适合初学者系统学习。如果配合網中人的shell 十三問? ,效果更加明显。
这里是其中的第十章 sed. 其他各章可察看相应的 link.
shell基础1:文件安全与权限
http://bbs.chinaunix.net/thread-434579-1-1.html
附:Linux的用户和用户组管理
http://bbs.chinaunix.net/thread-438660-1-1.html
shell基础二:查找技巧,find及xargs的使用
http://bbs.chinaunix.net/thread-441883-1-1.html
shell基础三和四:后台(crontab,at,&,nohup)及(*,?,[]等)
http://bbs.chinaunix.net/thread-442596-1-1.html
shell基础五:输入和输出(echo,read,cat,管道,tee,重定向等)
http://bbs.chinaunix.net/thread-444209-1-1.html
shell基础六七:命令执行顺序(||及&&,{}及())和正则表?
http://bbs.chinaunix.net/thread-445229-1-1.html
shell基础八:文本过滤工具(grep)
http://bbs.chinaunix.net/thread-446683-1-1.html
shell基础九:awk
http://bbs.chinaunix.net/thread-448687-1-1.html
shell基础十:sed
http://bbs.chinaunix.net/thread-452942-1-1.html
shell基础11:文件分类、合并和分割(sort,uniq,join,cut,paste,split)
http://bbs.chinaunix.net/thread-457730-1-1.html
shell基础十二:tr
http://bbs.chinaunix.net/thread-459099-1-1.html
感谢L_kernel补充的第十三——二十篇(cjaizss添加)
shell基础第十三篇-登录环境
http://bbs.chinaunix.net/thread-1820174-1-1.html
shell基础第十四篇-环境和shell变量
http://bbs.chinaunix.net/thread-1820194-1-1.html
shell基础第十五篇-引号
http://bbs.chinaunix.net/thread-1820220-1-1.html
shell基础第十六篇-shell脚本介绍
http://bbs.chinaunix.net/thread-1820301-1-1.html
shell基础学习第十七篇-条件测试
http://bbs.chinaunix.net/thread-1820304-1-1.html
shell基础第十八篇-控制流结构
http://bbs.chinaunix.net/thread-1820329-1-1.html
shell学习基础第十九篇-shell函数
http://bbs.chinaunix.net/thread-1820454-1-1.html
shell基础学习第二十篇-向脚本传递参数
http://bbs.chinaunix.net/thread-1823335-1-1.html
其它我的笔记都在我的BLOG上,呵呵,有兴趣的可以去看看
shell基础十:sed
sed 用法介绍
s e d是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。
可以在命令行输入s e d命令,也可以在一个文件中写入命令,然后调用s e d,这与a w k基本相同。使用s e d需要记住的一个重要事实是,无论命令是什么, s e d并不与初始化文件打交道,它操作的只是一个拷贝,然后所有的改动如果没有重定向到一个文件,将输出到屏幕。
因为s e d是一个非交互性编辑器,必须通过行号或正则表达式指定要改变的文本行。
本文介绍s e d用法和功能。本章大多编写的是一行命令和小脚本。这样做可以慢慢加深对s e d用法的了解,取得宝贵的经验,以便最终自己编出大的复杂s e d脚本。
和g r e p与a w k一样, s e d是一种重要的文本过滤工具,或者使用一行命令或者使用管道与g r e p与a w k相结合。
1 sed怎样读取数据
s e d从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。
2 调用sed
调用s e d有三种方式:在命令行键入命令;将s e d命令插入脚本文件,然后调用s e d;将s e d命令插入脚本文件,并使s e d脚本可执行。
使用s e d命令行格式为:复制代码记住在命令行使用s e d命令时,实际命令要加单引号。s e d也允许加双引号。
使用s e d脚本文件,格式为:复制代码要使用第一行具有s e d命令解释器的s e d脚本文件,其格式为:复制代码不管是使用s h e l l命令行方式或脚本文件方式,如果没有指定输入文件, s e d从标准输入中接受输入,一般是键盘或重定向结果。
2.1 保存sed输出
由于不接触初始化文件,如果想要保存改动内容,简单地将所有输出重定向到一个文件即可。下面的例子重定向s e d命令的所有输出至文件‘ m y o u t f i l e’,当对结果很满意时使用这种方法。复制代码2.2 使用sed在文件中查询文本的方式
s e d浏览输入文件时,缺省从第一行开始,有两种方式定位文本:
下面是使用s e d定位文本的一些方式。复制代码2.3 基本sed编辑命令复制代码sed和正则表达式
s e d识别任何基本正则表达式和模式及其行匹配规则。记住规则之一是:如果要定位一特殊字符,必须使用( \)屏蔽其特殊含义
[ 本帖最后由 mocou 于 2005-11-18 14:40 编辑 ]
这里是其中的第十章 sed. 其他各章可察看相应的 link.
shell基础1:文件安全与权限
http://bbs.chinaunix.net/thread-434579-1-1.html
附:Linux的用户和用户组管理
http://bbs.chinaunix.net/thread-438660-1-1.html
shell基础二:查找技巧,find及xargs的使用
http://bbs.chinaunix.net/thread-441883-1-1.html
shell基础三和四:后台(crontab,at,&,nohup)及(*,?,[]等)
http://bbs.chinaunix.net/thread-442596-1-1.html
shell基础五:输入和输出(echo,read,cat,管道,tee,重定向等)
http://bbs.chinaunix.net/thread-444209-1-1.html
shell基础六七:命令执行顺序(||及&&,{}及())和正则表?
http://bbs.chinaunix.net/thread-445229-1-1.html
shell基础八:文本过滤工具(grep)
http://bbs.chinaunix.net/thread-446683-1-1.html
shell基础九:awk
http://bbs.chinaunix.net/thread-448687-1-1.html
shell基础十:sed
http://bbs.chinaunix.net/thread-452942-1-1.html
shell基础11:文件分类、合并和分割(sort,uniq,join,cut,paste,split)
http://bbs.chinaunix.net/thread-457730-1-1.html
shell基础十二:tr
http://bbs.chinaunix.net/thread-459099-1-1.html
感谢L_kernel补充的第十三——二十篇(cjaizss添加)
shell基础第十三篇-登录环境
http://bbs.chinaunix.net/thread-1820174-1-1.html
shell基础第十四篇-环境和shell变量
http://bbs.chinaunix.net/thread-1820194-1-1.html
shell基础第十五篇-引号
http://bbs.chinaunix.net/thread-1820220-1-1.html
shell基础第十六篇-shell脚本介绍
http://bbs.chinaunix.net/thread-1820301-1-1.html
shell基础学习第十七篇-条件测试
http://bbs.chinaunix.net/thread-1820304-1-1.html
shell基础第十八篇-控制流结构
http://bbs.chinaunix.net/thread-1820329-1-1.html
shell学习基础第十九篇-shell函数
http://bbs.chinaunix.net/thread-1820454-1-1.html
shell基础学习第二十篇-向脚本传递参数
http://bbs.chinaunix.net/thread-1823335-1-1.html
其它我的笔记都在我的BLOG上,呵呵,有兴趣的可以去看看
shell基础十:sed
sed 用法介绍
s e d是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。
" 抽取域。
" 匹配正则表达式。
" 比较域。
" 增加、附加、替换。
" 基本的s e d命令和一行脚本。
可以在命令行输入s e d命令,也可以在一个文件中写入命令,然后调用s e d,这与a w k基本相同。使用s e d需要记住的一个重要事实是,无论命令是什么, s e d并不与初始化文件打交道,它操作的只是一个拷贝,然后所有的改动如果没有重定向到一个文件,将输出到屏幕。
因为s e d是一个非交互性编辑器,必须通过行号或正则表达式指定要改变的文本行。
本文介绍s e d用法和功能。本章大多编写的是一行命令和小脚本。这样做可以慢慢加深对s e d用法的了解,取得宝贵的经验,以便最终自己编出大的复杂s e d脚本。
和g r e p与a w k一样, s e d是一种重要的文本过滤工具,或者使用一行命令或者使用管道与g r e p与a w k相结合。
1 sed怎样读取数据
s e d从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。
2 调用sed
调用s e d有三种方式:在命令行键入命令;将s e d命令插入脚本文件,然后调用s e d;将s e d命令插入脚本文件,并使s e d脚本可执行。
使用s e d命令行格式为:
- sed [选项] s e d命令输入文件。
使用s e d脚本文件,格式为:
- sed [选项] -f sed脚本文件输入文件
- s e d脚本文件[选项] 输入文件
s e d选项如下:
n 不打印;s e d不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以用来打印编辑行。
c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条s e d命令,此选项无用,但指定它也没有关系。
f 如果正在调用s e d脚本文件,使用此选项。此选项通知s e d一个脚本文件支持所有的s e d命令,例如:sed -f myscript.sed input_file,这里m y s c r i p t . s e d即为支持s e d命令的文件。
2.1 保存sed输出
由于不接触初始化文件,如果想要保存改动内容,简单地将所有输出重定向到一个文件即可。下面的例子重定向s e d命令的所有输出至文件‘ m y o u t f i l e’,当对结果很满意时使用这种方法。
- $sed 'some-sed-commands' input-file > myoutfile
s e d浏览输入文件时,缺省从第一行开始,有两种方式定位文本:
1) 使用行号,可以是一个简单数字,或是一个行号范围。
2 ) 使用正则表达式
下面是使用s e d定位文本的一些方式。
- x x为一行号,如1
- x , y 表示行号范围从x到y,如2,5表示从第2行到第5行
- / p a t t e r n / 查询包含模式的行。例如/ d i s k /或/[a-z]/
- / p a t t e r n / p a t t e r n / 查询包含两个模式的行。例如/ d i s k / d i s k s /
- p a t t e r n / , x 在给定行号上查询包含模式的行。如/ r i b b o n / , 3
- x , / p a t t e r n / 通过行号和模式查询匹配行。3 . / v d u /
- x , y ! 查询不包含指定行号x和y的行。1 , 2 !
- sed编辑命令
- p 打印匹配行
- = 显示文件行号
- a \ 在定位行号后附加新文本信息
- i \ 在定位行号后插入新文本信息
- d 删除定位行
- c \ 用新文本替换定位文本
- s 使用替换模式替换相应模式
- r 从另一个文件中读文本
- w 写文本到一个文件
- q 第一个模式匹配完成后推出或立即推出
- l 显示与八进制A S C I I代码等价的控制字符
- { } 在定位行执行的命令组
- n 从另一个文件中读文本下一行,并附加在下一行
- g 将模式2粘贴到/pattern n/
- y 传送字符
- n 延续到下一输入行;允许跨行的模式匹配语句
s e d识别任何基本正则表达式和模式及其行匹配规则。记住规则之一是:如果要定位一特殊字符,必须使用( \)屏蔽其特殊含义
[ 本帖最后由 mocou 于 2005-11-18 14:40 编辑 ]
- 本版精华
- 文库|博客
- shell 十三問?
- awk初学之常见问题
- Linux相关书籍(2014-12-23:更新ABS英文第10版)
- shell基础二十篇
- shell数组小结
- 文本编辑的一点心得--sed篇
- awk数组的学习心得
- 取一个段落中关键字的问题(已解决)
- Shell 游戏:贪吃蛇
- 专家座谈——脚本分享与交流(大奖)
- Chapter 03 Using the Shell.ppt
- Shell Gadus S2 V220AD 2.pdf
- CS 497C – Introduction to UNIX Lecture 34 - Shell Programming.ppt
- Linux Shell脚本攻略.pdf
- 反转链表(递归实现)
- Java8读文件仅需一行代码
sed -e "/grep/d" 是什么意思?谢谢 ... | 谁能够帮我解决LINUX 2.6 10版本驱动 | 现在的博客积分不会更新了吗? | shell怎么读取网页内容 |
wingger
荣誉版主
- 帖子
- 22805
- 主题
- 776
- 精华
- 182
- 可用积分
- 8208
- 专家积分
- 0
- 在线时间
- 127 小时
- 注册时间
- 2003-07-09
- 最后登录
- 2014-11-24
2楼 [报告]
发表于 2004-11-26 11:13:55 |只看该作者
shell基础十二篇
s e d例子中使用下述文本文件q u o t e . t x t。
复制代码
1 使用p(rint)显示行
只打印第二行,用-n
复制代码
2 打印范围
可以指定行的范围,现打印1到3行,用逗号分隔行号。
复制代码
3 打印模式
假定要匹配单词N e a v e,并打印此行,方法如下。使用模式/ p a t t e r n /格式,这里为/ N e a v e /。
复制代码
4 使用模式和行号进行查询
可以将行号和模式结合使用。假定要改动文件q u o t e . t x t最后一行中的单词t h e,使用s e d查询t h e,返回两行:
复制代码
使用模式与行号的混合方式可以剔除第一行,格式为l i n e _ n u m b e r, / p a t t e r n /。逗号用来分隔行号与模式开始部分。为达到预期结果,使用4 , / t h e /。意即只在第四行查询模式t h e,命令如下:
复制代码
上面有错,其实是把第四行后的都打出来了
这个模式应该哪果指定行找不到符合条件的,就从下一行开始查找,直到找到为止,并把,找到行之前的全部打打印出来。
如果指定行本身就符合条伯,把本行及后面的行的全部打印出来
5 匹配元字符
匹配元字符$前,必须使用反斜线\屏蔽其特殊含义。模式为/\$/ p。
复制代码
6 显示整个文件
要打印整个文件,只需将行范围设为第一行到最后一行1 , $。$意为最后一行。
复制代码
7 任意字符
匹配任意字母,后跟任意字母的0次或多次重复,并以i n g结尾,模式为/ . * i n g /。可以使用这个模式查询以i n g结尾的任意单词。
复制代码
8 首行
要打印文件第一行,使用行号:
复制代码
9 最后一行
要打印最后一行,使用$。$是代表最后一行的元字符。
复制代码
10 打印行号
要打印行号,使用等号=。打印模式匹配的行号,使用格式/ p a t t e r n / =。
复制代码
整个文件都打印出来,并且匹配行打印了行号。如果只关心实际行号,使用- e选项。
复制代码
如果只打印行号及匹配行,必须使用两个s e d命令,并使用e选项。第一个命令打印模式匹配行,第二个使用=选项打印行号,格式为sed -n -e /pattern/p -e /pattern/=
复制代码
- [sam@Linux_chenwy sam]$ cat quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
1 使用p(rint)显示行
只打印第二行,用-n
- [sam@Linux_chenwy sam]$ sed -n '2p' quote.txt
- It was an evening of splendid music and company.
2 打印范围
可以指定行的范围,现打印1到3行,用逗号分隔行号。
- [sam@Linux_chenwy sam]$ sed -n '1,3p' quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
3 打印模式
假定要匹配单词N e a v e,并打印此行,方法如下。使用模式/ p a t t e r n /格式,这里为/ N e a v e /。
- [sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt
- The honeysuckle band played all night long for only $90.
- The local nurse Miss P.Neave was in attendance.
4 使用模式和行号进行查询
可以将行号和模式结合使用。假定要改动文件q u o t e . t x t最后一行中的单词t h e,使用s e d查询t h e,返回两行:
- [sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt
- The honeysuckle band played all night long for only $90.
- The local nurse Miss P.Neave was in attendance.
使用模式与行号的混合方式可以剔除第一行,格式为l i n e _ n u m b e r, / p a t t e r n /。逗号用来分隔行号与模式开始部分。为达到预期结果,使用4 , / t h e /。意即只在第四行查询模式t h e,命令如下:
- [sam@Linux_chenwy sam]$ sed -n '4,/The/p' quote.txt
- The local nurse Miss P.Neave was in attendance.
上面有错,其实是把第四行后的都打出来了
这个模式应该哪果指定行找不到符合条件的,就从下一行开始查找,直到找到为止,并把,找到行之前的全部打打印出来。
如果指定行本身就符合条伯,把本行及后面的行的全部打印出来
5 匹配元字符
匹配元字符$前,必须使用反斜线\屏蔽其特殊含义。模式为/\$/ p。
- [sam@Linux_chenwy sam]$ sed -n '/\$/p' quote.txt
- The honeysuckle band played all night long for only $90.
6 显示整个文件
要打印整个文件,只需将行范围设为第一行到最后一行1 , $。$意为最后一行。
- [sam@Linux_chenwy sam]$ sed -n '1,$p' quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
7 任意字符
匹配任意字母,后跟任意字母的0次或多次重复,并以i n g结尾,模式为/ . * i n g /。可以使用这个模式查询以i n g结尾的任意单词。
- [sam@Linux_chenwy sam]$ sed -n '/.*ing/p' quote.txt
- It was an evening of splendid music and company.
8 首行
要打印文件第一行,使用行号:
- [sam@Linux_chenwy sam]$ sed -n '1p' quote.txt
- The honeysuckle band played all night long for only $90.
9 最后一行
要打印最后一行,使用$。$是代表最后一行的元字符。
- [sam@Linux_chenwy sam]$ sed -n '$p' quote.txt
- The local nurse Miss P.Neave was in attendance.
10 打印行号
要打印行号,使用等号=。打印模式匹配的行号,使用格式/ p a t t e r n / =。
- [sam@Linux_chenwy sam]$ sed -e '/music/=' quote.txt
- The honeysuckle band played all night long for only $90.
- 2
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
整个文件都打印出来,并且匹配行打印了行号。如果只关心实际行号,使用- e选项。
- [sam@Linux_chenwy sam]$ sed -n '/music/=' quote.txt
- 2
如果只打印行号及匹配行,必须使用两个s e d命令,并使用e选项。第一个命令打印模式匹配行,第二个使用=选项打印行号,格式为sed -n -e /pattern/p -e /pattern/=
- [sam@Linux_chenwy sam]$ sed -n -e '/music/p' -e '/music/=' quote.txt
- It was an evening of splendid music and company.
- 2
注册送豪礼!云智慧监控宝送Kindle| AMD全新x86架构惊曝秘密大杀器 | 创新网络 启航未来 深信服带您飞 | 移动互联时代的网络安全讨论
寂寞烈火
家境小康
- 帖子
- 29808
- 主题
- 891
- 精华
- 22
- 可用积分
- 1114
- 专家积分
- 143
- 在线时间
- 1586 小时
- 注册时间
- 2004-10-01
- 最后登录
- 2012-01-13
3楼 [报告]
发表于 2004-11-26 12:30:05 |只看该作者
shell基础十二篇
终于等到sed啦,好好学学
P.S:新版本的sed(GNU sed version 4.0.5)带有-i选项,允许直接编辑修改文件内容而不需要重定向到临时文件
例如:
/home/lee#cat file
old
abc
/home/lee#sed -i 's/old/new/' file
/home/lee#cat file
new
abc
P.S:新版本的sed(GNU sed version 4.0.5)带有-i选项,允许直接编辑修改文件内容而不需要重定向到临时文件
例如:
/home/lee#cat file
old
abc
/home/lee#sed -i 's/old/new/' file
/home/lee#cat file
new
abc
注册送豪礼!云智慧监控宝送Kindle| AMD全新x86架构惊曝秘密大杀器 | 创新网络 启航未来 深信服带您飞 | 移动互联时代的网络安全讨论
wingger
荣誉版主
- 帖子
- 22805
- 主题
- 776
- 精华
- 182
- 可用积分
- 8208
- 专家积分
- 0
- 在线时间
- 127 小时
- 注册时间
- 2003-07-09
- 最后登录
- 2014-11-24
4楼 [报告]
发表于 2004-11-26 14:29:08 |只看该作者
shell基础十二篇
原帖由 "寂寞烈火" 发表:
终于等到sed啦,好好学学
P.S:新版本的sed(GNU sed version 4.0.5)带有-i选项,允许直接编辑修改文件内容而不需要重定向到临时文件
例如:
/home/lee#cat file
old
abc
/home/lee#sed -i 's/old/new/' file
/..........
这个不错,呵呵,
注册送豪礼!云智慧监控宝送Kindle| AMD全新x86架构惊曝秘密大杀器 | 创新网络 启航未来 深信服带您飞 | 移动互联时代的网络安全讨论
双眼皮的猪
小富即安
- 帖子
- 6137
- 主题
- 256
- 精华
- 27
- 可用积分
- 4023
- 专家积分
- 76
- 在线时间
- 83 小时
- 注册时间
- 2002-11-06
- 最后登录
- 2015-03-07
- 问答
- 好友
5楼 [报告]
发表于 2004-11-26 16:02:24 |只看该作者
shell基础十二篇
可等到wingger JJ的Sed啦...多教我哦...
信曾哥,机房不怕吵;
信春哥,机房不怕冷;
还是信自己吧...
信春哥,机房不怕冷;
还是信自己吧...
注册送豪礼!云智慧监控宝送Kindle| AMD全新x86架构惊曝秘密大杀器 | 创新网络 启航未来 深信服带您飞 | 移动互联时代的网络安全讨论
wingger
荣誉版主
- 帖子
- 22805
- 主题
- 776
- 精华
- 182
- 可用积分
- 8208
- 专家积分
- 0
- 在线时间
- 127 小时
- 注册时间
- 2003-07-09
- 最后登录
- 2014-11-24
6楼 [报告]
发表于 2004-11-26 16:13:05 |只看该作者
shell基础十二篇
[quote]原帖由 "双眼皮的猪"]可等到wingger JJ的Sed啦...多教我哦...[/quote 发表:
等着别人教俺
11 附加文本
要附加文本,使用符号a \,可以将指定文本一行或多行附加到指定行。如果不指定文本放置位置, s e d缺省放在每一行后面。附加文本时不能指定范围,只允许一个地址模式。文本附加操作时,结果输出在标准输出上。注意它不能被编辑,因为s e d执行时,首先将文件的一行文本拷贝至缓冲区,在这里s e d编辑命令执行所有操作(不是在初始文件上),因为文本直接输出到标准输出,s e d并无拷贝。
要想在附加操作后编辑文本,必须保存文件,然后运行另一个s e d命令编辑它。这时文件的内容又被移至缓冲区。
附加操作格式如下:
复制代码
地址指定一个模式或行号,定位新文本附加位置。a\ 通知s e d对a \后的文本进行实际附加操作。观察格式,注意每一行后面有一斜划线,这个斜划线代表换行。s e d执行到这儿,将创建一新行,然后插入下一文本行。最后一行不加斜划线, s e d假定这是附加命令结尾。
当附加或插入文本或键入几个s e d命令时,可以利用辅助的s h e l l提示符以输入多行命令。当附加或插入文本或键入几个s e d命令时,可以利用辅助的s h e l l提示符以输入多行命令。
等着别人教俺
11 附加文本
要附加文本,使用符号a \,可以将指定文本一行或多行附加到指定行。如果不指定文本放置位置, s e d缺省放在每一行后面。附加文本时不能指定范围,只允许一个地址模式。文本附加操作时,结果输出在标准输出上。注意它不能被编辑,因为s e d执行时,首先将文件的一行文本拷贝至缓冲区,在这里s e d编辑命令执行所有操作(不是在初始文件上),因为文本直接输出到标准输出,s e d并无拷贝。
要想在附加操作后编辑文本,必须保存文件,然后运行另一个s e d命令编辑它。这时文件的内容又被移至缓冲区。
附加操作格式如下:
- [address]a\
- text\
- text\
- ......
- text
地址指定一个模式或行号,定位新文本附加位置。a\ 通知s e d对a \后的文本进行实际附加操作。观察格式,注意每一行后面有一斜划线,这个斜划线代表换行。s e d执行到这儿,将创建一新行,然后插入下一文本行。最后一行不加斜划线, s e d假定这是附加命令结尾。
当附加或插入文本或键入几个s e d命令时,可以利用辅助的s h e l l提示符以输入多行命令。当附加或插入文本或键入几个s e d命令时,可以利用辅助的s h e l l提示符以输入多行命令。
注册送豪礼!云智慧监控宝送Kindle| AMD全新x86架构惊曝秘密大杀器 | 创新网络 启航未来 深信服带您飞 | 移动互联时代的网络安全讨论
寂寞烈火
家境小康
- 帖子
- 29808
- 主题
- 891
- 精华
- 22
- 可用积分
- 1114
- 专家积分
- 143
- 在线时间
- 1586 小时
- 注册时间
- 2004-10-01
- 最后登录
- 2012-01-13
7楼 [报告]
发表于 2004-11-26 16:35:26 |只看该作者
shell基础十二篇
文革MM的学习精神深深感染了大家!向文革MM致敬!
注册送豪礼!云智慧监控宝送Kindle| AMD全新x86架构惊曝秘密大杀器 | 创新网络 启航未来 深信服带您飞 | 移动互联时代的网络安全讨论
wingger
荣誉版主
- 帖子
- 22805
- 主题
- 776
- 精华
- 182
- 可用积分
- 8208
- 专家积分
- 0
- 在线时间
- 127 小时
- 注册时间
- 2003-07-09
- 最后登录
- 2014-11-24
8楼 [报告]
发表于 2004-11-27 14:15:34 |只看该作者
shell基础十二篇
http://www.chinalinuxpub.com/doc/oreillybookself/unix/sedawk/index.htm
这个不错哦,awk&sed,嘻嘻
这个不错哦,awk&sed,嘻嘻
注册送豪礼!云智慧监控宝送Kindle| AMD全新x86架构惊曝秘密大杀器 | 创新网络 启航未来 深信服带您飞 | 移动互联时代的网络安全讨论
wingger
荣誉版主
- 帖子
- 22805
- 主题
- 776
- 精华
- 182
- 可用积分
- 8208
- 专家积分
- 0
- 在线时间
- 127 小时
- 注册时间
- 2003-07-09
- 最后登录
- 2014-11-24
9楼 [报告]
发表于 2004-11-27 17:25:16 |只看该作者
shell基础十二篇
创建sed脚本文件
创建脚本文件a p p e n d . s e d:
第一行是s e d命令解释行。脚本在这一行查找s e d以运行命令,这里定位在/ b i n。
第二行以/ c o m p a n y /开始,这是附加操作起始位置。a \通知s e d这是一个附加操作,首先应插入一个新行。
第三行是附加操作要加入到拷贝的实际文本。
输出显示附加结果。如果要保存输出,重定向到一个文件。
复制代码
保存它,增加可执行权限,运行
复制代码
或直接用命令行:
复制代码
[sam@chenwy sam]$ sed "/company/i\utter confusion followed." quote.txt
The honeysuckle band played all night long for only $90.
utter confusion followed.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
插入文本:
插入命令类似于附加命令,只是在指定行前面插入。和附加命令一样,它也只接受一个地址。
如在a t t e n d a n c e结尾的行前插入文本utter confusion followed。
复制代码
也可以指定行:
复制代码
执行结果
复制代码
修改文本
修改命令将在匹配模式空间的指定行用新文本加以替代,格式如下:
将第一行The honeysuckle band played all night long for only $90替换为The office Di b b l e band played well。首先要匹配第一行的任何部分,可使用模式‘ / H o n e y s u c k l e /’。s e d脚本文件为c h a n g e . s e d。内容如下:
复制代码
复制代码
或命令行:
复制代码
可以对同一个脚本中的相同文件进行修改、附加、插入三种动作匹配和混合操作。
复制代码
复制代码
删除文本
s e d删除文本格式:
复制代码
删除第一行;1 d意为删除第一行。
复制代码
删除第一到第三行:
复制代码
删除最后一行:
复制代码
也可以使用正则表达式进行删除操作。下面的例子删除包含文本‘ N e a v e’的行。
复制代码
替换文本
替换命令用替换模式替换指定模式,格式为:
复制代码
s选项通知s e d这是一个替换操作,并查询p a t t e r n - t o - f i n d,成功后用r e p l a c e m e n t - p a t t e r n替换它。
替换选项如下:
如替换n i g h t为N I G H T,首先查询模式n i g h t,然后用文本N I G H T替换它。
复制代码
要从$ 9 0 中删除$ 符号(记住这是一个特殊符号,必须用\ 屏蔽其特殊含义),在r e p l a c e m e n t - p a t t e r n部分不写任何东西,保留空白,但仍需要用斜线括起来。在s e d中也可以这样删除一个字符串。
复制代码
要进行全局替换,即替换所有出现模式,只需在命令后加g选项。下面的例子将所有T h e替换成Wo w!。
复制代码
将替换结果写入一个文件用w选项,下面的例子将s p l e n d i d替换为S P L E N D I D的替换结果写入文件s e d . o u t:
复制代码
注意要将文件名括在s e d的单引号里。文件结果如下:
创建脚本文件a p p e n d . s e d:
第一行是s e d命令解释行。脚本在这一行查找s e d以运行命令,这里定位在/ b i n。
第二行以/ c o m p a n y /开始,这是附加操作起始位置。a \通知s e d这是一个附加操作,首先应插入一个新行。
第三行是附加操作要加入到拷贝的实际文本。
输出显示附加结果。如果要保存输出,重定向到一个文件。
- [sam@chenwy sam]$ cat append.sed
- #!/bin/sed -f
- /company/ a\
- Then suddenly it happed.
保存它,增加可执行权限,运行
- [sam@chenwy sam]chmod u+x append.sed
- [sam@chenwy sam]$ ./append.sed quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Then suddenly it happed.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
或直接用命令行:
- [sam@chenwy sam]$ sed "/company/a\Then suddenly it happened." quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Then suddenly it happened.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
[sam@chenwy sam]$ sed "/company/i\utter confusion followed." quote.txt
The honeysuckle band played all night long for only $90.
utter confusion followed.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
插入文本:
插入命令类似于附加命令,只是在指定行前面插入。和附加命令一样,它也只接受一个地址。
如在a t t e n d a n c e结尾的行前插入文本utter confusion followed。
- [sam@chenwy sam]$ sed "/company/i\Utter confusion followed." quote.txt
也可以指定行:
- [sam@chenwy sam]$ cat insert.sed
- #!/bin/sed -f
- 4 i\
- Utter confusion followed.
执行结果
- [sam@chenwy sam]$ chmod u+x insert.sed
- [sam@chenwy sam]$ ./insert.sed quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- Utter confusion followed.
- The local nurse Miss P.Neave was in attendance.
修改文本
修改命令将在匹配模式空间的指定行用新文本加以替代,格式如下:
将第一行The honeysuckle band played all night long for only $90替换为The office Di b b l e band played well。首先要匹配第一行的任何部分,可使用模式‘ / H o n e y s u c k l e /’。s e d脚本文件为c h a n g e . s e d。内容如下:
- [sam@chenwy sam]$ cat change.sed
- #!/bin/sed -f
- 3 c\
- The office Dibble band played well.
- [sam@chenwy sam]$ chmod u+x change.sed
- [sam@chenwy sam]$ ./change.sed quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- The office Dibble band played well.
- The local nurse Miss P.Neave was in attendance.
或命令行:
- [sam@chenwy sam]$ sed "/honeysuck/c\The Office Dibble band played well." quote.txt
- The Office Dibble band played well.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
可以对同一个脚本中的相同文件进行修改、附加、插入三种动作匹配和混合操作。
- [sam@chenwy sam]$ cat mix.sed
- #!/bin/sed -f
- 1 c\
- The Dibble band were grooving.
- /evening/ i\
- They played some great tunes.
- 3 a\
- Where was the nurse to help?
- [sam@chenwy sam]$ chmod u+x mix.sed
- [sam@chenwy sam]$ ./mix.sed quote.txt
- The Dibble band were grooving.
- They played some great tunes.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- Where was the nurse to help?
- The local nurse Miss P.Neave was in attendance.
删除文本
s e d删除文本格式:
- [ a d d r e s s [,a d d r e s s ] ] d
删除第一行;1 d意为删除第一行。
- [sam@chenwy sam]$ sed '1d' quote.txt
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
删除第一到第三行:
- [sam@chenwy sam]$ sed '1,3d' quote.txt
- The local nurse Miss P.Neave was in attendance.
删除最后一行:
- [sam@chenwy sam]$ sed '$d' quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
也可以使用正则表达式进行删除操作。下面的例子删除包含文本‘ N e a v e’的行。
- [sam@chenwy sam]$ sed '/Neave/d' quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
替换文本
替换命令用替换模式替换指定模式,格式为:
- [ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]
s选项通知s e d这是一个替换操作,并查询p a t t e r n - t o - f i n d,成功后用r e p l a c e m e n t - p a t t e r n替换它。
替换选项如下:
g 缺省情况下只替换第一次出现模式,使用g选项替换全局所有出现模式。
p 缺省s e d将所有被替换行写入标准输出,加p选项将使- n选项无效。- n选项不打印输出结果。
w 文件名使用此选项将输出定向到一个文件。
如替换n i g h t为N I G H T,首先查询模式n i g h t,然后用文本N I G H T替换它。
- [sam@chenwy sam]$ sed 's/night/NIGHT/' quote.txt
- The honeysuckle band played all NIGHT long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
要从$ 9 0 中删除$ 符号(记住这是一个特殊符号,必须用\ 屏蔽其特殊含义),在r e p l a c e m e n t - p a t t e r n部分不写任何东西,保留空白,但仍需要用斜线括起来。在s e d中也可以这样删除一个字符串。
- [sam@chenwy sam]$ sed 's/\$//' quote.txt
- The honeysuckle band played all night long for only 90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
要进行全局替换,即替换所有出现模式,只需在命令后加g选项。下面的例子将所有T h e替换成Wo w!。
- [sam@chenwy sam]$ sed 's/The/Wow!/g' quote.txt
- Wow! honeysuckle band played all night long for only $90.
- It was an evening of splendid music and company.
- Too bad the disco floor fell through at 23:00.
- Wow! local nurse Miss P.Neave was in attendance.
将替换结果写入一个文件用w选项,下面的例子将s p l e n d i d替换为S P L E N D I D的替换结果写入文件s e d . o u t:
- [sam@chenwy sam]$ sed 's/splendid/SPLENDID/w sed.out' quote.txt
- The honeysuckle band played all night long for only $90.
- It was an evening of SPLENDID music and company.
- Too bad the disco floor fell through at 23:00.
- The local nurse Miss P.Neave was in attendance.
注意要将文件名括在s e d的单引号里。文件结果如下:
- [sam@chenwy sam]$ cat sed.out
- It was an evening of SPLENDID music and company.
复制代码
http://bbs.chinaunix.net/thread-452942-1-1.html
0 0
- shell基础十二篇
- (十二)Shell编程基础
- shell基础十二篇 阅读笔记 shell基础1:文件安全与权限
- Shell(十二)
- WF4.0 基础篇 (二十二) Tracking
- Linux基础篇十二——管道
- shell 基础12篇
- Shell编程基础篇
- Shell脚本基础篇
- shell脚本入门篇---shell基础
- 游戏开发基础(十二)
- 网络安全基础之<十二>
- 十二.Java基础_GUI
- ORACLE_基础十二(Profiles)
- Java基础<十二>-----集合
- Java基础十二
- JAVASE基础(十二)
- shell十三问之十二
- ActiveMQ 集群搭建
- json jar 包 json-lib-2.2.3-jdk15.jar 及相关jar
- javascript IE下不能用 trim函数解决方法
- 机器学习入门神文——读懂机器学习从这里开始
- c语言与java中原始数据类型在内存中的占位
- shell基础十二篇
- Antlr包冲突问题
- jquery层级选择器, 选取td
- 23种设计模式(3):抽象工厂模式
- OCP-1Z0-051 补充题库 第42题 DROP TABLE命令的影响
- java ImageIO处理图像的封装 .
- FPGA 降频实现led的闪烁,LED不闪烁
- strdup字符复制,strtok_r分割字符串,strchr查找字符等的用法
- 深入PHP-直观观察array的扩展