shell 脚本-sed工具

sed 是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲

区中,称为 “模式空间” (pattern space),接着用 sed命令处理缓冲区中的内容,处理完成后,

把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并

没有 改变,除非你使用重定向存储输出。 Sed主要用来自动编辑一个或多个文件;简化对文

件的反复操作;

基本使用:

1. /pattern/p :打印匹配pattern的行

使用 p命令需要注意 ,sed 是把待处理文件的内容连同处理结果一起输出到标准输出的 ,因此

p命令 表示除了把文件内容打印出来之外还额外打印一遍匹配 pattern的行。比如一个文

件log的内 容是

要想只输出处理结果 ,应加上 -n选项 ,这种用法相当于 grep命令

[[email protected] sed]$ sed ‘/ /p‘ file
11111111111111111111
2222222222222222222
3333333333333333333
44444444444444444
5555555555555555555
[[email protected] sed]$ sed ‘/*/p‘ file
11111111111111111111
2222222222222222222
3333333333333333333
44444444444444444
5555555555555555555
[[email protected] sed]$ sed ‘/1/p‘ file
11111111111111111111
11111111111111111111
2222222222222222222
3333333333333333333
44444444444444444
5555555555555555555
[[email protected] sed]$ sed ‘/.*/p‘ file
11111111111111111111
11111111111111111111
2222222222222222222
2222222222222222222
3333333333333333333
3333333333333333333
44444444444444444
44444444444444444
5555555555555555555
5555555555555555555
[[email protected] sed]$ sed -n  ‘/.*/p‘ file
11111111111111111111
2222222222222222222
3333333333333333333
44444444444444444
5555555555555555555

2. /pattern/d :删除匹配pattern的行

注意,sed命令不会修改原文件,删除命令只表示某些行不打印输出 ,而不是从原文件中删去。

[[email protected] sed]$ sed ‘/11*/d‘ file
2222222222222222222
3333333333333333333
44444444444444444
5555555555555555555

3. /pattern/s/pattern1/pattern2/ :查找符合pattern的行 ,将该行第一个匹配

pattern1的字符串替换为pattern2

4. /pattern/s/pattern1/pattern2/g :查找符合pattern的行 ,将该行所有匹配

pattern1的字符串替换为pattern2

[[email protected] sed]$ cat file
aabbccdd
aabbaabb
aabbaabb
[[email protected] sed]$ sed ‘/aa*/s/aa/ff/‘ file
ffbbccdd
ffbbaabb
ffbbaabb
[[email protected] sed]$ sed ‘/aa*/s/aa/ff/g‘ file
ffbbccdd
ffbbffbb
ffbbffbb
[[email protected] sed]$ sed ‘/a/s/a/-f-/g‘ file
-f--f-bbccdd
-f--f-bb-f--f-bb
-f--f-bb-f--f-bb

[[email protected] sed]$ sed ‘/^[a-z]\+/s/\([a-c]\+\)/\1--/‘ file
aabbcc--dd
aabbaabb--
aabbaabb--

pattern2中的& 表示原文件的当前行中与pattern1匹配的字符串

[[email protected] sed]$ sed ‘/^[a-z]\+/s/\([a-c]\+\)/\-&-/‘ file
-aabbcc-dd
-aabbaabb-
-aabbaabb-

pattern2中的\1 表示与pattern1的第一个 ()括号相匹配的内容 ,\2表示与 pattern1的

第二个()括号 相匹配的内容。

sed默认使用Basic正则表达式规范,如果指定了-r选项则使用Extended规范 ,那 么 ()括号就不必转义了

[[email protected] sed]$ cat f1
<html><head><title>Hello World</title>
<body>Welcome to the world of regexp!</body></html>

sed/grep 匹配 是贪心的 总是匹配最长的 所以下面方法是错误的

下面才是去除<。。。>的正确方法   用[]做限定 防止<>中间再有< 或> 把整句都匹配了

5. sed -i : 做的操作会修改原文件

[[email protected] sed]$ cat f2
11111111111
22222222222
33333333333
[[email protected] sed]$ sed ‘$d‘ f2
11111111111
22222222222
[[email protected] sed]$ cat f2
11111111111
22222222222
33333333333
[[email protected] sed]$ sed -i ‘$d‘ f2
[[email protected] sed]$ cat f2
11111111111
22222222222

6. 定址

定址用于决定对哪些行进行编辑。地址的形式可以是数字、正则表达式、或二者的结合。

如果没有指定地址, sed将处理输入文件的所有行。

sed -n ‘3p’ file #打印第三行

sed -n ‘100,300p’ file # 打印100~ 300行的信息(包括 100和 300)

