在 Linux 中的特定行添加文本到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15157659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add text to file at certain line in Linux
提问by user2123459
I want to add a specific line, lets say avatar
to the files that starts with MakeFile
and avatar
should be added to the 15th line in the file.
我想添加一个特定的行,让我们说avatar
以开头的文件,MakeFile
并且avatar
应该添加到文件的第 15 行。
This is how to add text to files:
这是向文件添加文本的方法:
echo 'avatar' >> MakeFile.websvc
and this is how to add text to files that starts with MakeFile I think:
这就是我认为如何向以 MakeFile 开头的文件添加文本:
echo 'avatar' >> *MakeFile.
But I can not manage to add this line to the 15th line of the file.
但我无法将此行添加到文件的第 15 行。
采纳答案by Sunil Bojanapally
You can use sed
to solve this:
你可以用它sed
来解决这个问题:
sed "15i avatar" Makefile.txt
or use the -i
option to save the changes made to the file.
或使用该-i
选项保存对文件所做的更改。
sed -i "15i avatar" Makefile.txt
To change all the files beginning that start Makefile
:
要更改以 start 开头的所有文件Makefile
:
sed "15i avatar" Makefile*
Note: In the above 15
is your line of interest to place the text.
注意:在上面15
是您感兴趣的行放置文本。
回答by Vijay
perl -pi -e 'if($.==14){s/\n/\navatar\n/g}if(eof){$.=0}' MakeFile*
回答by lmsteffan
Using sed :
使用 sed :
sed -i '15i\avatar\' Makefile*
where the -i option indicates that transformation occurs in-place (which is useful for instance when you want to process several files).
其中 -i 选项表示转换就地发生(例如,当您要处理多个文件时,这很有用)。
Also, in your question, *MakeFile stands for 'all files that end with MakeFile', while 'all files that begin with MakeFile' would be denoted as MakeFile*.
此外,在您的问题中,*MakeFile 代表“以 MakeFile 结尾的所有文件”,而“以 MakeFile 开头的所有文件”将表示为 MakeFile*。
回答by Chris Koknat
If you need to pass in the string and the line-number options to the script, try this:
如果您需要将字符串和行号选项传递给脚本,请尝试以下操作:
perl -i -slpe 'print $s if $. == $n; $. = 0 if eof' -- -n=15 -s="avatar" Makefile*
-i
edit the input file, do not make a backup copy$.
is the line number
-i
编辑输入文件,不做备份副本$.
是行号
This is based on my solution to Insert a line at specific line number with sed or awk, which contains several other methods of passing options to Perl, as well as explanations of the command line options.
这是基于我使用 sed 或 awk 在特定行号插入一行的解决方案,其中包含其他几种将选项传递给 Perl 的方法,以及对命令行选项的解释。
回答by Wildcard
If you want a more portable version, you can use ex
, which will work on any*Nix system. (It's specified by POSIX.) The Sed commands given so far depend on GNU Sed.
如果你想要一个更便携的版本,你可以使用ex
,它可以在任何*Nix 系统上运行。(它由 POSIX 指定。)到目前为止给出的 Sed 命令依赖于 GNU Sed。
To insert a line containing nothing but "avatar" at the 15th line of each file in the current directory whose name begins with "Makefile.", use:
要在当前目录中名称以“Makefile.”开头的每个文件的第 15 行插入仅包含“avatar”的行,请使用:
for f in MakeFile.*; do printf '%s\n' 15i 'avatar' . x | ex "$f"; done