sed 整理

文本文件内如如下:

# cat ckl.txt 

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893

anybody ? help ?

but no money

lover & where

8992323777888


1. a\ 在当前行后添加一行或多行 (ckl后面添加两行)

# sed '/^ckl/ a\\--->test<--- \n haha' ckl.txt  (a 后面第二个反斜杠用来被shell转意为换行符,\n 为换行)

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893   

--->test<---   #添加的行

 haha        #添加的行

anybody ? help ?

but no money

lover & where

8992323777888


2.c\ 用新文本替换(修改)当前行 。(修改perfect这行)

# sed '/perfect/ c\ wanmei' ckl.txt 

This is a test file for sed .

a dood day

 wanmei    #perfect 变成wanmei


but i no ideal

ckl893

anybody ? help ?

but no money

lover & where

8992323777888


3.i\ 在当前行添加一行或多行 (在anybody 前添加一行)

# sed '/anybody/ i\ renheren' ckl.txt 

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893

 renheren #添加的行,在anybody 之前

anybody ? help ?

but no money

lover & where

8992323777888


4.d 删除行

# sed '/money/d' ckl.txt    (删除money 行)

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893

anybody ? help ?

lover & where

8992323777888


5. 将ckl到lover之间所有行结尾替换为**VC**

# sed '/ckl/,/lover/ s/$/**VC**/g' ckl.txt 

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893**VC**

anybody ? help ?**VC**

but no money**VC**

lover & where**VC**

8992323777888


6.将结尾为两位数字的行,后面添加.5

# sed 's/[0-9][0-9]$/&.5/g' ckl.txt  

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893.5

anybody ? help ?

but no money

lover & where

8992323777888.5


7.y 将字符转换为另一字符

将1到3行之间所有小写字母全部转换为大写

# sed '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' ckl.txt  

THIS IS A TEST FILE FOR SED .

A DOOD DAY

PERFECT !


but i no ideal

ckl893

anybody ? help ?

but no money

lover & where

8992323777888


8.h 把模式空间里的内容拷贝暂存缓冲区 (将ckl这一行拷贝到最后一行)

# sed -e '/ckl/h' -e '$G' ckl.txt 

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893

anybody ? help ?

but no money

lover & where

8992323777888

ckl893


9.h 把模式空间里的内容追加暂存缓冲区 (将ckl这一行追加到最后一行)

# sed -e '/ckl/H' -e '$G' ckl.txt 

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893

anybody ? help ?

but no money

lover & where

8992323777888


ckl893


10.g取出暂存缓冲区内容,将其拷贝到模式空间,覆盖该处原有的内容。

(把包含perfect 这行拷贝到暂存空间,删除改行,然后替换掉文件包含but的行。)

# sed -e '/perfect/{h;d}' -e '/but/{g;}' ckl.txt 

This is a test file for sed .

a dood day


perfect !

ckl893

anybody ? help ?

perfect !

lover & where

8992323777888


11.G取出暂存缓冲区内容,将其拷贝到模式空间,追加在原有内容后面

(把包含perfect 这行拷贝到暂存空间,删除改行,然后追加到文件的最后一行。)

# sed -e '/perfect/{h;d}' -e '$G' ckl.txt          

This is a test file for sed .

a dood day


but i no ideal

ckl893

anybody ? help ?

but no money

lover & where

8992323777888

perfect !


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

(包含lover的行将被perfect 的行替换。)

# sed -e '/perfect/h' -e '/lover/x' ckl.txt      

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893

anybody ? help ?

but no money

perfect !

8992323777888


13.!对所选行以外所有行应用命令

(只保留只包含but的行)

# sed '/but/!d' ckl.txt    

but i no ideal

but no money



14.{} 组合命令

(删除perfect到ckl之间所有空行,并将包含but的行拷贝到暂存空间并删除)

# sed '/perfect/,/ckl/{/^$/d;/but/{h;d}}' ckl.txt  

This is a test file for sed .

a dood day

perfect !

ckl893

anybody ? help ?

but no money

lover & where

8992323777888


15.n 读入下一行,从下一条命令开始,而不是第一跳命令开始操作

(匹配到ckl这行,从这行下一行开始,将anybody替换为lover)

# sed '/ckl/{n;s/anybody/lover/;}' ckl.txt                  

This is a test file for sed .

a dood day

perfect !


but i no ideal

ckl893

lover ? help ?

but no money

lover & where

8992323777888


你可能感兴趣的:(模块)