地址是逗号分隔的,那么需要处理的地址是这两行之间的范围(包括这两行在内)。

围可以用数字、正则表达式、或二者的组合表示。

sed ‘2,5d’ file #删除第二行到第五行

sed ‘/start/ ,/end/d’ file # 删除包含’start’行和 ’end’行之间的行

sed ‘/start/, 10d’ file # 删除包含’start’ 的行到第十行的内容

等等,例子如下:

[[email protected] sed]$ cat f2
11111111111
start
22222222222
33333333333
44444444444444
555555555
end
66666666
7777777

[[email protected] sed]$ sed -n ‘/start/,8p‘ f2
start
22222222222
33333333333
44444444444444
555555555
end
66666666
[[email protected] sed]$ sed -n ‘/start/,/end/p‘ f2
start
22222222222
33333333333
44444444444444
555555555
end
[[email protected] sed]$ sed -n ‘4p‘ f2
33333333333
[[email protected] sed]$ sed -n ‘2,4p‘ f2
start
22222222222
33333333333

[[email protected] sed]$ seq 6 | sed -n ‘2,4p‘
2
3
4

’0~3p‘表示 从0开始 每次行号加3的行   ~表示每次行号加几

[[email protected] sed]$ seq 6 | sed -n  ‘0~3p‘
3
6
[[email protected] sed]$ seq 6 | sed -n  ‘0~2p‘
2
4
6
[[email protected] sed]$ seq 6 | sed -n  ‘0~1p‘
1
2
3
4
5
6

7. 命令和选项

sed命令告诉sed 如何处理由地址指定的各输入行,如果没有指定地址则处理所有的输入

行。

命令

a\ :在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用 “\”续行

[[email protected] sed]$ sed   ‘2a nihao \hello‘ f3
11111111111
22222222222
nihao hello
33333333333
44444444444

c\ :用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用 ”\"

续行

i\ :在当前行之前插入文本。多行时除最后一行外,每行末尾需用 ”\"续行 d删除行

[[email protected] sed]$ sed   ‘2i nihao \hello‘ f3
11111111111
nihao hello
22222222222
33333333333
44444444444

h : 把模式空间里的内容复制到暂存缓冲区

H :把模式空间里的内容追加到暂存缓冲区

g :把暂存缓冲区里的内容复制到模式空间,覆盖原有的内容

G:把暂存缓冲区的内容追加到模式空间里,追加在原有内容的后面

l :列出非打印字符

p :打印行

n :读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理

q :结束或退出sed

r :从文件中读取输入行

! : 对所选行以外的所有行应用命令

s :用一个字符串替换另一个

g :在行内进行全局替换

w :将所选的行写入文件

x :交换暂存缓冲区与模式空间的内容

y :将字符替换为另一字符(不能对正则表达式使用 y命令)

选项

-e :进行多项编辑,即对输入行应用多条 sed命令时使用

[[email protected] sed]$ seq 6 | sed  -e ‘1d‘ -e ‘5d‘
2
3
4
6

-n :取消默认的输出

-f :指定sed 脚本的文件名

8. 退出状态

sed不向grep 一样,不管是否找到指定的模式,它的退出状态都是 0。只有当命令存在语法

错误时, sed的退出状态才不是 0。



正则表达式 练习

sed使用的正则表达式是括在斜杠线 "/"之间的模式。如果要把正则表达式分隔符 "/"改为另一个字符,比如 o,只要在这个字符前加一个反斜线,

在字符后跟上正则表达式,再跟上这个字符即可。例如: sed -n ‘\o^Myop‘ datafile

[[email protected] sed]$ sed -n ‘\u^myup‘ log
my1111111

^:行首定位符:/^my/ 匹配所有以 my开头的行

[[email protected] sed]$ cat log
111111111
my1111111
11my
111111111my
3333333333
[[email protected] sed]$ sed -n ‘/^my/p‘ log
my1111111

$:行尾定位符:/my$/ 匹配所有以 my结尾的行

[[email protected] sed]$ sed -n ‘/my$/p‘ log
11my
111111111my


.:匹配除换行符以外的单个字符: /m..y/ 匹配包含字母 m,后跟两个任意字符,再跟字母y的行

[[email protected] sed]$ sed -n ‘/1..y/p‘ log
11my
111111111my

*:匹配零个或多个前导字符: /test*/ 匹配包含 string tes,后跟零个或多个 t字母的行

[[email protected] sed]$ sed -n  ‘/test*/p‘ f4
test
testy
tesa
tests
testttt
[[email protected] sed]$ cat f4
test
testy
tesa
tests
testttt
tea
tebs

