Sed的使用方法
来源:互联网 发布:linux vi覆盖文件内容 编辑:程序博客网 时间:2024/05/15 23:50
语法: sed [-hnv] [-e <script>] [-f <script 文件>] [文本文件]
参数:
-e <script> 以选项中指定的script来处理输入的文件
-f <script 文件> 以选项中指定的script文件来处理输入的文件
-h 显示帮助
-n --quiet或--silent 仅显示script处理后的结果.
-V 或 --version 显示版本信息
在参数中使用的script语法: [行号] [/查找字符串/命令 <参数>]
[行号] : [行号, 行号], 指定第一个行号和第二个行号之间的每一行
[行号!] 除此行之外的所有行
不指定行号,且没有指定/查找字符串/, 处理输入文件的每一行
a\<字符串> : 在指定的行后新增一行<字符串>
c\<字符串> : 以<字符串> 取代指定的行
d: 删除指定的行
i\<字符串> : 在指定的行前新增一行<字符串>
p: 显示指定的行
r<文本文件>: 先处理此处指定的文本文件, 然后处理命令行中所指定的文本文件
s/<查找字符串>/<取代字符串>/<取代方式>: 取代方式有3种
n: 取代第n个找到的<查找字符串>
g: 取代所有找到的<查找字符串>
p: 取代后,再显示一次此行
w<文本文件>: 在此文件填入指定字符
y/<查找字符>/<取代字符>/: <查找字符>和<取代字符>的长度必须相同
范例:
- 查找含有"target"的行,在后面新增一行,内容是" A New Line"
trace@realize:~/study/shell/sed$ cat sed_script /target/a\A new linetrace@realize:~/study/shell/sed$ cat textfile This is 1st lineThis is target lineThis is last linetrace@realize:~/study/shell/sed$ sed -f sed_script textfile This is 1st lineThis is target lineA new lineThis is last linetrace@realize:~/study/shell/sed$
- 将textfile中第1,2行的"is"取代为12
trace@realize:~/study/shell/sed$ sed -e 1,2s/is/12/g textfile Th12 12 1st lineTh12 12 target lineThis is last line
- 将textfile中第1,2行的"i"取代为1, "s"取代为2:
trace@realize:~/study/shell/sed$ sed -e 1,2y/is/12/ textfile Th12 12 12t l1neTh12 12 target l1neThis is last line
- 将textfile中第2,3行写入newfile文件中
trace@realize:~/study/shell/sed$ cat sed_script 1!w newfiletrace@realize:~/study/shell/sed$ sed -f sed_script textfile This is 1st lineThis is target lineThis is last linetrace@realize:~/study/shell/sed$ cat newfile This is target lineThis is last line
- Sed 的使用方法
- Sed的使用方法
- Sed 的使用方法
- sed的基本使用方法
- sed使用方法
- sed使用方法
- sed使用方法
- sed&awk基本使用方法
- sed&awk基本使用方法
- linux sed命令使用方法
- sed&awk基本使用方法
- shell: sed使用方法
- sed 详细使用方法
- sed&awk基本使用方法
- sed 工具基本使用方法
- sed的精华贴
- sed的神奇
- sed的使用
- 状态模式和策略模式
- boost serialization 32位库与64位库间二进制数据读写
- 迷茫的大学------李开复
- 表格控件GridPanel
- 人脸数据库总结
- Sed的使用方法
- Oracle TYPE OBJECT详解
- Linux下passwd和shadow文件内容详解
- 大学入党申请书范文
- Java操作XML(使用org.w3c.dom)1/3
- 为多种屏幕尺寸编写移动Flash内容
- 常用网页播放器代码
- Java操作XML(使用org.w3c.dom)2/3
- 挺立在孤独与失败的废墟上-----俞敏洪