Linux Shell笔记之sed

(转载:http://blog.csdn.net/hunterno4/article/details/17101021)


sed:流编辑器,stream editor

sed编辑器本身不会修改文本文件的数据,只会将修改后的数据发送到STDOUT
命令格式:sed options script file

一、sed基础
1.定义编辑器

# echo "this is a test" | sed 's/test/sed test/'           //将test替换为sed test
this is a sed test

2.同时使用多个编辑器
# echo "this is a test" | sed -e 's/test/sed test/;s/this/it/'
it is a sed test
执行多个命令,使用-e选项,命令间用分号隔开

3.从文件中读取编辑器命令
# cat sedtest1 
s/this/it/
s/test/sed test/
# cat data1 
this is a test
# sed -f sedtest1 data1            //使用-f选项指定文件
it is a sed test

4.sed替换选项
s/pattern/replacement/flags
1)数字替换标记
# cat cat
it is cat cat
it is cat cat
# sed 's/cat/dog/2' cat             //替换行中第二次出现的匹配模式
it is cat dog
it is cat dog

2)g,全局替换标记
# sed 's/cat/dog/g' cat             //全部替换
it is dog dog
it is dog dog

3)p,原来的行要打印出来
# sed -n 's/cat/dog/p' cat          //-n禁止sed编辑器输出,p只输出修改过的行,两者配合则只输出修改过的行
it is dog cat
it is dog cat

4)w,替换的结果保存到文件
# sed 's/cat/dog/w test' cat
it is dog cat
it is dog cat

5)替换字符
# sed 's!/bin/bash!/bin/csh!' passwd      //使用!号来当作字符串分隔符,使得路径名更容易读取与理解
root:x:0:0:root:/root:/bin/csh

5.sed使用地址
sed编辑器默认会作用于文本数据的所有行,若要指定某行或某些行,需要使用行寻址
# cat cat
it is cat cat
it is cat cat
it is cat 3
it is cat 4

1)数字方式的行寻址
# sed '2s/cat/dog/' cat                   //替换第二行中的匹配模式
it is cat cat
it is dog cat
# sed '2,3s/cat/dog/' cat                 //替换第二、三行的
it is cat cat
it is dog cat
it is dog 3
# sed '2,$s/cat/dog/' cat                 //$符号代码末尾,替换二至末尾的
it is cat cat
it is dog cat
it is dog 3
it is dog 4

2)文本模式过滤器
# sed '/3/s/cat/dog/' cat                  //使用正则表达式方式,只修改含有“3”的行
it is cat cat
it is cat cat
it is dog 3

6.删除行
# sed 'd' cat                               //不做任何匹配的话,d将删除所有
# sed '3d' cat                              //删除第三行
it is cat cat
it is cat cat
it is cat 4

# sed '/4/d' cat                            //删除含有“4”的行
it is cat cat
it is cat cat
it is cat 3

7.插入与附加文本
1)命令i在指定行前增加一个新行,命令a在指定行后附加一个新行
# echo "it is line 2"| sed 'i\it is line 1'       //i之后为右\符号
it is line 1
it is line 2

# echo "it is line 2"| sed 'a\it is line 1'
it is line 2
it is line 1

2)对指定行前插入或附加多行
# cat dataline 
line 1
line 2
line 3
# sed '1i\                                         //i前加数字,指定第几行
> it is line 1\                                    //插入多行每行的开头需加反斜线
> it is line 2' dataline
it is line 1
it is line 2
line 1
line 2
line 3

# sed '1a\
> it is line one\
> it is line two' dataline
line 1
it is line one
it is line two
line 2
line 3

8.修改行,机制同插入与附加
# sed '1c\it is line one' dataline
it is line one
line 2
line 3

9.转换命令
# sed 'y/123/789/' dataline                         //y命令转换,将123依次转换为789,长度需一致
line 7
line 8
line 9

10.打印
1)打印行
# sed -n '2,3p' dataline
line 2
line 3

2)打印行号
# sed '=' dataline                                   //=号命令打印行号
1
line 1
2
line 2
3
line 3

3)列出行
# cat datatab 
this    line    contains        tabs
# sed -n 'l' datatab                                  //l命令(list)可以显示制表符等特殊字符
this\tline\tcontains\ttabs$

11.sed处理文件
1)向文件写入,格式:[address]w filename
]# sed '1,2w test' dataline                           //w命令向文件写入数据,加-n可以不向控制台输出信息
line 1
line 2
line 3
# cat test 
line 1
line 2

