bash 在csv文件中插入一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13402809/
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
insert a line in csv file
提问by frazman
I have a huge csv file (on order of terabytes).
我有一个巨大的 csv 文件(按 TB 级计算)。
Now, I want to insert one row which is a header to the the top.
现在,我想插入一行,它是顶部的标题。
For example if input.csv looks like this:
例如,如果 input.csv 如下所示:
1,2,3,4
22,3,23,1
I want it to look like
我希望它看起来像
id1,id2,id3,id4
1,2,3,4
and so on
How do i do this from shell, terminal, awk, bash?/
我如何从 shell、终端、awk、bash 执行此操作?/
回答by Lee Netherton
In place, using sed:
就地,使用 sed:
sed -i 1i"id1,id2,id3,id4" file.csv
edit:
编辑:
As @Ed Morton points out, using sed with the -i
switch sed edits the file in place, and can therefore be dangerous when editing large files. If you supply a prefix after the -i
option then sed creates a backup. So something like this would be safer:
正如@Ed Morton 指出的那样,将 sed 与-i
开关sed 一起使用可以就地编辑文件,因此在编辑大文件时可能会很危险。如果您在-i
选项后提供前缀,则 sed 会创建一个备份。所以这样的事情会更安全:
sed -i.bak 1i"id1,id2,id3,id4" file.csv
The original file will then be located in file.csv.bak
然后原始文件将位于 file.csv.bak
回答by Gilles Quenot
This is that simple as :
这很简单:
{ echo "id1,id2,id3,id4"; cat file.csv; } > newfile.csv
using simple shell concatenation.
使用简单的外壳连接。
EDIT
编辑
after discussion thread below, I propose this :
经过下面的讨论,我提出这个建议:
- create a file with your header, said
head.txt
- 用你的标题创建一个文件,说
head.txt
Then :
然后 :
cat head.txt file.csv > newfile.csv
回答by gniourf_gniourf
Edit.When I wrote this answer, I overlooked the "terabyte" part of the question. Hence, do not use the method presented here. I still leave this post, as it advertises the use of this wonderful tool, ed
, the standard text editor.
编辑。当我写这个答案时,我忽略了问题的“太字节”部分。因此,请勿使用此处介绍的方法。我仍然离开这篇文章,因为它宣传了这个很棒的工具,ed
标准文本编辑器的使用。
As usual, ed
is the standard text editor. The solution using sed -i
doesn't, as it mentions, "edit the file in place". Instead, it outputs its content to a temporary file, and then renames this file to the original one. That's really not good for large files!
像往常一样,ed
是标准的文本编辑器。sed -i
正如它所提到的,使用的解决方案不是“就地编辑文件”。相反,它将其内容输出到一个临时文件,然后将该文件重命名为原始文件。这对大文件真的不好!
Using ed
instead really edits the file. Something along the following lines:
使用ed
代替真正编辑文件。大致如下:
#!/bin/bash
file="input.csv"
{
ed -s "$file" <<EOF
1
i
id1,id2,id3,id4
.
wq
EOF
} > /dev/null
Explanation: 1
goes to the first line, i
goes into insert mode, then we insert id1,id2,id3,id4
then .
to go back to normal mode, and wq
to write and quit.
解释:1
到第一行,i
进入插入模式,然后我们插入id1,id2,id3,id4
然后.
回到正常模式,然后wq
写入并退出。
With this method, you're really editing the file and it's twice faster than the sed method. Also, ed
is known to be "large file safe"!
使用这种方法,您实际上是在编辑文件,而且它比 sed 方法快两倍。此外,ed
已知是“大文件安全”!
Done.
完毕。
回答by shellter
There's no easy way, you're going to have to rewrite the file. Probably the safest way is to
没有简单的方法,您将不得不重写文件。可能最安全的方法是
( echo "id1,id2,id3,id4" ; cat file ) > newFile && rm file
IHTH
IHTH