仿站酷网站模板,平面设计培训学校一年学费,良品铺子的网站建设目标,危险网站怎么办sed 简明教程 转自#xff1a;https://coolshell.cn/articles/9104.html awk于1977年出生#xff0c;今年36岁本命年#xff0c;sed比awk大2-3岁#xff0c;awk就像林妹妹#xff0c;sed就是宝玉哥哥了。所以 林妹妹跳了个Topless#xff0c;他的哥哥sed坐不住了#xf…sed 简明教程 转自https://coolshell.cn/articles/9104.html awk于1977年出生今年36岁本命年sed比awk大2-3岁awk就像林妹妹sed就是宝玉哥哥了。所以 林妹妹跳了个Topless他的哥哥sed坐不住了也一定要出来抖一抖。
sed全名叫stream editor流编辑器用程序的方式来编辑文本相当的hacker啊。sed基本上就是玩正则模式匹配所以玩sed的人正则表达式一般都比较强。
同样本篇文章不会说sed的全部东西你可以参看sed的手册我这里主要还是想和大家竞争一下那些从手机指缝间或马桶里流走的时间用这些时间来学习一些东西。当然接下来的还是要靠大家自己双手。
用s命令替换
我使用下面的这段文本做演示
$ cat pets.txt
This is my catmy cats name is betty
This is my dogmy dogs name is frank
This is my fishmy fishs name is george
This is my goatmy goats name is adam把其中的my字符串替换成Hao Chen’s下面的语句应该很好理解s表示替换命令/my/表示匹配my/Hao Chen’s/表示把匹配替换成Hao Chen’s/g 表示一行上的替换所有的匹配
$ sed s/my/Hao Chens/g pets.txt
This is Hao Chens catHao Chens cats name is betty
This is Hao Chens dogHao Chens dogs name is frank
This is Hao Chens fishHao Chens fishs name is george
This is Hao Chens goatHao Chens goats name is adam注意如果你要使用单引号那么你没办法通过 \’ 这样来转义就有双引号就可以了在双引号内可以用 \” 来转义。
再注意上面的sed并没有对文件的内容改变只是把处理过后的内容输出如果你要写回文件你可以使用重定向如
$ sed s/my/Hao Chens/g pets.txt hao_pets.txt或使用 -i 参数直接修改文件内容
$ sed -i s/my/Hao Chens/g pets.txt在每一行最前面加点东西
$ sed s/^/#/g pets.txt
#This is my cat
# my cats name is betty
#This is my dog
# my dogs name is frank
#This is my fish
# my fishs name is george
#This is my goat
# my goats name is adam在每一行最后面加点东西
$ sed s/$/ --- /g pets.txt
This is my cat ---my cats name is betty ---
This is my dog ---my dogs name is frank ---
This is my fish ---my fishs name is george ---
This is my goat ---my goats name is adam ---顺手介绍一下正则表达式的一些最基本的东西
^ 表示一行的开头。如/^#/ 以#开头的匹配。$ 表示一行的结尾。如/}$/ 以}结尾的匹配。\ 表示词首。 如\abc 表示以 abc 为首的詞。\ 表示词尾。 如abc\ 表示以 abc 結尾的詞。. 表示任何单个字符。* 表示某个字符出现了0次或多次。[ ] 字符集合。 如[abc] 表示匹配a或b或c还有 [a-zA-Z] 表示匹配所有的26个字符。如果其中有^表示反如 [^a] 表示非a的字符
正规则表达式是一些很牛的事比如我们要去掉某html中的tags
bThis/b is what span styletext-decoration: underline;I/span meant. Understand?看看我们的sed命令
# 如果你这样搞的话就会有问题
$ sed s/.*//g html.txtUnderstand?# 要解决上面的那个问题就得像下面这样。
# 其中的[^] 指定了除了的字符重复0次或多次。
$ sed s/[^]*//g html.txt
This is what I meant. Understand?我们再来看看指定需要替换的内容
$ sed 3s/my/your/g pets.txt
This is my catmy cats name is betty
This is your dogmy dogs name is frank
This is my fishmy fishs name is george
This is my goatmy goats name is adam下面的命令只替换第3到第6行的文本。
$ sed 3,6s/my/your/g pets.txt
This is my catmy cats name is betty
This is your dogyour dogs name is frank
This is your fishyour fishs name is george
This is my goatmy goats name is adam$ cat my.txt
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my fish, my fishs name is george
This is my goat, my goats name is adam只替换每一行的第一个s
$ sed s/s/S/1 my.txt
ThiS is my cat, my cats name is betty
ThiS is my dog, my dogs name is frank
ThiS is my fish, my fishs name is george
ThiS is my goat, my goats name is adam只替换每一行的第二个s
$ sed s/s/S/2 my.txt
This iS my cat, my cats name is betty
This iS my dog, my dogs name is frank
This iS my fish, my fishs name is george
This iS my goat, my goats name is adam只替换第一行的第3个以后的s
$ sed s/s/S/3g my.txt
This is my cat, my catS name iS betty
This is my dog, my dogS name iS frank
This is my fiSh, my fiShS name iS george
This is my goat, my goatS name iS adam多个匹配
如果我们需要一次替换多个模式可参看下面的示例第一个模式把第一行到第三行的my替换成your第二个则把第3行以后的This替换成了That
$ sed 1,3s/my/your/g; 3,$s/This/That/g my.txt
This is your cat, your cats name is betty
This is your dog, your dogs name is frank
That is your fish, your fishs name is george
That is my goat, my goats name is adam上面的命令等价于注下面使用的是sed的-e命令行参数
sed -e 1,3s/my/your/g -e 3,**$s**/This/That/g my.txt
我们可以使用来当做被匹配的变量然后可以在基本左右加点东西。如下所示
$ sed s/my/[]/g my.txt
This is [my] cat, [my] cats name is betty
This is [my] dog, [my] dogs name is frank
This is [my] fish, [my] fishs name is george
This is [my] goat, [my] goats name is adam圆括号匹配
使用圆括号匹配的示例圆括号括起来的正则表达式所匹配的字符串会可以当成变量来使用sed中使用的是\1,\2…
$ sed s/This is my \([^,]*\),.*is \(.*\)/\1:\2/g my.txt
cat:betty
dog:frank
fish:george
goat:adam上面这个例子中的正则表达式有点复杂解开如下去掉转义字符
正则为This is my ([^,]*),.*is (.*) 匹配为This is my (cat),……….is (betty)
然后\1就是cat\2就是betty
sed的命令
让我们回到最一开始的例子pets.txt让我们来看几个命令
N命令
先来看N命令 —— 把下一行的内容纳入当成缓冲区做匹配。
下面的的示例会把原文本中的偶数行纳入奇数行匹配而s只匹配并替换一次所以就成了下面的结果
$ sed N;s/my/your/ pets.txt
This is your catmy cats name is betty
This is your dogmy dogs name is frank
This is your fishmy fishs name is george
This is your goatmy goats name is adam也就是说原来的文件成了
This is my cat\n my cats name is betty
This is my dog\n my dogs name is frank
This is my fish\n my fishs name is george
This is my goat\n my goats name is adam这样一来下面的例子你就明白了
$ sed N;s/\n/,/ pets.txt
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my fish, my fishs name is george
This is my goat, my goats name is adama命令和i命令
a命令就是append i命令就是insert它们是用来添加行的。如
# 其中的1i表明其要在第1行前插入一行insert
$ sed 1 i This is my monkey, my monkeys name is wukong my.txt
This is my monkey, my monkeys name is wukong
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my fish, my fishs name is george
This is my goat, my goats name is adam# 其中的1a表明其要在最后一行后追加一行append
$ sed $ a This is my monkey, my monkeys name is wukong my.txt
This is my cat, my cats name is betty
This is my monkey, my monkeys name is wukong
This is my dog, my dogs name is frank
This is my fish, my fishs name is george
This is my goat, my goats name is adam我们可以运用匹配来添加文本
# 注意其中的/fish/a这意思是匹配到/fish/后就追加一行
$ sed /fish/a This is my monkey, my monkeys name is wukong my.txt
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my fish, my fishs name is george
This is my monkey, my monkeys name is wukong
This is my goat, my goats name is adam下面这个例子是对每一行都挺插入
$ sed /my/a ---- my.txt
This is my cat, my cats name is betty
----
This is my dog, my dogs name is frank
----
This is my fish, my fishs name is george
----
This is my goat, my goats name is adam
----c命令
c 命令是替换匹配行
$ sed 2 c This is my monkey, my monkeys name is wukong my.txt
This is my cat, my cats name is betty
This is my monkey, my monkeys name is wukong
This is my fish, my fishs name is george
This is my goat, my goats name is adam$ sed /fish/c This is my monkey, my monkeys name is wukong my.txt
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my monkey, my monkeys name is wukong
This is my goat, my goats name is adamd命令
删除匹配行
$ sed /fish/d my.txt
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my goat, my goats name is adam$ sed 2d my.txt
This is my cat, my cats name is betty
This is my fish, my fishs name is george
This is my goat, my goats name is adam$ sed 2,$d my.txt
This is my cat, my cats name is bettyp命令
打印命令
你可以把这个命令当成grep式的命令
# 匹配fish并输出可以看到fish的那一行被打了两遍
# 这是因为sed处理时会把处理的信息输出
$ sed /fish/p my.txt
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my fish, my fishs name is george
This is my fish, my fishs name is george
This is my goat, my goats name is adam# 使用n参数就好了
$ sed -n /fish/p my.txt
This is my fish, my fishs name is george# 从一个模式到另一个模式
$ sed -n /dog/,/fish/p my.txt
This is my dog, my dogs name is frank
This is my fish, my fishs name is george#从第一行打印到匹配fish成功的那一行
$ sed -n 1,/fish/p my.txt
This is my cat, my cats name is betty
This is my dog, my dogs name is frank
This is my fish, my fishs name is george几个知识点
好了下面我们要介绍四个sed的基本知识点
Pattern Space
第零个是关于-n参数的大家也许没看懂没关系我们来看一下sed处理文本的伪代码并了解一下Pattern Space的概念
foreach line in file {//放入把行Pattern_SpacePattern_Space line;// 对每个pattern space执行sed命令Pattern_Space EXEC(sed_cmd, Pattern_Space);// 如果没有指定 -n 则输出处理后的Pattern_Spaceif (sed option hasnt -n) {print Pattern_Space}
}Address
第一个是关于address几乎上述所有的命令都是这样的注其中的!表示匹配成功后是否执行命令
[address[,address]][!]{cmd}
address可以是一个数字也可以是一个模式你可以通过逗号要分隔两个address 表示两个address的区间参执行命令cmd伪代码如下
bool bexec false
foreach line in file {if ( match(address1) ){bexec true;}if ( bexec true) {EXEC(sed_cmd);}if ( match (address2) ) {bexec false;}
}关于address可以使用相对位置如
# 其中的3表示后面连续3行
$ sed /dog/,3s/^/# /g pets.txt
This is my catmy cats name is betty
# This is my dog
# my dogs name is frank
# This is my fish
# my fishs name is george
This is my goatmy goats name is adam命令打包
第二个是cmd可以是多个它们可以用分号分开可以用大括号括起来作为嵌套命令。下面是几个例子
$ cat pets.txt
This is my catmy cats name is betty
This is my dogmy dogs name is frank
This is my fishmy fishs name is george
This is my goatmy goats name is adam# 对3行到第6行执行命令/This/d
$ sed 3,6 {/This/d} pets.txt
This is my catmy cats name is bettymy dogs name is frankmy fishs name is george
This is my goatmy goats name is adam# 对3行到第6行匹配/This/成功后再匹配/fish/成功后执行d命令
$ sed 3,6 {/This/{/fish/d}} pets.txt
This is my catmy cats name is betty
This is my dogmy dogs name is frankmy fishs name is george
This is my goatmy goats name is adam# 从第一行到最后一行如果匹配到This则删除之如果前面有空格则去除空格
$ sed 1,${/This/d;s/^ *//g} pets.txt
my cats name is betty
my dogs name is frank
my fishs name is george
my goats name is adam Hold Space
第三个我们再来看一下 Hold Space
接下来我们需要了解一下Hold Space的概念我们先来看四个命令
g 将hold space中的内容拷贝到pattern space中原来pattern space里的内容清除G 将hold space中的内容append到pattern space\n后h 将pattern space中的内容拷贝到hold space中原来的hold space里的内容被清除H 将pattern space中的内容append到hold space\n后x 交换pattern space和hold space的内容
这些命令有什么用我们来看两个示例吧用到的示例文件是
$ cat t.txt
one
two
three第一个示例
$ sed H;g t.txt
oneone
twoone
two
three是不是有点没看懂我作个图你就看懂了。 第二个示例反序了一个文件的行
$ sed 1!G;h;$!d t.txt
three
two
one其中的 ‘1!G;h;$!d’ 可拆解为三个命令
1!G —— 只有第一行不执行G命令将hold space中的内容append回到pattern spaceh —— 第一行都执行h命令将pattern space中的内容拷贝到hold space中$!d —— 除了最后一行不执行d命令其它行都执行d命令删除当前行
这个执行序列很难理解做个图如下大家就明白了 就先说这么多吧希望对大家有用。
全文完