2)从文件读取数据
# cat dataadd 
it is added line one
it is added line two
# sed '3r dataadd' dataline                            //r命令可以将一个文件中的数据插入到数据流中
line 1
line 2
line 3
it is added line one
it is added line two

二、sed进阶
模式空间(pattern space):是一块活动缓存区,在sed编辑器执行命令时它会保存sed编辑器要检验的文本
保持空间(hold space):另一块缓冲区域,可以在处理模式空间中其他行时用保持空间来临时保存一些行
sed编辑器会从脚本的顶部开始执行命令并一直处理到脚本的结尾(D命令例外)
1.next命令
1)单行next命令
# cat dataheader 
this is the header line

this is the first data line
this is the second data line

this is the last line
# sed '/header/{n;d}' dataheader                      //n命令使sed编辑器移动到数据流的下一文本行
this is the header line                               //找到header行,移到下一行,删除之
this is the first data line
this is the second data line

this is the last line

2)合并文本行
# sed '/first/{N;s/\n/ /}' dataheader                  //N命令将下一文本行加到已经在模式空间中的文本上
this is the header line

this is the first data line this is the second data line

this is the last line

2.多行删除命令
# cat dataheader 

this is the header line

this is the first data line
this is the second data line
# sed '/^$/{N;/header/D}' dataheader                    //D命令只删除模式空间中的第一行
this is the header line                                 //找到空白行,移到下一行,若匹配header,则用D命令删除第一行

this is the first data line
this is the second data line

3.多行打印命令
# cat dataroot 
the first meeting of the root
user group will be held on Tuesday.
All root user should attend this meeting
# sed -n 'N;/root\nuser/P' dataroot                     //P命令只会打印模式空间中的第一行
the first meeting of the root

4.保持空间
h            将模式空间复制到保持空间
H            将模式空间附加到保持空间
g            将保持空间复制到模式空间
G            将保持空间附加到模式空间
x            交换模式空间和保持空间的内容
# cat dataheader 
this is the header line
this is the first data line
this is the second data line
# sed -n '/first/{
> h                                  //将第一行复制到保持空间
> n                                  //读取下一行,并放到模式空间
> p                                  //打印模式空间的数据,即第二行
> g                                  //将保持空间复制到模式空间
> p                                  //打印模式空间的数据,即第一行
> }' dataheader
this is the second data line
this is the first data line

5.排除命令
!感叹号命令用来排除命令,即让原本会起作用的命令不起作用
# sed -n '/header/!p' dataheader             //除了header行,其它都不打印了
this is the first data line
this is the second data line

# sed -n '1!G;h;$p' dataheader              //反转数据,1!G,第一行时,不进行将保持空间附加到模式空间
this is the last line

this is the second data line
this is the first data line
this is the header line

# tac dataheader                             //tac命令执行cat命令的反向功能
this is the last line

this is the second data line
this is the first data line
this is the header line

6.改变流
1).跳转(branch)
格式:[address]b [label]                    //如果没有label,默认跳转到脚本的结尾
# sed '{/first/b jump1;s/this is the/no jump on/
> :jump1
> s/this is the/jump here on/}' dataheader
no jump on header line
jump here on first data line
no jump on second data line

no jump on last line

2)测试命令(test)
如果替换命令成功匹配了,测试命令会跳转到指定的标签
# sed '{
> s/first/matched/
> t                                          //如果第一个替换命令匹配成功,则替换,并跳过后面的命令
> s/this is the/no match on /                //如果第一个替换命令不匹配,则第二个命令会被执行
> }' dataheader
no match on  header line
this is the matched data line
no match on  second data line

no match on  last line

# echo "this, is, a, test, to, remove, commas"|sed -n '{
> :start                                           //标签以:号开头
> s/,//1p
> t start
> }'
this is, a, test, to, remove, commas
this is a, test, to, remove, commas
this is a test, to, remove, commas
this is a test to, remove, commas
this is a test to remove, commas
this is a test to remove commas

7.模式替换
1)and符号
# echo "the cat sleeps in the hat"|sed 's/.at/".at"/g'
the ".at" sleeps in the ".at"
# echo "the cat sleeps in the hat"|sed 's/.at/"&"/g'       //&符号用来代表替换命令中的匹配模式
the "cat" sleeps in the "hat"

