SHELL 在指定行的前/后插入指定内容
2017-04-01 13:59
148 查看
原文件:
[root@xiaowu shell]# cat -n file
1 aaaa
2 bbbb
3 cccc
4 dddd
现在要在第二行即“bbbb”行的下面添加一行,内容为“xiaowu”
[root@xiaowu shell]# sed '/bbbb/a\xiaowu' file
aaaa
bbbb
xiaowu
cccc
dddd
如果要加两行“xiaowu”可以用一下语句,注意用“\n”换行
[root@xiaowu shell]# sed '/bbbb/a\xiaowu\nxiaowu' file
aaaa
bbbb
xiaowu
xiaowu
cccc
dddd
如果要在第二行即“bbbb”行的上添加一行,内容为“xiaowu”,可以把参数“a”换成“i”
[root@xiaowu shell]# sed '/b/i\xiaowu' file
aaaa
xiaowu
bbbb
cccc
dddd
以上文件中只有一行匹配,如果文件中有两行或者多行匹配,结果有是如何呢?
[root@xiaowu shell]# cat -n file
1 aaaa
2 bbbb
3 cccc
4 bbbb
5 dddd
[root@xiaowu shell]# sed '/bbbb/a\xiaowu' file
aaaa
bbbb
xiaowu
cccc
bbbb
xiaowu
dddd
由结果可知,每个匹配行的下一行都会被添加“xiaowu”
那么如果指向在第二个“bbbb”的下一行添加内容“xiaowu”,该如何操作呢?
可以考虑先获取第二个“bbbb”行的行号,然后根据行号在此行的下一行添加“xiaowu”
获取第二个“bbbb”行的行号的方法:
方法一:
[root@xiaowu shell]# cat -n file |grep b |awk '{print $1}'|sed -n "2"p
4
方法二:
[root@xiaowu shell]# sed -n '/bbbb/=' file |sed -n "2"p
4
由结果可知第二个“bbbb”行的行号为4,然后再在第四行的前或后添加相应的内容:
[root@xiaowu shell]# sed -e '4a\xiaowu' file
aaaa
bbbb
cccc
bbbb
xiaowu
dddd
[root@xiaowu shell]# sed -e '4a\xiaowu\nxiaowu' file
aaaa
bbbb
cccc
bbbb
xiaowu
xiaowu
dddd
向指定行的末尾添加指定内容,比如在“ccccc”行的行尾介绍“ eeeee”
[root@xiaowu shell]# cat file
aaaaa
bbbbb
ccccc
ddddd
[root@xiaowu shell]# sed 's/cc.*/& eeeee/g' file
aaaaa
bbbbb
ccccc eeeee
ddddd
[root@xiaowu shell]# cat -n file
1 aaaa
2 bbbb
3 cccc
4 dddd
现在要在第二行即“bbbb”行的下面添加一行,内容为“xiaowu”
[root@xiaowu shell]# sed '/bbbb/a\xiaowu' file
aaaa
bbbb
xiaowu
cccc
dddd
如果要加两行“xiaowu”可以用一下语句,注意用“\n”换行
[root@xiaowu shell]# sed '/bbbb/a\xiaowu\nxiaowu' file
aaaa
bbbb
xiaowu
xiaowu
cccc
dddd
如果要在第二行即“bbbb”行的上添加一行,内容为“xiaowu”,可以把参数“a”换成“i”
[root@xiaowu shell]# sed '/b/i\xiaowu' file
aaaa
xiaowu
bbbb
cccc
dddd
以上文件中只有一行匹配,如果文件中有两行或者多行匹配,结果有是如何呢?
[root@xiaowu shell]# cat -n file
1 aaaa
2 bbbb
3 cccc
4 bbbb
5 dddd
[root@xiaowu shell]# sed '/bbbb/a\xiaowu' file
aaaa
bbbb
xiaowu
cccc
bbbb
xiaowu
dddd
由结果可知,每个匹配行的下一行都会被添加“xiaowu”
那么如果指向在第二个“bbbb”的下一行添加内容“xiaowu”,该如何操作呢?
可以考虑先获取第二个“bbbb”行的行号,然后根据行号在此行的下一行添加“xiaowu”
获取第二个“bbbb”行的行号的方法:
方法一:
[root@xiaowu shell]# cat -n file |grep b |awk '{print $1}'|sed -n "2"p
4
方法二:
[root@xiaowu shell]# sed -n '/bbbb/=' file |sed -n "2"p
4
由结果可知第二个“bbbb”行的行号为4,然后再在第四行的前或后添加相应的内容:
[root@xiaowu shell]# sed -e '4a\xiaowu' file
aaaa
bbbb
cccc
bbbb
xiaowu
dddd
[root@xiaowu shell]# sed -e '4a\xiaowu\nxiaowu' file
aaaa
bbbb
cccc
bbbb
xiaowu
xiaowu
dddd
向指定行的末尾添加指定内容,比如在“ccccc”行的行尾介绍“ eeeee”
[root@xiaowu shell]# cat file
aaaaa
bbbbb
ccccc
ddddd
[root@xiaowu shell]# sed 's/cc.*/& eeeee/g' file
aaaaa
bbbbb
ccccc eeeee
ddddd
相关文章推荐
- shell中sed在指定行插入内容,删除指定行
- Java输入输出(7)---向指定文件,指定位置插入内容
- 向指定文件,指定位置插入内容
- [QTP]通过vbscript往文本文件中指定位置插入指定内容
- Linux的Shell--Sed操作(指定行增加删除内容)
- Linux下搜索指定内容的文件 shell命令备忘
- Linux下在文件内部指定行(首行、末尾行等)插入内容
- javascript实现 在光标处插入指定内容
- 将richTextBox中的内容写入txt文件发现不换行(解决方法),在richTextBox指定位置插入文字
- 编写shell脚本unload表格里的内容到指定文件里
- shell 替换文本中的指定内容
- python和shell脚本实现查看大文件指定区域范围内的文件内容
- 关于文本输入框获取光标位置以及指定位置插入内容
- shell如何在指定文件的指定位置后面添加内容
- python插入内容到指定文件的位置
- jquery 在textarea 指定 位置处 插入内容
- 如何在指定的地方插入html内容和文本内容
- shell如何在指定文件的指定位置后面添加内容
- shell 精确获取指定内容
- javascript实现 在光标处插入指定内容