Linux 如何在shell脚本中向文件添加一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/487894/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:55:28  来源:igfitidea点击:

How can I add a line to a file in a shell script?

linuxshellsed

提问by user4812

I want to add a row of headers to an existing CSV file, editing in place. How can I do this?

我想在现有的 CSV 文件中添加一行标题,就地编辑。我怎样才能做到这一点?

echo 'one, two, three' > testfile.csv

and I want to end up with

我想结束

column1, column2, column3
one,     two,     three

Changing the initial CSV output is out of my hands.

更改初始 CSV 输出不在我的掌握之中。

Any standard command will do. The important thing is the file is edited in place, and the line is inserted at the beginning of the file.

任何标准命令都可以。重要的是文件被编辑到位,并且在文件的开头插入了该行。

采纳答案by tunnuz

This adds custom text at the beginning of your file:

这会在文件开头添加自定义文本:

echo 'your_custom_escaped_content' > temp_file.csv
cat testfile.csv >> temp_file.csv
mv temp_file.csv testfile.csv

回答by Scottie T

This doesn't use sed, but using >> will append to a file. For example:

这不使用 sed,但使用 >> 将附加到文件。例如:

echo 'one, two, three' >> testfile.csv

Edit: To prepend to a file, try something like this:

编辑:要添加到文件,请尝试以下操作:

echo "text"|cat - yourfile > /tmp/out && mv /tmp/out yourfile

I found this through a quick Google search.

我通过快速的谷歌搜索找到了这个。

回答by Steve B.

sed is line based, so I'm not sure why you want to do this with sed. The paradigm is more processing one line at a time( you could also programatically find the # of fields in the CSV and generate your header line with awk) Why not just

sed 是基于行的,所以我不确定您为什么要使用 sed 来执行此操作。范式更多的是一次处理一行(您也可以以编程方式找到 CSV 中的字段数并使用 awk 生成标题行)为什么不只是

echo "c1, c2, ... " >> file
cat testfile.csv >> file

?

?

回答by mouviciel

As far as I understand, you want to prepend column1, column2, column3to your existing one, two, three.

据我了解,您想column1, column2, column3在现有的one, two, three.

I would use edin place of sed, since sedwrite on the standard output and not in the file.

我会用ed代替sed,因为sed写在标准输出上而不是在文件中。

The command:

命令:

printf '0a\ncolumn1, column2, column3\n.\nw\n' | ed testfile.csv

should do the work.

应该做的工作。

perl -iis worth taking a look as well.

perl -i也值得一看。

回答by tunnuz

Use perl -i, with a command that replaces the beginning of line 1 with what you want to insert (the .bk will have the effect that your original file is backed up):

使用 perl -i 命令,将第 1 行的开头替换为您要插入的内容(.bk 将具有备份原始文件的效果):

perl -i.bk -pe 's/^/column1, column2, column3\n/ if($.==1)' testfile.csv  

回答by Matthew Crumley

To answer your original question, here's how you do it with sed:

要回答您的原始问题,请按以下方法使用 sed:

sed -i '1icolumn1, column2, column3' testfile.csv

The "1i" command tells sed to go to line 1 and insert the text there.

"1i" 命令告诉 sed 转到第 1 行并在那里插入文本。

The -i option causes the file to be edited "in place" and can also take an optional argument to create a backup file, for example

-i 选项导致文件被“就地”编辑,并且还可以使用可选参数来创建备份文件,例如

sed -i~ '1icolumn1, column2, column3' testfile.csv

would keep the original file in "testfile.csv~".

将原始文件保存在“testfile.csv~”中。

回答by tflutre

Add a given line at the beginning of a file in two commands:

用两个命令在文件开头添加给定的行:

cat <(echo "blablabla") input_file.txt > tmp_file.txt
mv tmp_file.txt input_file.txt