2)替换单独的单词
# echo "the furry cat is pretty"|sed 's/furry .at /\1/'    //用圆括号来定义替换模式的子字符串,圆括号需转义
the cat is pretty                                             //\1代表第一个模块,\2代表第二个模块

8.在脚本中使用sed
1)使用包装脚本
# vi reverse
#!/bin/bash

sed -n '{
1!G
h
$p
}' $1                                           //使用第一个参数
# ./reverse dataheader 
this is the last line

this is the second data line
this is the first data line
this is the header line

三.sed实用工具
1.删除HTML标签

# cat index.html 
<html><body><h1>welcome to hunter's computer!</h1></body></html>
# sed 's/<[^>]*>//g;/^$/d' index.html            //s/<[^>]*>//g删除标签,/^$/d删除空白行

welcome to hunter's computer!






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: sed是一个强大的文本处理工具,常用于从标准输入或文件中读取文本,进行编辑和转换,并将输出写入标准输出或文件中。 以下是sed常用的命令选项: 1. -n:只显示经过sed编辑的行 2. -i:直接修改文件内容,不输出到终端 3. -e:允许多个命令一起执行 4. -f:从文件中读取sed命令进行处理 以下是sed常用的命令: 1. s:替换。语法为:s/要替换的内容/替换后的内容/g,其中g表示全局替换。 2. p:打印。将指定的行或所有行输出到终端。 3. d:删除。删除指定的行或所有行。 4. a:追加。在指定行后面添加新的内容。 5. i:插入。在指定行前面插入新的内容。 6. w:写入。将指定行写入到文件中。 举例说明: 1. 将文件中的所有"cat"替换为"dog":sed -i 's/cat/dog/g' file.txt 2. 只显示文件中包含“hello”的行:sed -n '/hello/p' file.txt 3. 删除文件中的第3行:sed -i '3d' file.txt 4. 在文件中第2行后面添加一行内容:“hello world”:sed -i '2a hello world' file.txt 5. 将文件中第1行替换为“hello”并保存到另一个文件中:sed '1s/.*/hello/' file.txt > newfile.txt ### 回答2: sed是一种常用的Linux命令,其全称为Stream Editor,用于对文本进行处理和转换。它可以在命令行中以各种方式对文件内容进行修改,包括替换、插入、删除、追加等操作。 sed的基本语法是`s/old/new/`,其中old表示待替换的字符串,new表示替换后的内容。sed命令会读取文件的内容,并对匹配到的old进行替换。如果需要对整个文件进行替换,则需要加上选项`-i`。 除了替换以外,sed还可以实现很多其他的文本操作。例如,通过`d`选项可以删除匹配到的行;通过`i`选项可以在指定行前插入文本;通过`a`选项可以在指定行后追加文本等。 另外,sed命令还可以使用正则表达式来进行模式匹配。通过在替换语句中使用正则表达式,可以对符合特定模式的字符串进行批量替换。 总之,sed是一种非常强大和灵活的文本处理工具,可以方便地对文件内容进行修改和转换,提高工作效率。熟练掌握sed命令可以帮助我们更好地处理和编辑文本文件。 ### 回答3: sed(Stream Editor)是Linux中常用的文本处理工具之一。它可以对文本进行流式编辑,具体操作包括查找、删除、替换、插入行或字符等。 使用sed命令的基本语法为: ``` sed [选项] '动作' 文件名 ``` 其中,选项可以包括: - -n:只显示或处理特定行; - -e:指定要执行的动作; - -f:从指定文件中读取动作。 动作部分可以包括: - 数字n:表示打印第n行; - p:打印匹配行; - d:删除匹配行; - s/old/new/:查找并替换文本; - a\text:在匹配行后插入文本; - i\text:在匹配行前插入文本。 以下是一些常用的sed命令示例: - 打印文件内容: ``` sed 'p' file.txt ``` - 删除匹配行: ``` sed '/pattern/d' file.txt ``` - 查找并替换文本: ``` sed 's/old/new/' file.txt ``` - 打印指定行数: ``` sed -n 'n' file.txt ``` - 在匹配行后插入文本: ``` sed '/pattern/a\text' file.txt ``` - 在匹配行前插入文本: ``` sed '/pattern/i\text' file.txt ``` 通过灵活运用sed命令,我们可以轻松地对文本文件进行各种编辑操作,提高工作效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值