
Sed is a stream editor. sed全称是:stream editor 流编辑器 对文件的操作无非就是”增删改查“,sed命令就是实现对文件的”增删改查“。
sed - stream editor for filtering and transforming text 用于过滤和转换文本的流编辑器
Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。
sed 本身是一个非常复杂的工具,有专门的书籍讲解 sed 的具体用法
作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一
sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据
vi 采用的是交互式文本编辑模式,你可以用键盘命令来交互性地插入、删除或替换数据中的文本。
sed 采用的是流编辑模式,最明显的特点是,在 sed 处理数据之前,需要预先提供一组规则,sed 会按照此规则来编辑数据。
sed 是一种非交互式编辑器(即用户不必参与编辑过程),它使用预先设定好的编辑指令对输入的文本进行编辑,完成之后再输出编辑结构
sed [-hnV] [ -e<script>] [-f<script文件>] [文本文件]sed [选项] [脚本命令] 文件名 选项部分,常见选项包括 -n,-e,-i,-f,-r选项。 脚本命令,常见包括: 增a 删d 改s(替换) 插入i 打印p 取代c
选项 含义
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~ c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行! d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚; i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~只输出被替换命令修改过的行
s :取代,可以直接进行取代的工作. s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
下面所有例子都以 test 文件为例子说明
hello world,hello hani.
hello world,hello hani.
hani is a good man,hani is handsome.
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/cccs/pattern/replacement/flags
flags 标记 功能
sed ‘s/old/new/’ test 匹配每一行的第一个old替换为new
sed 编辑器只替换每行中第 2 次出现的匹配模式
root@ubuntu-admin-a1:/home/sedTest# sed 's/hello/HELLO/2' test
hello world,HELLO hani.
hello world,HELLO hani.
hani is a good man,hani is handsome.
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/ccc替换所有匹配的字符串
root@ubuntu-admin-a1:/home/sedTest# sed 's/hani/HANI/g' test
hello world,hello HANI.
hello world,hello HANI.
HANI is a good man,HANI is handsome.
this is the test script of sed test.
linux sed
133456789
/aaa/bbb/ccc-n 选项会禁止 sed 输出,但 p 标记会输出修改过的行, 将二者匹配使用的效果就是只输出被替换命令修改过的行
root@ubuntu-admin-a1:/home/sedTest# sed -n 's/hani/HANI/p' test
hello world,hello HANI.
hello world,hello HANI.
HANI is a good man,hani is handsome.sed ‘s/old/new/’ test 匹配每一行的第一个old替换为new sed ‘s/old/new/gi’ test 匹配所有old替换为new,g 代表一行多个,i 代表匹配忽略大小写 sed ‘3,9s/old/new/gi’ test 匹配第 3~9 行所有old替换为new
sed -i 's/\r//' test\r \n 回车换行符详解 \r \n 回车换行符详解_Hani_97的博客-CSDN博客_回车换行符
格式为: [#]d
root@ubuntu-admin-a1:/home/sedTest# sed 'd' test root@ubuntu-admin-a1:/home/sedTest# sed '3d' test
hello world,hello hani.
hello world,hello hani.
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/cccroot@ubuntu-admin-a1:/home/sedTest# sed '1,4d' test
linux sed
123456789
/aaa/bbb/ccca 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行, 它们的基本格式完全相同
格式为: [#]a(或 i)\新文本内容
root@ubuntu-admin-a1:/home/sedTest# sed '3i you are beautiful' test
hello world,hello hani.
hello world,hello hani.
you are beautiful
hani is a good man,hani is handsome.
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/cccroot@ubuntu-admin-a1:/home/sedTest# sed '3a you are more beautiful' test
hello world,hello hani.
hello world,hello hani.
hani is a good man,hani is handsome.
you are more beautiful
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/cccroot@ubuntu-admin-a1:/home/sedTest# sed '4i hani\
hani hani\
hani hani hani' test
hello world,hello hani.
hello world,hello hani.
hani is a good man,hani is handsome.
hani
hani hani
hani hani hani
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/cccroot@ubuntu-admin-a1:/home/sedTest# sed '3c you are beautiful' test
hello world,hello hani.
hello world,hello hani.
you are beautiful
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/cccy 转换命令是唯一可以处理单个字符的 sed 脚本命令 格式 [address]y/inchars/outchars/
p 命令表示搜索符号条件的行,并输出该行的内容 格式为: [address]p
root@ubuntu-admin-a1:/home/sedTest# sed -n '/hani/p' test
hello world,hello hani.
hello world,hello hani.
hani is a good man,hani is handsome.sed ‘2p’ test 重复打印第 2 行 sed ‘1,3p’ test 重复打印第1~3行 sed -n ‘2p’ test 只打印第 2 行 sed -n ‘1,3p’ test 只打印第 1~3 行 sed -n ‘/hani/p’ test 打印匹配到 hani 的行,类似grep sed -n ‘/hani/!p’ test ! 反选,打印没有匹配到 hani 的行 sed -n ‘s/old/new/gp’ test 只打印匹配替换的行
w 命令用来将文本中指定行的内容写入文件中 格式如下: [address]w filename
w 标记会将匹配后的结果保存到指定文件中
root@ubuntu-admin-a1:/home/sedTest# sed 's/hani/HANI/w another' test
hello world,hello HANI.
hello world,hello HANI.
HANI is a good man,hani is handsome.
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/ccc
root@ubuntu-admin-a1:/home/sedTest# ls
another test
root@ubuntu-admin-a1:/home/sedTest# cat another
hello world,hello HANI.
hello world,hello HANI.
HANI is a good man,hani is handsome.q 命令的作用是使 sed 命令在第一次匹配任务结束后,退出 sed 程序,不再进行对后续数据的处理。
root@ubuntu-admin-a1:/home/sedTest# sed '1q' test
hello world,hello hani.定址用于决定对哪些行进行编辑。 地址的形式可以是数字、正则表达式、或二者的结合。 如果没有指定地址,sed将处理输入文件的所有行。
如果只想将命令作用于特定行或某些行,则必须写明 address 部分,表示的方法有以下 2 种: 以数字形式指定行区间; 用文本模式指定具体行区间。
root@ubuntu-admin-a1:/home/sedTest# sed '1,4s/hani/HANI/' test
hello world,hello HANI.
hello world,hello HANI.
HANI is a good man,hani is handsome.
this is the test script of sed test.
linux sed
123456789
/aaa/bbb/ccc
其中,sed 's/hani/HANI/' test 默认所有都替换sed 允许指定文本模式来过滤出命令要作用的行 格式: /pattern/command
这里是最常用的几个操作例子:
功能:模式匹配语言
文本和数据进行处理;~(匹配)和~!(不匹配);选项:
示例:
假设文件1.txt的文本如下(可以利用touch命令和vi命令组合创建):
1. hello world
2. wold hello
3. nothing is important
4. i like movie movie
hello world
wold hello
nothing is important
i like movie movieawk 命令可以用来过滤文本,简单理解就是对文本的每行执行命令awk指定的脚本,它的基本命令格式如下:
awk 'BEGIN{ commands } pattern{ commands } END{ commands }' file其中:
'BEGIN{ commands } 指定最开始执行的脚本
pattern{ commands } 对文件的每一行遍历,判断是否满足pattern的模式,如果满足则执行脚本
打印“开始”,打印每行,打印“结束”: awk 'BEGIN{ print "开始" } { print } END{ print "结束" }' 1.txt
打印每行的行号:awk '{print NR}' 1.txt
打印每行的文本:awk '{print $0}' 1.txt
打印每行的第1列(默认用空格分离):awk '{print $1}' 1.txt
打印每行的最后1列(默认用空格分离):awk '{print $NF}' 1.txt
打印每行的倒数第2列(默认用空格分离):awk '{print $(NF-1)}' 1.txt
打印每行,并为每行带上行号:awk '{print NR":",$0}' 1.txt
打印含有序号的行:awk '/\d./ {print}' 1.txt
END{ commands } 指定最后执行的脚本grep作用
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。
grep全称是Global Regular Expression Print,表示全局正则表达式版本。
它的使用权限是所有用户。grep格式
grep [选项] ”模式“ [文件]-E :开启扩展(Extend)的正则表达式。
-i :忽略大小写(ignore case)。
-v :反过来(invert),只打印没有匹配的,而匹配的反而不打印。
-n :显示行号
-w :被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker
-c :显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。
-o :只显示被模式匹配到的字符串。
--color :将匹配到的内容以颜色高亮显示。
-A n:显示匹配到的字符串所在的行及其后n行,after
-B n:显示匹配到的字符串所在的行及其前n行,before
-C n:显示匹配到的字符串所在的行及其前后各n行,context
1、直接输入要匹配的字符串,这个可以用fgrep(fast grep)代替来提高查找速度,比如我要匹配一下hello.c文件中printf的个数:grep -c "printf" hello.c
2、使用基本正则表达式,下面谈关于基本正则表达式的使用:
匹配字符:
. :任意一个字符。
[abc] :表示匹配一个字符,这个字符必须是abc中的一个。
[a-zA-Z] :表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。
[^123] :匹配一个字符,这个字符是除了1、2、3以外的所有字符。
对于一些常用的字符集,系统做了定义:
[A-Za-z] 等价于 [[:alpha:]]
[0-9] 等价于 [[:digit:]]
[A-Za-z0-9] 等价于 [[:alnum:]]
tab,space 等空白字符 [[:space:]]
[A-Z] 等价于 [[:upper:]]
[a-z] 等价于 [[:lower:]]
标点符号 [[:punct:]]
匹配次数:
\{m,n\} :匹配其前面出现的字符至少m次,至多n次。 \? :匹配其前面出现的内容0次或1次,等价于\{0,1\}。 * :匹配其前面出现的内容任意次,等价于\{0,\},所以 ".*" 表述任意字符任意次,即无论什么内容全部匹配。
位置锚定:
^ :锚定行首
:锚定行尾。技巧:"^
\b或\<:锚定单词的词首。如"\blike"不会匹配alike,但是会匹配liker
\b或\>:锚定单词的词尾。如"\blike\b"不会匹配alike和liker,只会匹配like
\B :与\b作用相反。
分组及引用:
\(string\) :将string作为一个整体方便后面引用
\1 :引用第1个左括号及其对应的右括号所匹配的内容。
\2 :引用第2个左括号及其对应的右括号所匹配的内容。
\n :引用第n个左括号及其对应的右括号所匹配的内容。
grep常用例子
-例1 在文件中查找模式(单词)
在/etc/passwd文件中查找单词“linuxtechi”
grep linuxtechi /etc/passwd-例2 在多个文件中查找模式。
grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow-例3 使用-l参数列出包含指定模式的文件的文件名。
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
/etc/passwd
/etc/shadow
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab-例4 使用-n参数,在文件中查找指定模式并显示匹配行的行号
grep -n root /etc/passwd /etc/shadow
-例5 使用-v参数输出不包含指定模式的行
输出/etc/passwd文件中所有不含单词“linuxtechi”的行
grep -v linuxtechi /etc/passwd-例6 使用 ^ 符号输出所有以某指定模式开头的行
Bash脚本将 ^ 符号视作特殊字符,用于指定一行或者一个单词的开始。
例如输出/etc/passes文件中所有以“root”开头的行
root@Linux-world:~# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
root@Linux-world:~#-例7 使用 $ 符号输出所有以指定模式结尾的行。
输出/etc/passwd文件中所有以“bash”结尾的行。
root@Linux-world:~# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#
Bash脚本将美元($)符号视作特殊字符,用于指定一行或者一个单词的结尾。-例8 使用 -r 参数递归地查找特定模式
root@Linux-world:~# grep -r linuxtechi /etc/
/etc/subuid:linuxtechi:100000:65536
/etc/group:adm:x:4:syslog,linuxtechi
/etc/group:cdrom:x:24:linuxtechi
/etc/group:sudo:x:27:linuxtechi
/etc/group:dip:x:30:linuxtechi
/etc/group:plugdev:x:46:linuxtechi
/etc/group:lpadmin:x:115:linuxtechi
/etc/group:linuxtechi:x:1000:
/etc/group:sambashare:x:131:linuxtechi
/etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
............................................................................
上面的命令将会递归的在/etc目录中查找“linuxtechi”单词-例9 使用 grep 查找文件中所有的空行
root@Linux-world:~# grep ^$ /etc/shadow
root@Linux-world:~#
由于/etc/shadow文件中没有空行,所以没有任何输出-例10 使用 -i 参数查找模式
grep命令的-i参数在查找时忽略字符的大小写。
我们来看一个例子,在paswd文件中查找“LinuxTechi”单词。
nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd
linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash
nextstep4it@localhost:~$-例11 使用 -e 参数查找多个模式
例如,我想在一条grep命令中查找‘linuxtechi’和‘root’单词,使用-e参数,我们可以查找多个模式。
root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
root@Linux-world:~#-例12 使用 -f 用文件指定待查找的模式
首先,在当前目录中创建一个搜索模式文件“grep_pattern”,我想文件中输入的如下内容。
root@Linux-world:~# cat grep_pattern
^linuxtechi
root
false$
root@Linux-world:~#
现在,试试使用grep_pattern文件进行搜索
root@Linux-world:~# grep -f grep_pattern /etc/passwd
-例13 使用 -c 参数计算模式匹配到的数量
继续上面例子,我们在grep命令中使用-c命令计算匹配指定模式的数量
root@Linux-world:~# grep -c -f grep_pattern /etc/passwd
22
root@Linux-world:~#-例14 输出匹配指定模式行的前或者后面N行
a)使用-B参数输出匹配行的前4行
root@Linux-world:~# grep -B 4 "games" /etc/passwdb)使用-A参数输出匹配行的后4行
root@Linux-world:~# grep -A 4 "games" /etc/passwdc)使用-C参数输出匹配行的前后各4行
root@Linux-world:~# grep -C 4 "games" /etc/passwd