sed详细用法

来源:互联网 发布:电子表格数据库 编辑:程序博客网 时间:2024/05/22 15:20

本文转自:http://fangfang0717.blog.51cto.com/236466/130525/

首先来点文的:sed 对文件 进行操作,可以查找,
 
sed 在一行中 使用多个命令时,一定要加-e 命令。
- 描点(anchor)
用以标 识 于句子中的位置所在. 常见有:
^: 表示句首. 如 ^abc 表示以 abc 为首的句子.
$: 表示句尾. 如 abc$ 表示以 abc 结尾的句子.
\<: 表示词首. 如 \<abc 表示以 abc 为首的詞.
\>: 表示词尾. 如 abc\> 表示以 abc 結尾的詞.
- 修饰字符(modifier)
独立表示时本身不具意义, 专门用以修改前一个 char. set 的出现次数. 常见有:


*: 表示前一个char. set 的出现次数为 0 或多次. 如 ab*c 表示 a 與 c 之间可有 0 或多个b 存在.

?: 表示前一个 char. set 的出現次数为 0 或 1 次. 如 ab?c 表示 a 与c 之间可有 0 或 1 个b 存在.

+: 表示前一个 char. set 的出现次數为 1 或多次. 如 ab+c 表示 a 與 c 之间可有 1 或多个 b 存在.
{n}: 表示前一個 char. set 的出現次數必須為 n 次. 如 ab{3,}c 表示 a 與 c 之間必須有 3 個 b 存在.
 
{n,}: 表示前一個 char. set 的出現次數至少為 n 次. 如 ab{3,}c 表示 a 與 c 之間至少有 3 個 b 存在.

{n,m}: 表示前一個 char. set 的出現次數為 n 到 m 次. 如 ab{3,5}c 表示 a 與 c 之間有 3 到 5 個 b 存在. 
 
. : 匹配任意一个字符(1个)

.*:匹配任意多个字符(1或多个)

1.sed 用法介绍:
sed 是一个非交互性的文本编辑器,它可以
.抽取域
.匹配正则表达式
.比较域
.增加, 附加 替换
.基本的sed 命令 和一行脚本
[root@localhost ~]# cat vs.bak
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
[root@localhost ~]# sed -e '1,2w vs.1.bak' vs.bak
 
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.

[root@localhost ~]# cat vs.1.bak
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.

sed基础用法


使用sed在文件中查询文本的方式:
sed浏览输入文件时,缺省从第一行开始,有两种方式定位文本:
a. 使用行号,可以是一个简单数字,或是一个行号范围。
b. 使用正则表达式。
x #为一行号,如1
x,y #表示行号范围从x到y,如2,5表示从第2行到第5行
/pattern/ #查询包含模式的行。例如/disk/或/[a-z]/
/pattern/pattern/ #查询包含两个模式的行。例如/disk/disks/
/pattern/,x #在给定行号上查询包含模式的行。如/ribbon/,3
x,/pattern/ #通过行号和模式查询匹配行。3,/vdu/
x,y! #查询不包含指定行号x和y的行。1,2!

[root@localhost ~]# sed -e '1p' vs.bak
# The default compiled in settings are fairly paranoid. This sample file
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
[root@localhost ~]# sed -n -e  '2,3p' vs.bak
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.

[root@localhost ~]# sed -n -e '/to/p' vs.bak
# loosens things up a bit, to make the ftp daemon more usable.

[root@localhost ~]# sed -n  '/to/,2p' vs.bak

# loosens things up a bit, to make the ftp daemon more usable.
-n:表示不把文档的内容 打到标准输出!!
sed -n -e '/to/=' test.txt//加行号
 
sed -n -e '/to/p' -e '/to/=' test.txt//不打出内容 加行号
 
sed -e '=;p' test.txt 加行号,打印
 
sed -n -e '=;p' test.txt
 
sed -n -e '=' -e 'p' test.txt
a\附加内容 #缺省放在每一行后面
 
sed -e 'a\this line will be added to the end of each line!oooooooooo' test.txt
 
i\插入内容 #缺省放在每一行前面

sed -e 'i\this line will be inserted to the begin of each line!oooooooooo' test.txt
 
sed -e '/music/i\this line will be inserted to the begin of the matching line!oooooooooo' test.txt
 
 sed -n l (小写的L)  tab-space.txt
[root@localhost zsun]# sed -n l tab_space
asdasd asd\tddd$
:可以查看文本中 如果为tab 则显示为\t,如果为空格,则就显示空格