sed如何插入多行和特殊字符

hqmb2 2019-01-30 06:51:18
[root@Client1-RHEL chap05]# cat -n datafile
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 1

问题1:
比如匹配north以后,我想插入多行
--->This is line 1<---
--->This is line 2<---
--->This is line 3<---
--->This is line 4<---

[root@Client1-RHEL chap05]# sed '/^north /a
> --->This is line 1<---
> --->This is line 2<---
> --->This is line 3<---
> --->This is line 4<---' datafile
sed: -e expression #1, char 13: unknown command: `-'

问题2:
如何正确插入特殊字符,比如IT'S里边的单引号
[root@Client1-RHEL chap05]# sed '/^north /a "IT'S A SECRET"' datafile
>
[root@Client1-RHEL chap05]# sed '/^north /a "IT\'S A SECRET"' datafile
>
尝试了两种方法,都直接接入多行编辑模式
...全文
1927 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2019-02-26
  • 打赏
  • 举报
回复
楼上正解
$ sed -e '/north /a \
--->This is line 1<---\
--->This is line 2<---\
--->This is line 3<---\
--->This is line 4<---' datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
--->This is line 1<---
--->This is line 2<---
--->This is line 3<---
--->This is line 4<---
central CT Ann Stephens 5.7 .94 5 1
$
cjaizss 2019-02-19
  • 打赏
  • 举报
回复
sed '/^north[ \t]/a\--->This is line 1<---\
--->This is line 2<---\
--->This is line 3<---\
--->This is line 4<---'
这个是你要的吗?
aabbabababaa 2019-02-09
  • 打赏
  • 举报
回复
特殊符号要转义吧,多行也是因为有特殊符号。。。

23,216

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