[]:匹配指定字符组内的任一字符 : /t[eE]st/ 匹配包含 test或 tEst的行

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
tfst
[[email protected] sed]$ sed -n ‘/t[eE]st/p‘ f4
test
tEsta
tEstsad

[^]:匹配不在指定字符组内的任一字符: /t[^eE]st/ 匹配 string 以 t开头,但 st之前的

那个字符不是 e或 E的行

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
tfst
[[email protected] sed]$ sed -n ‘/t[^eE]st/p‘ f4
tast
tfst

\(..\):保存已匹配的字符: 标记元字符之间的模式,并将其保存为标签 1,之后可以使用

\1来引用它。最多可以定义 9个标签,从左边开始编号,最左边的是第一个。此例中,对第

1到第3 行进行处理, tes被保存为标签 1,如果发现 tes或tEs,则替换为 tes-或tEs。

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
tfst
[[email protected] sed]$ sed  ‘1,3s/\(t[Ee]s\)/\1-/g‘ f4
tes-t
tebs
tEs-ta
tEstsad
tast
tfst

&:保存查找串以便在替换串中引用: s/test/*&*/g 符号& 代表查找串。test将被替换为

*test*

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
tfst
[[email protected] sed]$ sed ‘s/t[eE]st/*&*/g‘ f4
*test*
tebs
*tEst*a
*tEst*sad
tast
tfst


\<:词首定位符:/\<my/ 匹配包含以 my开头的单词的行

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
tfst
[[email protected] sed]$ sed -n ‘/\<te/p‘ f4
test
tebs


\>:词尾定位符:/my\>/ 匹配包含以 my结尾的单词的行

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
tfst
[[email protected] sed]$ sed -n ‘/st\>/p‘ f4
test
tast
tfst


x\{m\}:连续m 个x :/m\{5\}/ 匹配包含连续5个 m的行

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
tfst
OOOOmmmmmmmmmUUUU
[[email protected] sed]$ sed -n ‘/\m\{5\}/p‘ f4
OOOOmmmmmmmmmUUUU

x\{m,\}:至少m 个x :/m\{5,\}/ 匹配包含至少连续 5个 m的行

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
mmmmm
mmmmmm
OOOOmmmmmmmmmUUUU
mmmm
[[email protected] sed]$ sed -n ‘/m\{5,\}/p‘ f4
mmmmm
mmmmmm
OOOOmmmmmmmmmUUUU


x\{m,n\}:至少m 个,但不超过n个 x: /m\{5,7\}/ 匹配包含连续 5到 7个 m的行

[[email protected] sed]$ cat f4
test
tebs
tEsta
tEstsad
tast
mmmmm
mmmmmm
OOOOmmmmmmmmmUUUU
mmmm
[[email protected] sed]$ sed -n ‘/m\{5,7\}/p‘ f4
mmmmm
mmmmmm
OOOOmmmmmmmmmUUUU 【注意   这个还是会匹配到的】

模式空间保持空间(高级用法,难度较高)

sed在正常情况下,将处理的行读入模式空间( pattern space),脚本中的“sedcommand (sed命令)” 就一条接着一条进行处理,直到脚本执行完毕。然后该行呗输出,模式( pattern space)被清空;接着,在重复执行刚才的动作,文件中的

新的一行被读入,直到文件处理完毕。

一般情况下,数据的处理只使用模式空间( pattern space),按照如上的逻辑即可

完成主要任务。但是某些时候,使用通过使用保持空间( hold space),还可以带

来意想不到的效果。

模式空间:可以想成工程里面的流水线,数据之间在它上面进行处理。

保持空间:可以想象成仓库,我们在进行数据处理的时候,作为数据的暂存区域。

正常情况下,如果不显示使用某些高级命令,保持空间不会使用到!

小写命令   表示 清除

大写的命令  表示 追加

+ g :[address[,address]]g 将hold space 中的内容拷贝到 pattern space中,

原来pattern space里的内容清除

+ G :[address[,address]]G 将hold space 中的内容append到 pattern

space\n

+ h :[address[,address]]h 将pattern space 中的内容拷贝到 hold space中,

原来的hold space里的内容被清除

+ H :[address[,address]]H 将pattern space 中的内容append到 hold

space\n

+ d :[address[,address]]d 删除pattern中的所有行,并读入下一新行到

pattern中

+ D: [address[,address]]D 删除multiline pattern 中的第一行,不读入下一行

+ x:交换保持空间和模式空间的内容

例子:

1. 给每行结尾添加一行空行

