sed如何插入多行和特殊字符
[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
>
尝试了两种方法,都直接接入多行编辑模式
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
>
尝试了两种方法,都直接接入多行编辑模式
...全文
相关推荐
Linux sed命令详解[可运行源码]
在高级用法方面,sed允许用户处理多行模式,例如,可以使用正则表达式匹配多行文本,并执行插入、删除或替换等操作。此外,sed还支持引用匹配到的文本内容,这在构建复杂文本模式时非常有用。
详解Sed命令的用法与正则表达式元字符
Sed是一种流编辑器,用于对文本数据执行各种转换。它特别擅长处理基于行的文本数据流,允许用户进行模式匹配、替换、插入、删除等编辑操作。
sed高级用法学习笔记和实例
- **a\**: 在匹配行之后添加一行或多行文本。- **c\**: 替换整个匹配行。- **i\**: 在匹配行之前插入一行或多行文本。- **n**: 读取下一行,不打印当前行。
sed和awk简明手册
#### 三、sed与awk的区别- **编辑方式**: - sed主要针对文本编辑,主要用于文本替换、删除、插入等操作。 - awk更偏向于数据分析和处理,支持更复杂的逻辑操作和数学计算。
sed_parse_xml
`sed`主要处理的是文本,所以处理XML时需要考虑到XML的层次结构和特殊字符的转义。


