sed简单用法

简介:

一、替换

1、将替换后的文本显示在屏幕上

1
sed  's/pattern/replace_string/'  file

或者通过管道

1
cat  file | sed  's/pattern/replace_string/'

2、替换源文件中的文本

1
sed  's/pattern/replace_string/g'  file

或者

1
sed  -i  's/pattern/replace_string/'  file

从第N处开始替换匹配的行

1
sed  's/pattern/replace_string/Ng'  file


二、删除匹配的行

1
sed  '/pattern/d'


三、用&来标记匹配的字段

1
2
3
#sed 's/mail/[&]/' sent |head -20
send [mail]  time
Mon Sep 29 03:16:49 CDT 2014


四、组合表达式

1
sed  'expression1' | sed  'expression2'

或者

1
sed  'expression1;expression2'

 

五、引用

1
2
3
# text=hello
# echo "hello world" |sed "s/$text/HELLO/"
HELLO world





本文转自 bannerpei 51CTO博客,原文链接:http://blog.51cto.com/281816327/1617273,如需转载请自行联系原作者

相关文章
|
1月前
|
Perl
sed的用法
sed的用法
27 2
|
2月前
|
Unix Perl
sed的具体用法
sed的具体用法
18 2
|
2月前
|
Perl
sed的基本语法
sed的基本语法
17 2
|
2月前
|
Perl
sed的复杂用法
sed的复杂用法
27 2
|
10月前
|
Perl
sed 基本用法
sed 基本用法
64 0
|
缓存 关系型数据库 MySQL