如何写入第10行?

发布于 2024-10-16 02:49:12 字数 395 浏览 3 评论 0原文

我需要一个可以将文本写入从第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你曾走过我的故事 2024-10-23 02:49:12

使用 sed

sed -i -e '10a\
new stuff' file

使用 bash

IFS=

请注意,我不太确定您的意思是在第 10 行插入还是在第 11 行插入,因此请仔细检查我编写 的位置10 上面。您可能需要 9 用于 sed 命令,或 11 用于 bash 版本。

perl 中,您可以使用 $NR 变量。

open FILEHANDLE, "<file";
while (<FILEHANDLE>) {
    if ($NR == 10) {
        # do something
    }
}

awk 中,它是 NR

awk 'NR != 10 { print }
NR == 10 { print "Something else" }' file

但请注意,您可以查找并替换空行,例如

sed -i -e 's/^$/replacement text/' file
\n' i=0 while read -r line; do i=$((i+1)) if test $i -eq 10; then echo "new stuff" else echo "$line" fi done <file >file.tmp mv file.tmp file

请注意,我不太确定您的意思是在第 10 行插入还是在第 11 行插入,因此请仔细检查我编写 的位置10 上面。您可能需要 9 用于 sed 命令,或 11 用于 bash 版本。

perl 中,您可以使用 $NR 变量。

awk 中,它是 NR


但请注意,您可以查找并替换空行,例如

Using sed:

sed -i -e '10a\
new stuff' file

Using bash:

IFS=

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 want 9 for the sed command or 11 for the bash version.

In perl, you can use the $NR variable.

open FILEHANDLE, "<file";
while (<FILEHANDLE>) {
    if ($NR == 10) {
        # do something
    }
}

And in awk, it's NR.

awk 'NR != 10 { print }
NR == 10 { print "Something else" }' file

But note that you can find and replace a blank line, e.g.

sed -i -e 's/^$/replacement text/' file
\n' i=0 while read -r line; do i=$((i+1)) if test $i -eq 10; then echo "new stuff" else echo "$line" fi done <file >file.tmp mv file.tmp file

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 want 9 for the sed command or 11 for the bash version.

In perl, you can use the $NR variable.

And in awk, it's NR.


But note that you can find and replace a blank line, e.g.

故笙诉离歌 2024-10-23 02:49:12

使用 sed:

sed '10 s/^/text' file >file.new && mv file.new file

使用 gnu-sed:

sed -i '10 s/^/text' file

使用 awk:

awk 'NR==10 {$0="text"} 1' file >filen.new && mv file.new file

With sed:

sed '10 s/^/text' file >file.new && mv file.new file

With gnu-sed:

sed -i '10 s/^/text' file

With awk:

awk 'NR==10 {$0="text"} 1' file >filen.new && mv file.new file
木格 2024-10-23 02:49:12

您可以这样做:

head -10 fileA > fileB; echo "new text" >> fileB;

请注意,这比使用 sed 快一点

You could just do:

head -10 fileA > fileB; echo "new text" >> fileB;

Note that this is fractionally quicker than doing it with sed

木有鱼丸 2024-10-23 02:49:12
sed 10cFoobar foo

将第 10 行替换为“Foobar”。这可能包含换行符:

sed 4cFoobar"\nFoobar" foo

这与使用 a 而不是 c 的 Mikel 解决方案形成对比。区别在于,一个是替换,另一个是追加。

sed 10cFoobar foo

replaces line 10 with "Foobar". This may contain line breaks:

sed 4cFoobar"\nFoobar" foo

This is in contrast to Mikel's solution who used a instead of c. The difference is that one replaces, the other one appends.

调妓 2024-10-23 02:49:12

如果文件已经有 9 行,那么在 shell 上会很容易;)

$ echo "I am line 10" >> file-with-9-lines.txt

如果文件可能包含少于 9 行甚至可能更大(并且您仍然想写入第 10 行)perl modul Tie::File 将使事情变得非常简单:

#!/usr/bin/perl
use Tie::File;
tie my @file, 'Tie::File', '/path/to/some/file' or die $!;
$file[9] = "I am the content of line 10";

If the file already has 9 lines, it would be easy on the shell ;)

$ echo "I am line 10" >> file-with-9-lines.txt

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:

#!/usr/bin/perl
use Tie::File;
tie my @file, 'Tie::File', '/path/to/some/file' or die $!;
$file[9] = "I am the content of line 10";
雨后彩虹 2024-10-23 02:49:12

红宝石 (1.9+)

$ ruby -i.backup -ne 'print ($.==10)?"word\n":$_' file

Ruby (1.9+)

$ ruby -i.backup -ne 'print ($.==10)?"word\n":$_' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文