Sed用法测试和整理

        sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。
sed可删除(delete)、改变(change)、添加(append)、插入(insert)、合、交换文件中的资料行,或读入其它档的资料到文>件中,也可替换(substuite)它们其中的字串、或转换(tranfer)其中的字母等等。

sed 的工作方式
sed 实用工具按顺序逐行将文件读入到内存中。然后,它执行为该行指定的所有操作,并在完成请求的修改之后将该行放回到内存中,以将其转储至终端。完成了这一行上的所有操作之后,它读取文件的下一行,然后重复该过程直到它完成该文件。如同前面所提到的,默认输出是将每一行的内容输出到屏幕上。在这里,开始涉及到两个重要的因素—首先,输出可以被重定向到另一文件中,以保存变化;第二,源文件(默认地)保持不被修改。sed 默认读取整个文件并对其中的每一行进行修改。不过,可以按需要将操作限制在指定的行上。

定址
可以通过定址来定位你所希望编辑的行,该地址用数字构成,用逗号分隔的两个行数表示以这两行为起止的行的范围(包括行数表示的那两行)。如1,3表示1,2,3行,美元符号($)表示最后一行。范围可以通过数据,正则表达式或者二者结合的方式确定.

1.sed替换命令功能:
's/{old value}/{new value}/' 

echo The tiger cubs will meet on Tuesday after school | sed 's/tiger/wolf/'
执行结果
[root@oradb xd]# echo The tiger cubs will meet on Tuesday after school | sed 's/tiger/wolf/'
The wolf cubs will meet on Tuesday after school


多次修改
如果需要对同一文件或行作多次修改,
可以有三种方法来实现它。
第一种是使用"-e" 选项,它通知程序使用了多条编辑命令。例
如:
[root@oradb xd]# echo The tiger cubs will meet on Tuesday after school | sed -e 's/tiger/wolf/' -e 's/after/before/'
The wolf cubs will meet on Tuesday before school
但是很复杂,使用";"号功能
[root@oradb xd]# echo The tiger cubs will meet on Tuesday after school | sed 's/tiger/wolf/;s/after/before/'
The wolf cubs will meet on Tuesday before school


root@oradb xd]# echo The tiger cubs will meet on Tuesday after school | sed '
> s/tiger/wolf/
> s/after/before/'
The wolf cubs will meet on Tuesday before school
总结:用-e,或者“;”,或者延续提示符(通常为">")三种方法进行多条目编辑


全局替换
使用:'s/{old value}/{new value}/g' 


[root@oradb xd]# echo The tiger cubs will meet this Tuesday at the same time  as the meeting last Tuesday|sed 's/Tuesday/Monday/'
The tiger cubs will meet this Monday at the same time as the meeting last Tuesday


[root@oradb xd]# echo The tiger cubs will meet this Tuesday at the same time  as the meeting last Tuesday|sed 's/Tuesday/Monday/g'
The tiger cubs will meet this Monday at the same time as the meeting last Monday

sed 还可以用来修改记录字段分隔符。例如,以下命令将把所有的tab 修改为空格  sed 's// /g'
第一组斜线之间的项目是一个tab,而第二组斜线之间的项目是一个空格
创建名为t的文件
one 1  
two 1  
three 1  
one 1  
two 1  
two 1  
three 1

将t文本中two后面的1替换成2
[root@oradb xd]# sed '/two/ s/1/2/g' t
one 1
two 2
three 1
one 1
two 2
two 2

three 1

[root@oradb xd]# sed '
/two/ s/1/2/g
/three/ s/1/3/g' t
one 1
two 2
three 3
one 1
two 2
two 2
three 3
注:/要替换的文本位置 / s/
[root@oradb xd]# sed '
/two/ s/1/2/g
/three/ s/1/3/g' t > t1 
cat t1

