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

sh/bash/dash/ksh/zsh等Shell脚本
回复
searland
帖子: 29
注册时间: 2006-07-16 2:26

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

#1

帖子 searland » 2008-03-20 11:09

先给出几个正确运行的例子,最后一个有语法错误,可是我不懂错误的含义,请教诸位

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:未匹配的“{”

请问诸位这一句的错误在哪?正确的语法应该如何实现呢。谢谢。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#2

帖子 eexpress » 2008-03-20 11:14

看花了。你为什么不这样写。看得清楚些
1
2
3
4
5
6
7
8
9

{}的批量操作,记得是针对一个地址寻址的。换地址,要重新{}的。
● 鸣学
searland
帖子: 29
注册时间: 2006-07-16 2:26

#3

帖子 searland » 2008-03-20 11:17

呵呵,加上那些字是为了作替换(s)实验用的。
我在重新看一下{}的用法
searland
帖子: 29
注册时间: 2006-07-16 2:26

#4

帖子 searland » 2008-03-20 11:21

eexpress 写了:看花了。你为什么不这样写。看得清楚些
1
2
3
4
5
6
7
8
9

{}的批量操作,记得是针对一个地址寻址的。换地址,要重新{}的。
可是这一句是正确的啊
sed '/1/{n;s/this/THAT;}' test
baic
帖子: 166
注册时间: 2006-08-22 23:10

#5

帖子 baic » 2008-03-29 17:14

百度了一下wu终于找到了

$ sed -e '/1/{n;a\---' -e '}' test
searland
帖子: 29
注册时间: 2006-07-16 2:26

#6

帖子 searland » 2008-03-29 22:03

我试一下先,哈哈
回复