首页 > 代码库 > sed Demo

sed Demo

@1:sed basic usage:

#!/bin/bash#File: sedDemo.sh#Author: lxw#Time: 2014-08-18#Usage: Demonstration for sed.#NOTE: use " instead of ‘ in sed.echo "Demo -----------------------"sed "s/Chen Hao‘s/my/g" pets.txtecho "-i选项: 修改文件本身-----------------------"sed -i "s/my/Chen Hao‘s/g" pets.txtecho "\<fish:以fish开头 -------------------------"sed "s/\<fish/FISH/g" pets.txtecho "sh\>:以sh结尾-------------------------"sed "s/sh\>/SH/g" pets.txtecho "s匹配替换-----------------"echo "第3行-------------------"sed "3s/Chen/CHEN/g" pets.txtecho "3-6行(既包括第3行,又包括第6行)-------------------"sed "3,6s/Chen/CHEN/g" pets.txtecho "i -> I: 每行的第一个i-------------------"sed "s/i/I/1" pets.txtecho "i -> I: 每行的第2个i(包括第2个i)以后的i --> 不是第2个字母-------------------"sed "s/i/I/2g" pets.txtecho "多个s匹配-----------------"sed "1,3s/Chen/CHEN/g; 5,10s/This/That/g" pets.txtecho "上面的命令等价于这条命令--------------------"sed -e "1,3s/Chen/CHEN/g" -e "5,10s/This/That/g" pets.txt#sed -e "1,3s/Chen/CHEN/g" -e "5,$s/This/That/g" pets.txt    #$字符的使用有点儿问题echo "&字符的使用-------------------"sed "s/Chen/[&]/g" pets.txt#圆括号括起来的正则表达式所匹配的字符串可以当成变量来使用,sed中使用的是\1,\2echo "sed的格式化文本处理-----------"sed "s/This is my \([^,]*\),.*is \(.*\)/---\1:\2---/g" my.txtecho "N命令: 只匹配奇数行---------------------------"sed "N;s/my/your/" my.txtecho "每两行和并为一行(例如:第一行和第二行合并,但第二行和第三行不合并)----"sed "N;s/\n//" pets.txt#a命令和i命令echo "第一行前面添加一行-----------------"sed "1 i This is my monkey, my monkey‘s name is wukong" my.txtecho "最后一行后面添加一行-----------------"sed "$ a This is my monkey, my monkey‘s name is wukong" my.txtecho "在匹配到/my/的行后面添加一行-----------------"sed "/my/a This is my monkey, my monkey‘s name is wukong" my.txt    #只要这一行中有匹配的字符串就会添加echo "c命令: 整行替换,替换匹配行-----------------"echo "替换第1行-----------------"sed "1 c This is my monkey, my monkey‘s name is wukong" my.txtecho "匹配到/fish/的行-----------------"sed "/fish/c This is my monkey, my monkey‘s name is wukong" my.txtecho "d命令: 删除匹配行-----------------"sed "1d" my.txtecho "删除匹配到/fish/的行-----------------"sed "/fish/d" my.txt#sed "2,$d" my.txt    #$字符的使用有点儿问题echo "p命令: 打印匹配行----------------------"echo "匹配fish并输出,fish行被输出了两遍,因为sed处理时会把处理的信息输出----"sed "/fish/p" my.txtecho "-n选项: 只输出匹配行------------------------"sed -n "/fish/p" my.txtecho "从cat到fish的行,而不仅仅是cat和fish行------------------------"sed -n "/cat/,/fish/p" my.txtecho "从第一行打印到匹配fish的行-------------------"sed -n "1,/fish/p" my.txtecho "+3表示后面连续3行------------------"sed "/dog‘s/,+3s/^/# /g" pets.txtecho "对3行到第6行,执行命令/This/d---------"sed "3,6{/This/d}" pets.txtecho "对3行到第6行,匹配/This/成功后,再匹配/fish/,成功后执行d命令---------"sed "3,6{/This/{/fish/d}}" pets.txtecho "从第一行到最后一行,如果匹配到This,则删除之;如果前面有空格,则去除空格(tab不行,多个空格可以)----"sed "1,10{/This/d; s/^ *//g}" pets.txt    #NOTE: 用这个例子学习*的作用。

 

@2: sed 中的N命令

lxw@lxw-PC:~/lxw_Documents$ cat my.txtThis is my cat,my cats name is bettyThis is my dog,my dogs name is frankThis is my fish,my fishs name is georgeThis is my goat,my goats name is adam
lxw@lxw-PC:~/lxw_Documents$ sed "s/my/your/" my.txt This is your cat,my cats name is bettyThis is your dog,my dogs name is frankThis is your fish,my fishs name is georgeThis is your goat,my goats name is adam
lxw@lxw-PC:~/lxw_Documents$ sed "s/my/your/g" my.txt This is your cat,your cats name is bettyThis is your dog,your dogs name is frankThis is your fish,your fishs name is georgeThis is your goat,your goats name is adam
lxw@lxw-PC:~/lxw_Documents$ sed "N;s/my/your/" my.txt This is your cat,my cats name is bettyThis is my dog,my dogs name is frankThis is your fish,my fishs name is georgeThis is my goat,my goats name is adam
lxw@lxw-PC:~/lxw_Documents$ sed "N;s/my/your/g" my.txt This is your cat,your cats name is bettyThis is your dog,your dogs name is frankThis is your fish,your fishs name is georgeThis is your goat,your goats name is adam

 

@3: 使用到的两个示例文件的内容如下:

my.txt

This is my cat,    my cats name is bettyThis is my dog,    my dogs name is frankThis is my fish,    my fishs name is georgeThis is my goat,    my goats name is adam

pets.txt

This is my cat, my cats name is bettyThis is my dog, my dogs name is frankThis is my fish, my fishs name is georgeThis is my goat, my goats name is adam

 

Reference:

sed简明教程:http://coolshell.cn/articles/9104.html