★ 命令行参数简介
sed
-e script 指定sed编辑命令
-f scriptfile 指定的文件中是sed编辑命令
-n 寂静模式,抑制来自sed命令执行过程中的冗余输出信息,比如只
显示那些被改变的行。
不明白?不要紧,把这些肮脏丢到一边,跟我往下走,不过下面的介绍里
不包括正则表达式的解释,如果你不明白,可能有点麻烦。
★ 首先假设我们有这样一个文本文件 sedtest.txt
cat > sedtest.txt
Sed is a stream editor
----------------------
A stream editor is used to perform basic text transformations on an input stream
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
sed works by making only one pass over the input(s), and is consequently more
-----------------------------------------------------------------------------
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------
-
★ 输出指定范围的行 p other types of editors.
sed -e "1,4p" -n sedtest.txt
sed -e "/from/p" -n sedtest.txt
sed -e "1,/from/p" -n sedtest.txt
★ 在每一行前面增加一个制表符(^I)
sed "s/^/^I/g" sedtest.txt
注意^I的输入方法是ctrl-v ctrl-i
单个^表示行首
★ 在每一行后面增加--end
sed "s/$/--end/g" sedtest.txt
单个$表示行尾
★ 显示指定模式匹配行的行号 [/pattern/]=
sed -e '/is/=' sedtest.txt
1
Sed is a stream editor
----------------------
3
A stream editor is used to perform basic text transformations on an input stream
--------------------------------------------------------------------------------
While in some ways similar to an editor which permits scripted edits (such as ed
)
,
--------------------------------------------------------------------------------
-
-
7
sed works by making only one pass over the input(s), and is consequently more
-----------------------------------------------------------------------------
9
efficient. But it is sed's ability to filter text in a pipeline which particular
l
y
--------------------------------------------------------------------------------