sed Demo
@1:sed basic usage:
和AWK一样, sed也是逐行对文本进行处理. sed的主要功能如下:
@1:对每行中的匹配项进行处理(修改/删除)
@2:格式化文本的处理
@3:(行的增删改):整行(指定行或匹配行)替换/在指定位置(指定行或匹配行)添加整行文本/删除整行(指定行或匹配行)
#!/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.txt echo "-i选项: 修改文件本身-----------------------" sed -i "s/my/Chen Hao's/g" pets.txt echo "\<fish:以fish开头 -------------------------" sed "s/\<fish/FISH/g" pets.txt echo "sh\>:以sh结尾-------------------------" sed "s/sh\>/SH/g" pets.txt echo "s匹配替换-----------------" echo "第3行-------------------" sed "3s/Chen/CHEN/g" pets.txt echo "3-6行(既包括第3行,又包括第6行)-------------------" sed "3,6s/Chen/CHEN/g" pets.txt echo "i -> I: 每行的第一个i-------------------" sed "s/i/I/1" pets.txt echo "i -> I: 每行的第2个i(包括第2个i)以后的i --> 不是第2个字母-------------------" sed "s/i/I/2g" pets.txt echo "多个s匹配-----------------" sed "1,3s/Chen/CHEN/g; 5,10s/This/That/g" pets.txt echo "上面的命令等价于这条命令--------------------" 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,\2 echo "sed的格式化文本处理-----------" sed "s/This is my \([^,]*\),.*is \(.*\)/---\1:\2---/g" my.txt echo "N命令: 只匹配奇数行---------------------------" sed "N;s/my/your/" my.txt echo "每两行和并为一行(例如:第一行和第二行合并,但第二行和第三行不合并)----" sed "N;s/\n//" pets.txt #a命令和i命令 echo "第一行前面添加一行-----------------" sed "1 i This is my monkey, my monkey's name is wukong" my.txt echo "最后一行后面添加一行-----------------" sed "$ a This is my monkey, my monkey's name is wukong" my.txt echo "在匹配到/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.txt echo "匹配到/fish/的行-----------------" sed "/fish/c This is my monkey, my monkey's name is wukong" my.txt echo "d命令: 删除匹配行-----------------" sed "1d" my.txt echo "删除匹配到/fish/的行-----------------" sed "/fish/d" my.txt #sed "2,$d" my.txt #$字符的使用有点儿问题 echo "p命令: 打印匹配行----------------------" echo "匹配fish并输出,fish行被输出了两遍,因为sed处理时会把处理的信息输出----" sed "/fish/p" my.txt echo "-n选项: 只输出匹配行------------------------" sed -n "/fish/p" my.txt echo "从cat到fish的行,而不仅仅是cat和fish行------------------------" sed -n "/cat/,/fish/p" my.txt echo "从第一行打印到匹配fish的行-------------------" sed -n "1,/fish/p" my.txt echo "+3表示后面连续3行------------------" sed "/dog's/,+3s/^/# /g" pets.txt echo "对3行到第6行,执行命令/This/d---------" sed "3,6{/This/d}" pets.txt echo "对3行到第6行,匹配/This/成功后,再匹配/fish/,成功后执行d命令---------" sed "3,6{/This/{/fish/d}}" pets.txt echo "从第一行到最后一行,如果匹配到This,则删除之;如果前面有空格,则去除空格(tab不行,多个空格可以)----" sed "1,10{/This/d; s/^ *//g}" pets.txt #NOTE: 用这个例子学习*的作用。
@2: sed 中的N命令
lxw@lxw-PC:~/lxw_Documents$ 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
lxw@lxw-PC:~/lxw_Documents$ sed "s/my/your/" my.txt This is your cat,my cat's name is betty This is your dog,my dog's name is frank This is your fish,my fish's name is george This is your goat,my goat's name is adam
lxw@lxw-PC:~/lxw_Documents$ sed "s/my/your/g" my.txt This is your cat,your cat's name is betty This is your dog,your dog's name is frank This is your fish,your fish's name is george This is your goat,your goat's name is adam
lxw@lxw-PC:~/lxw_Documents$ sed "N;s/my/your/" my.txt This is your cat,my cat's name is betty This is my dog,my dog's name is frank This is your fish,my fish's name is george This is my goat,my goat's name is adam
lxw@lxw-PC:~/lxw_Documents$ sed "N;s/my/your/g" my.txt This is your cat,your cat's name is betty This is your dog,your dog's name is frank This is your fish,your fish's name is george This is your goat,your goat's name is adam
@3: 使用到的两个示例文件的内容如下:
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
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
Reference: