Chinaunix

标题: 請問如何在已知的行號的前一行插入字串? [打印本页]

作者: inmolee    时间: 2005-07-11 13:50
标题: 請問如何在已知的行號的前一行插入字串?
aaa
bbb
ccc
ddd
eee
fff
ggg

如何在指定的行前面加入指定字串呢?(例如第3行前面插入test)

aaa
bbb
test
ccc
ddd
eee
fff
ggg
作者: waker    时间: 2005-07-11 14:01
标题: 請問如何在已知的行號的前一行插入字串?
看置顶基础十二篇sed篇或awk篇
作者: icesummit    时间: 2005-07-11 14:28
标题: 請問如何在已知的行號的前一行插入字串?
http://bbs.chinaunix.net/forum/viewtopic.php?t=575020
作者: honbj    时间: 2005-07-11 17:07
标题: 請問如何在已知的行號的前一行插入字串?
原帖由 "inmolee" 发表:
cc
ddd
eee
fff
ggg


如果该文件没有其他空行

  1. sed '/ccc/{x;p;x}' filename | sed 's/^$/test/g'
复制代码

作者: liuyunxiaoyan    时间: 2005-07-11 17:37
标题: 請問如何在已知的行號的前一行插入字串?
sed '/ccc/i\
test' filename

不知为什么 如果这样就得不到结果:
sed '/ccc/i\test'  filename

那位大侠指点一下?
作者: icesummit    时间: 2005-07-12 00:05
标题: 請問如何在已知的行號的前一行插入字串?
原帖由 "liuyunxiaoyan" 发表:
sed '/ccc/i\
test' filename

不知为什么 如果这样就得不到结果:
sed '/ccc/i\test'  filename

那位大侠指点一下?


红色的一定要这么写才行:
test
' filename




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2