`
omygege
  • 浏览: 1356772 次
文章分类
社区版块
存档分类
最新评论

sed 命令基本操作举例

 
阅读更多

1、替换

$ cat data1

this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog

The quick green elephant jumps over the fox dog


替换遇到的第一个匹配字符(默认)

$ sed 's/test/hello/' data1
this hello inside test
this hello inside test
this si inside hello
this si inside hello
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog


-n 禁止打印,命令p是打印修改后的行

$ sed -n 's/test/hello/p' data1
this hello inside test
this hello inside test
this si inside hello
this si inside hello


替换行内指定的匹配字符 2

$ sed -n 's/test/hello/2' data1
$ sed -n 's/test/hello/2p' data1
this test inside hello
this test inside hello


替换行内所有的匹配字符 g

$ sed -n 's/test/hello/gp' data1
this hello inside hello
this hello inside hello
this si inside hello
this si inside hello


$ cat /etc/passwd

zhongxuan:x:1001:1001:,,,:/home/zhongxuan:/bin/bash

用其他符号指定字符分节符 用 !作为分解符

$ sed -n 's!/home/zhongxuan!/yangzhongxuan!p' /etc/passwd
zhongxuan:x:1001:1001:,,,:/yangzhongxuan:/bin/bash
$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog

The quick green elephant jumps over the fox dog

文本模式匹配替换

$ sed -n '/green/s/fox/red/p' data1
The quick green elephant jumps over the red dog

$ sed -n '/fox/s/green/red/p' data1
The quick red elephant jumps over the fox dog

$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog


命令行组合

$ sed '2{
s/test/test_ch/
s/inside/outside/
}' data1
this test inside test
this test_ch outside test

$ cat data6
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.


2、插入

$ sed 'i\
quote> hello this is insert line 1' data6
hello this is insert line 1
This is line number 1.
hello this is insert line 1
This is line number 2.
hello this is insert line 1
This is line number 3.
hello this is insert line 1
This is line number 4.
hello this is insert line 1
This is line number 5.

$ sed '3i\
quote> you are ok insert the line 2.' data6
This is line number 1.
This is line number 2.
you are ok insert the line 2.
This is line number 3.
This is line number 4.

This is line number 5.


追加
$ sed '3a\
you are ok insert the line 2.' data6
This is line number 1.
This is line number 2.
This is line number 3.
you are ok insert the line 2.
This is line number 4.
This is line number 5.

$ sed '3i\
quote> you can insert more line.\
quote> you insert secnd line.\
quote> you insert third line.' data6
This is line number 1.
This is line number 2.
you can insert more line.
you insert secnd line.
you insert third line.
This is line number 3.
This is line number 4.
This is line number 5.


3、修改

$ sed '3c\
quote> you can change line with the sed command c' data6
This is line number 1.
This is line number 2.
you can change line with the sed command c
This is line number 4.
This is line number 5.

$ sed '/number 3/c\
quote> change line with txt mode' data6
This is line number 1.
This is line number 2.
change line with txt mode
This is line number 4.
This is line number 5.

$ sed '/number/c\
quote> chang lines that match the parrten' data6
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten

$ sed '2,3c\
quote> this line chang the 2 to 3 line of text.' data6
This is line number 1.
this line chang the 2 to 3 line of text.
This is line number 4.
This is line number 5.


4、写入文件

$ sed '2,3w tsetw' data6
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.

$ cat tsetw
This is line number 2.
This is line number 3.


5、读入文件

$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog

The quick green elephant jumps over the fox dog

指定行地址读入

$ sed '3r tsetw' data1
this test inside test
this test inside test
this si inside test
This is line number 2.
This is line number 3.
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog


指定匹配模式的行地址读入

$ sed '/dog/r tsetw' data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
This is line number 2.
This is line number 3.
The quick green elephant jumps over the fox dog
This is line number 2.
This is line number 3.
分享到:
评论

相关推荐

    shell编程和unix命令

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    UNIX Handbook

    三.sed命令 27 1.sed文本的定位方法: 27 2.sed编辑命令 27 3.sed打印文件的第二行 27 4.sed打印文件的第一到三行 27 5.sed打印匹配test的行 28 6.sed打印匹配$的行 28 7.sed打印最后一行:$是代表最后一行...

    CentOS.5系统管理-part1

    3.2 Shell和命令操作基础 3.2.1 Shell简介 3.2.2 命令操作基础 3.2.3 获得命令帮助 3.3 文件概述 3.3.1 什么是文件 3.3.2 文件的类型 3.4 文件与目录操作命令 3.4.1 目录操作命令 3.4.2 文件操作命令 3.4.3 文件打包...

    redhat linux教材20课程学习文档

    3.2 目录操作命令 cd 命令 pwd 命令 mkdir 命令 rmdir 命令 3.3 文件操作命令 ls 命令 touch 命令 cat 命令 more 和 less 命令 head 和 tail 命令 rm 命令 cp 命令 mv 命令 ln 命令 find 命令 file ...

    linux与unix shell编程指南.rar

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    Linux与unix shell编程指南

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    Linux与unix shell编程指南(1-16)

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    Linux与Unix Shell编程指南(PDF格式,共30章)

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    LINUX与UNIX_Shell编程指南(上)

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    Linux shell编程指南

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    LINUX与UNIX SHELL编程指南 高清PDF

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    LINUX与UNIX SHELL编程指南

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    linux与unix shell编程指南

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    linux与unix shell编程指南part2

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    linux shell 编程教程

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

    shell 编程指南pdf

    10.2.3 基本sed编辑命令 90 10.3 sed和正则表达式 91 10.4 基本sed编程举例 91 10.4.1 使用p(rint)显示行 91 10.4.2 打印范围 91 10.4.3 打印模式 92 10.4.4 使用模式和行号进行查询 92 10.4.5 匹配元字符 92 ...

Global site tag (gtag.js) - Google Analytics