如何向shell脚本中的文件添加行?

时间:2022-01-22 06:19:25

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.

任何标准命令都可以。重要的是在适当的位置编辑文件,并在文件的开头插入行。

7 个解决方案

#1


28  

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

#2


64  

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~”中。

#3


7  

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

据我所知,您希望将column1、column2、column3提前到现有的1,2,3。

I would use ed in place of sed, since sed write 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 -i is worth taking a look as well.

perl -我也值得一看。

#4


6  

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.

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

#5


3  

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

?

吗?

#6


1  

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  

#7


1  

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

#1


28  

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

#2


64  

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~”中。

#3


7  

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

据我所知,您希望将column1、column2、column3提前到现有的1,2,3。

I would use ed in place of sed, since sed write 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 -i is worth taking a look as well.

perl -我也值得一看。

#4


6  

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.

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

#5


3  

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

?

吗?

#6


1  

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  

#7


1  

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