进行行中文本的替换
[root@oradb xd]# sed '5,6 s/2/99/' t1
one 1
two 2
three 3
one 1
two 99
two 99
three 3

打印文件2到6行
[root@oradb xd]# sed -n '2,6p' t1
two 2
three 3
one 1
two 2
two 2

   3. 删除行
     用一个值替换另一个值远非流编辑器可以执行的唯一功能。它还具有许多的潜在功能,在我看来第二种最常用的功能是删除。删除与替换的工作方式相同,只是它删除指定的行(如果您想要删除一个单词而不是一行,不要考虑删除,而应考虑用空的内容来替换它—s/cat//)。格式:'{what to find} d'  
比如:t1文件中删除包含"two" 的所有行
[root@oradb xd]# sed '/two/ d' t1
one 1
three 3
one 1
three 3

删除2到3行
[root@oradb xd]# sed '2,3 d' t1
one 1
one 1
two 2
two 2
three 3

全局表达式时,特别是应用于删除操作时,有几点要记住:
上三角号(^) 表示一行的开始,因此,如果"two" 是该行的头三个字符,则
sed '/^two/ d' t1  将只删除该行。
[root@oradb xd]# sed '/^two/ d' t1
one 1
three 3
one 1
three 3
  
美元符号($) 代表文件的结尾,或一行的结尾,因此,如果 "two" 是该行的最后三个字符,则sed '/two$/ d' t1
[root@oradb xd]# sed '/two$/ d' t1
one 1
two 2
three 3
one 1
two 2
two 2
three 3
[root@oradb xd]# sed '/3$/ d' t1
one 1
two 2
one 1
two 2
two 2
将只删除该行。 将这两者结合在一起的结果:sed '/^$/ d' {filename} 删除文件中的所有空白行。
例如,以下命令将 "1" 替换为"2",以及将 "1" 替换为 "3",并删除文件中所有尾随的空行
[root@oradb xd]# sed '/two/ s/1/2/; /three/ s/1/3/; /^$/ d' t
one 1
two 2
three 3
one 1
two 2
two 2
three 3
将删除文件中所有的行,从第一行直到第一个空行sed '1,/^$/ d' {filename}


4.添加和插入文本
可以结合使用sed 和 "a"选项将文本添加到一个文件的末尾。实现方法如下:
[root@oradb xd]# sed '$a\
>This is where we test\
>the test' t
one 1
two 1
three 1
one 1
two 1
two 1
three 1
 This is where we test
the test
在该命令中,美元符号 ($) 表示文本将被添加到文件的末尾。反斜线 (\) 是必需的,它表示将插入一个回车符。如果它们被遗漏了,则将导致一个错误,显示该命令是错乱的;在任何要输入回车的地方您必须使用反斜线。 


要将这些行添加到第 4 和第 5 个位置而不是末尾:
[root@oradb xd]# sed '3a\
> This is where we stop\
> the test' t
one 1
two 1
three 1
This is where we stop
the test
one 1
two 1
two 1
three 1

这将文本添加到第 3 行之后。和几乎所有的编辑器一样,您可以选择插入而不是添加(如果您希望这样的话)。这两者的区别是添加跟在指定的行之后,而插入从指定的行开始。当用插入来代替添加时,只需用 "i" 来代替 "a",如下所示:
sed '3i\
> This is where we stop\
> the test' t
[root@oradb xd]# sed '3i\
> This is where we stop\
> the test' t
one 1
two 1
This is where we stop
the test
three 1
one 1
two 1
two 1
three 1

读写文件    
重定向输出的功能已经演示过了,但需要指出的是,在编辑命令运行期间可以同步地读入和写出文件。例如,执行替换,并将 1-3 行写到名称为 t2 的文件中:
[root@oradb xd]# sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 1,3 w t2' t
one 1
two 2
three 3
one 1
two 2
two 2
three 3
[root@oradb xd]# cat t2
one 1
two 2
three 3
或者
[root@oradb xd]# sed '/two/ s/1/2/;/three/ s/1/3/;1,3 w t2' t
one 1
two 2
three 3
one 1
two 2
two 2
three 3
[root@oradb xd]# cat t2
one 1
two 2
three 3
w (write) 命令指定了 "1,3",所以只有指定的行被写到了新文件中

5.修改命令    
除了替换项目之外,还可以将行从一个值修改为另一个值。要记住的是,替换是对字符逐个进行,而修改功能与删除类似,它影响整行:
[root@oradb xd]# sed '/two/ c\
> We are no longer using two' t
one 1
We are no longer using two
three 1
one 1
We are no longer using two
We are no longer using two
three 1

删除two字符串所在的行sed '/two/ d' t
[root@oradb xd]# sed '/two/ d' t
one 1
three 1
one 1
three 1
而要删除除包含单词 "two" 的行之外的所有行,则命令变为:sed '/two/ d' t
[root@oradb xd]# sed '/two/ !d' t
two 1
two 1
two 1
[root@oradb xd]# sed '/two/ !d' t1
two 2
two 2
two 2

提前退出    
sed 默认读取整个文件,并只在到达末尾时才停止。不过,您可以使用退出命令提前停止处理。只能指定一条退出命令,而处理将一直持续直到满足调用退出命令的条件
[root@oradb xd]# sed '
> /two/ s/1/2/
> /three/ s/1/3/
> 5q' t
one 1
two 2
three 3
one 1
two 2
或者
[root@oradb xd]# sed '/two/ s/1/2/;/three/ s/1/3/;5q' t
one 1
two 2
three 3
one 1
two 2

在退出命令之前的项目可以是一个行号(如上所示),或者一条查找/匹配命令:
[root@oradb xd]# sed '/two/ s/1/2/;/three/ s/1/3/;/three/q' t
one 1
two 2
three 3

还可以使用退出命令来查看超过一定标准数目的行,并增加比 head 中的功能更强的功能。例如,head 命令允许您指定您想要查看一个文件的前多少行—默认数为 10,但可以使用从 1 到 99 的任意一个数字。如果您想查看一个文件的前 110 行,您用 head 不能实现这一目的,但用 sed 可以:     sed 110q filename
[root@oradb xd]# sed 6q t
one 1
two 1
three 1
one 1
two 1
two 1
一些示例:
(1)sed -n 's/\(love\)able/\1rs/p' example-----love被标记为1,所有loveable会被替换成lovers,而且替换的行会被打印出来。
选定行的范围:逗号
(2)sed -n '/test/,/check/p' example-----所有在模板test和check所确定的范围内的行都被打印。
(3)sed -n '5,/^test/p' example-----打印从第五行开始到第一个包含以test开始的行之间的所有行。
(4)sed '/test/,/check/s/$/sed test/' example-----对于模板test和west之间的行,每行的末尾用字符串sed test替换。
从文件读入:r命令
 (5) sed '/test/r file' example-----file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面。

6.小结:
(1)常用选项:
-n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e∶直接在指令列模式上进行 sed 的动作编辑;
-f∶直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作;
-r∶sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
-i∶直接修改读取的档案内容,而不是由萤幕输出。
(2)常用命令
a :新增, a 的后面可以接字符串,而这些字符串会在新的一行出现(目前的下一行)
i  :插入, i 的后面可以接字符串,而这些字符串会在新的一行出现(目前的上一行)
d :删除,因为是删除啊,所以 d 后面通常不接任何输入
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运作
c :取代, c 的后面可以接字符串,这些字符串可以取代 n1,n2 之间的行! (全部取代)
s :取代,可以直接进行行取代的工作哩!通常这个 s 的动作可以搭配 正规表示法!例如 1,20s/old/new/g 就是啦! (选择性取代)


-------------------------------------------------------------------------------------------
整理自网络






你可能感兴趣的:(linux,sed)