[[email protected] sed]$ cat f5
0 hello
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello
[[email protected] sed]$ sed ‘G‘ f5                      【模式空间 为空  按行处理 追加到模式空间 就相当于追加空行  】
0 hello

1 hello

2 hello

3 hello

4 hello

5 hello

6 hello

7 hello

[[email protected] sed]$

2. 用sed 模拟出tac的功能(倒序输出)

[[email protected] sed]$ seq 10 > f6
[[email protected] sed]$ cat f6
1
2
3
4
5
6
7
8
9
10

先理解这个

[[email protected] sed]$ sed ‘G;h;‘ f6         【先将保持空间的 追加到模式空间 (第一次是空 所以1后面是空 ) 然后是将模式空间的(1  空 )拷贝到保持空间   输出    下一次从保持空间 到 模式空间追加的就是 (1 空 )  所以第二次为 2 1 空   以此类推】

1

2
1

3
2
1

4
3
2
1

5
4
3
2
1

6
5
4
3
2
1

7
6
5
4
3
2
1

8
7
6
5
4
3
2
1

9
8
7
6
5
4
3
2
1

10
9
8
7
6
5
4
3
2
1

再理解这个

[[email protected] sed]$ sed ‘G;h;2,3!d‘ f6                【2,3!d】表示按行删除时第二次和第三次处理时候 不删除模式空间的所有行
2
1

3
2
1

最后理解这个

1!G第1 行不执行“G”命令,从第 2行开始执行。 $!d,最后一行不删除(保留最后 1行)

[[email protected] sed]$ sed ‘1!G;h;$!d‘ f6       【1!G】表示再追加的时候 第一次按行处理是不追加保持空间的空行到模式空间
10
9
8
7
6
5
4
3
2
1

3. 追加匹配行到文件结尾

[[email protected] sed]$ cat f5
0 hello
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello
[[email protected] sed]$ sed -e ‘/[234].*/H‘ -e ‘$G‘ f5
0 hello
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello

2 hello
3 hello
4 hello

4. 行列转化

