sed Tutorial

1.s option

$ cat pets.txt
This is my cat  
  my cat's name is betty 
This is my dog   
  my dog's name is frank 
This is my fish   
  my fish's name is george 
This is my goat   
  my goat's name is adam 

sed "s/my/Fei's/g" pets.txt
sed "s/my/Fei's/g" pets.txt > fei_pets.txt
sed -i "s/my/Fei's/g" pets.txt
sed 's/^/#/g' pets.txt
sed 's/$/ --- /g' pets.txt

 

regex tips:
^ 表示一行的开头。如:/^#/ 以#开头的匹配。
$ 表示一行的结尾。如:/}$/ 以}结尾的匹配。
\< 表示词首。 如 \<abc 表示以 abc 为首的詞。
\> 表示词尾。 如 abc\> 表示以 abc 結尾的詞。
. 表示任何单个字符。
* 表示某个字符出现了0次或多次。
[ ] 字符集合。 如:[abc]表示匹配a或b或c,还有[a-zA-Z]表示匹配所有的26个字符。如果其中有^表示反,如[^a]表示非a的字符

 

$ cat html.txt:
<b>This</b> is what <span style="text-decoration: underline;">I</span> meant. Understand?

 

sed 's/<[^>]*>//g' html.txt
=>This is what I meant. Understand?

sed "3s/my/your/g" pets.txt    #replace third row
sed "3,6s/my/your/g" pets.txt  #replace third to sixth rows

 

$ cat my.txt 
This is my cat, my cat's name is betty 
This is my dog, my dog's name is frank 
This is my fish, my fish's name is george 
This is my goat, my goat's name is adam

sed 's/s/S/1' my.txt   #replace only first  's'
sed 's/s/S/2' my.txt   #replace only second 's'
sed 's/s/S/3g' my.txt  #replace after third(contain) 's'

 

$ sed 's/\<Thi/Tha/g' my.txt 
Thas is my cat, my cat's name is betty 
Thas is my dog, my dog's name is frank 
Thas is my fish, my fish's name is george 
Thas is my goat, my goat's name is adam 

$ sed 's/his\>/hat/g' my.txt 
That is my cat, my cat's name is betty 
That is my dog, my dog's name is frank 
That is my fish, my fish's name is george 
That is my goat, my goat's name is adam 

 

2.Multiple matching
sed '1,3s/my/your/g; 3,$s/This/That/g' my.txt
sed -e '1,3s/my/your/g' -e '3,$s/This/That/g' my.txt
#use & replace matched var
sed 's/my/[&]/g' my.txt

 

3.() matching
#use \1,\2,... replace matched ()
sed 's/This is my \([^,]*\),.*is \(.*\)/\1:\2/g' my.txt

 

4.sed command
1)N  : Read/append the next line of input into the pattern space.
sed 'N;s/my/your/' pets.txt
sed 'N;s/\n/,/' pets.txt

 

2)add new row
a : after append row
i : before insert row
#specify row number,1 first isrow, $ is last row
sed "1 i This is my monkey, my monkey's name is wukong" my.txt
sed "1 a This is my monkey, my monkey's name is wukong" my.txt

#pattern match
sed "/fish/a This is my monkey, my monkey's name is wukong" my.txt
sed "/my/a ----" my.txt

 

3)c : replace matched row
sed "2 c This is my monkey, my monkey's name is wukong" my.txt
sed "2 c This is my monkey, my monkey's name is wukong" my.txt

 

4)d : delete matched rows
sed '/fish/d' my.txt
sed '2d' my.txt
sed '2,$d' my.txt

 

5)p : print rows,like grep
sed -n '/fish/p' my.txt
#print from dog to fish rows
sed -n '/dog/,/fish/p' my.txt
sed -n '1,/fish/p' my.txt
sed -n '/fish/,/$/p' my.txt

 

5.Other knowledge points
1)Pattern Space

-n parameter


2)Address

#relative position, +3
sed '/dog/,+3s/^/# /g' pets.txt

 

3)Command Package
sed '3,6 {/This/d}' pets.txt
sed '3,6 {/This/{/fish/d}' pets.txt
sed '1,$ {/This/d; s/^ *//g' pets.txt

 

4)Hold Space
g|G : rewrite|append content from hold space to pattern spac.
h|H : rewrite|append content from pattern space to hold space
x   : swap content between pattern space and hold space

 

$ cat t.txt
one
two
three

 $sed 'H;g' t.txt



 
$sed '1!G;h;$!d' t.txt


 
Refers:
http://coolshell.cn/articles/9104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值