分页: 1 / 1

[问题]sed的append(\a)与next命令(n)问题

发表于 : 2008-03-20 11:09
searland
先给出几个正确运行的例子,最后一个有语法错误,可是我不懂错误的含义,请教诸位

sed version: GNU sed 4.1.5
test文件共6行
$ cat test
1 this is the first line
2 this is the second line
3 this is the third line
i this is the fourth line
1 this is the fifth line
2 this is the sixth line
在每行下append一行‘---’
$ sed 'a\---' test
1 this is the first line
---
2 this is the second line
---
3 this is the third line
---
i this is the fourth line
---
1 this is the fifth line
---
2 this is the sixth line
---
找到含有1的行,移到下一行,将this换为THAT
$ sed '/1/{n;s/this/THAT;}' test
1 this is the first line
2 THAT is the second line
3 this is the third line
i this is the fourth line
1 this is the fifth line
2 THAT is the sixth line
找到含有1的行,跳过,其余的每行下append一行‘---’
$ sed '/1/n;a\---' test
1 this is the first line
2 this is the second line
---
3 this is the third line
---
i this is the fourth line
---
1 this is the fifth line
2 this is the sixth line
---
出语法错误的一行,原目的是在含有1的一行的下一行(这里是含有2的那两行)下append一行‘---’
$ sed '/1/{n;a\---;}' test
sed:-e 表达式 #1,字符 0:未匹配的“{”

请问诸位这一句的错误在哪?正确的语法应该如何实现呢。谢谢。

发表于 : 2008-03-20 11:14
eexpress
看花了。你为什么不这样写。看得清楚些
1
2
3
4
5
6
7
8
9

{}的批量操作,记得是针对一个地址寻址的。换地址,要重新{}的。

发表于 : 2008-03-20 11:17
searland
呵呵,加上那些字是为了作替换(s)实验用的。
我在重新看一下{}的用法

发表于 : 2008-03-20 11:21
searland
eexpress 写了:看花了。你为什么不这样写。看得清楚些
1
2
3
4
5
6
7
8
9

{}的批量操作,记得是针对一个地址寻址的。换地址,要重新{}的。
可是这一句是正确的啊
sed '/1/{n;s/this/THAT;}' test

发表于 : 2008-03-29 17:14
baic
百度了一下wu终于找到了

$ sed -e '/1/{n;a\---' -e '}' test

发表于 : 2008-03-29 22:03
searland
我试一下先,哈哈