[[email protected] sed]$ cat f6
1
2
3
4
5
6
7
8
9
10
[[email protected] sed]$ sed -n  ‘H;${x;s/\n/-/g;p}‘ f6
-1-2-3-4-5-6-7-8-9-10                             【说明 每行显示 是最前面有一个\n被替换】
[[email protected] sed]$ sed -n  ‘H;${x;s/\n//g;p}‘ f6
12345678910
[[email protected] sed]$ sed -n  ‘H;${x;s/\n/ /g;p}‘ f6
 1 2 3 4 5 6 7 8 9 10

H表示把pattern space 的内容追加到hold space中去, H可以带一个地址,这里

用的是$,表示到文件的末尾,然后用 x将之取到 pattern space中,把 \n替换成空格再打印即可。


5.同上,求1 100的求和

[[email protected] sed]$ seq 100 | sed -n ‘H;${x;s/\n/+/g;s/^+//;p}‘
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[[email protected] sed]$ seq 100 | sed -n ‘H;${x;s/\n/+/g;s/^+//;p}‘|bc
5050


6. 打印奇偶数行

[[email protected] sed]$ cat f6
1
2
3
4
5
6
[[email protected] sed]$ sed -n ‘n;p‘ f6            【‘n;p‘    n是清除当前行 读取下一行】
2
4
6
[[email protected] sed]$ sed -n ‘p;n‘ f6
1
3
5
[[email protected] sed]$ sed -n ‘n;n;p‘ f6
3
6

7.再求1 ~100求和

先看这个 

[[email protected] sed]$ seq 10 | sed ‘:a;N;s/\n/+/;‘                【N 表示多读取一行】
1+2
3+4
5+6
7+8
9+10

再看这个

[[email protected] sed]$ seq 10 | sed ‘:a;N;s/\n/+/;ba‘
1+2+3+4+5+6+7+8+9+10
[[email protected] sed]$ seq 10 | sed ‘:c;N;s/\n/+/;bc‘
1+2+3+4+5+6+7+8+9+10
[[email protected] sed]$ seq 100 | sed ‘:c;N;s/\n/+/;bc‘|bc
5050
[[email protected] sed]$ seq 100 | sed ‘:f;N;s/\n/+/;bf‘|bc
5050

:a表示标签a ,ba 表示跳转到a标签, $表示最后一行,!表示不做后续操作,所

以,$!ba表示最后一行不用跳转到 a标签,结束此次操作。

相关博客:

讨论:互换模式空间和保持空间的内容

http://blog.chinaunix.net/uid-10540984-id-316081.html

http://oldboy.blog.51cto.com/2561410/767862/

http://blog.chinaunix.net/uid-9950859-id-98222.html

http://czmmiao.iteye.com/blog/1899880

http://blog.sina.com.cn/s/blog_46ecf5890100jtip.html (vi和 sed结合)

时间: 2024-08-23 23:06:34

shell 脚本-sed工具的相关文章

shell脚本 sed工具练习

1. Sed简介           sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕.接着处理下一行,这样不断重复,直到文件末尾.文件内容并没有 改变,除非你使用重定向存储输出.Sed主要用来自动编辑一个或多个文件:简化对文件的反复操作:编写转换程序等.以下介绍的是Gnu版本的Sed 3.02.   2. 定址         

shell脚本--sed工具

sed sed:它叫做流式编辑器,它的工作场景一般是在编辑器中,把一段文本按行读入sed中然后按指定的方式输出,这是它最基本的用法. 一.sed的基本操作: 1./pattern/action pattern为正则表达式,对满足pattern的行作action操作 例如 这条命令和grep基本一样,其中-n的意思是只将执行后面指令的显示出来,如果不加-n选项则会把所有信息显示以便并且把匹配到的多显示一遍,p的意思是打印,如果改成d则是删除.-i的话则会修改源文件 2./pattern/s/pat

&lt;zz&gt;linux运维自动化shell脚本小工具

from http://www.cnblogs.com/wang-li/p/5728461.html linux运维shell 脚本小工具,如要分享此文章,请注明文章出处,以下脚本仅供参考,若放置在服务器上出错,后果请自负 1.检测cpu剩余百分比 #!/bin/bash #Inspect CPU #Sun Jul 31 17:25:41 CST 2016 PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/hom

linux运维自动化shell脚本小工具

linux运维shell 脚本小工具,如要分享此文章,请注明文章出处,以下脚本仅供参考,若放置在服务器上出错,后果请自负 1.检测cpu剩余百分比 #!/bin/bash #Inspect CPU #Sun Jul 31 17:25:41 CST 2016 PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin export PATH TERM=linux export TERM CpuResu

shell脚本加密工具

1. 加密工具 1.1 gzexe 1.1.1 说明 gzexe是用来压缩执行文件的程序.当您去执行被压缩过的执行文件时,该文件会自动解压然后继续执行,和使用一般的执行文件相同. 1.1.2 安装 Linux自带,不用单独安装. 1.1.3 用法 gzexe filename.sh 它会把原来没有加密的文件备份为 file.sh~ ,同时 file.sh 即被变成加密文件. 1.1.4 参数 -d 解开压缩文件 1.2 shc 1.2.1 说明 shc是一个专业的加密shell脚本的工具,它的作

远程shell脚本执行工具类

/** * 远程shell脚本执行工具类 */public class RemoteShellExecutorUtils { private static final Logger logger = LoggerFactory.getLogger(RemoteShellExecutorUtils.class); private Connection conn; /** * 服务器IP */ private String ip; /** * 用户名 */ private String user;

Makefile Shell 脚本;sed命令

1. 在Makefile中想使用shell脚本,需要添加"@"符号,例如: @if [ -d xxx ]; then \                        //-d 判断是否存在,在shell中,用[]号代替括号 rm -rf xxx;  \ fi; @cp -fv  xxx  xxxx     //-f 删除已经存在的目标文件而不提示  -v  cp命令将告诉用户正在做什么 例:  cp -fv temp temp2 显示: 'temp'  ->  'temp2'

shell脚本--grep工具

grep工具 shell命令行之所以便捷和它所支持的工具是离不开的,其中grep工具就是一个搜索神器,它可以搜索整个linux系统所有文件中的所有信息. grep的灵活使用离不开正则表达式 正则表达式是一种查找以及字符串替换操作,它只是一个字符串,没有长度的限制,但是一般却表较短.可以被认为是一种轻量级.简介.使用与特定领域的编程语言. 这里主要使用grep工具 grep工具一般用来做两件事,搜索文件和搜索指定字符串 grep常用命令 -a 不要忽略二进制数据. -b 在显示符合范本样式的那一行

Shell脚本sed命令

from:http://blog.csdn.net/engledb/article/details/19623087 六.常用范例 1.p命令 命令p用于显示模式空间的内容.默认情况下,sed把输入行打印在屏幕上,选项-n用于取消默认的打印操作.当选项-n和命令p同时出现时,sed可打印选定的内容. 例子: [plain] view plaincopy (1)sed '/my/p' datafile #默认情况下,sed把所有输入行都打印在标准输出上.如果某行匹配模式my,p命令将把该行另外打印