如何写入第10行?
我需要一个可以将文本写入从第 10 行开始的现有文件的脚本。它是一个空行,因此不会是查找/替换。最好是在 bash 中,但是终端可以解释的任何内容都可以正常工作。
RE-EDITED:
抱歉,但在我测试后仍然存在一些问题...认为这与我想要写入文件的内容有关。也许这会让事情变得更容易..
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 zone "$zone" in {
12 type master;
13 file "/etc/bind/db.$zone";
14 };
15 k
16 l
17 m
提前致谢, 乔
I need a script that can write text to an exsisting file starting on line 10. It is a blank line so it wont be a find / replace. Would like preferably it to be in bash, but anything that the terminal can interpret will work just fine.
RE-EDITED:
Sorry but still having a bit of a problem after I tested... Think it has something to do with what I want write to a file. Maybe this will make it easier..
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 zone "$zone" in {
12 type master;
13 file "/etc/bind/db.$zone";
14 };
15 k
16 l
17 m
Thanks in Advance,
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
sed
:使用
bash
:请注意,我不太确定您的意思是在第 10 行插入还是在第 11 行插入,因此请仔细检查我编写
的位置10 上面。您可能需要
9
用于sed
命令,或11
用于bash
版本。在
perl
中,您可以使用$NR
变量。在
awk
中,它是NR
。但请注意,您可以查找并替换空行,例如
Using
sed
:Using
bash
:Note that I'm not really sure if you mean insert at line 10 or at line 11, so double check the places I wrote
10
above. You might want9
for thesed
command or11
for thebash
version.In
perl
, you can use the$NR
variable.And in
awk
, it'sNR
.But note that you can find and replace a blank line, e.g.
使用 sed:
使用 gnu-sed:
使用 awk:
With sed:
With gnu-sed:
With awk:
您可以这样做:
请注意,这比使用
sed
快一点You could just do:
Note that this is fractionally quicker than doing it with
sed
将第 10 行替换为“Foobar”。这可能包含换行符:
这与使用 a 而不是 c 的 Mikel 解决方案形成对比。区别在于,一个是替换,另一个是追加。
replaces line 10 with "Foobar". This may contain line breaks:
This is in contrast to Mikel's solution who used a instead of c. The difference is that one replaces, the other one appends.
如果文件已经有 9 行,那么在 shell 上会很容易;)
如果文件可能包含少于 9 行甚至可能更大(并且您仍然想写入第 10 行)perl modul Tie::File 将使事情变得非常简单:
If the file already has 9 lines, it would be easy on the shell ;)
If the file may contain less than 9 lines or even may be larger (and you still want to write into line 10) the perl modul Tie::File will make things realy easy:
红宝石 (1.9+)
Ruby (1.9+)