Linux 向制表符分隔的文件添加标题

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

Add a header to a tab delimited file

linuxunixsedawkcat

提问by Dnaiel

I'd like to add a header to a tab-delimited file but I am not sure how to do it in one line in linux.

我想在制表符分隔的文件中添加一个标题,但我不确定如何在 linux 中的一行中执行此操作。

Let us say my file is:

让我们说我的文件是:

roger\t18\tcolumbia\tnew york\n
albert\t21\tdartmouth\tnew london\n
etc...

and now I'd like to add a header that says:

现在我想添加一个标题,上面写着:

name\tage\tuniversity\tcity

How would I do that in one line in linux? I am ok with awk, sed, cat, etc. not familiar at all with perl though.

我将如何在 linux 中的一行中做到这一点?我可以使用 awk、sed、cat 等,但对 perl 完全不熟悉。

采纳答案by newfurniturey

There isn't a "prepend" operator like the "append" operator >>, but you can write the header to a temp-file, copy your file's contents into the temp-file after that, and move it back:

没有像“追加”运算符那样的“前置”运算符>>,但是您可以将标头写入临时文件,然后将文件内容复制到临时文件中,然后将其移回:

echo -e "name\tage\tuniversity\tcity" | cat - yourfile > /tmp/out && mv /tmp/out yourfile

回答by wallyk

First create a file with the header content:

首先创建一个包含标题内容的文件:

$ cat >header
name^Iage^Iuniversity^Icity (return)
^D

(where ^Iis the tab key)

^Itab键在哪里)

Then prepend it to the data

然后将其添加到数据中

$ cat header myfile >newfile
$ mv newfile myfile

回答by William Pursell

$ { printf 'name\tage\tuniversity\tcity\n'; cat orig-file; } > new-file

Or

或者

$ printf '1\ni\nname\tage\tuniversity\tcity\n.\nw\n' | ed -s orig-file

回答by David Z

Personally I would go with nano -w file.txt;-) (i.e. just use a text editor, doesn't have to be nano of course)

我个人会选择nano -w file.txt;-) (即只使用文本编辑器,当然不必是 nano)

But if you wanted to do this in a non-interactive environment for some reason, you can use catfor all sorts of concatenations:

但是,如果您出于某种原因想在非交互式环境中执行此操作,则可以将其cat用于各种串联:

echo $'name\tage\tuniversity\tcity' | cat - file.txt > file2.txt

will prepend the header and put the output in file2.txt. If you want to overwrite the original file you can do it with

将添加标题并将输出放入file2.txt. 如果你想覆盖原始文件,你可以用

echo $'name\tage\tuniversity\tcity' | cat - file.txt > file2.txt; mv file{2,}.txt

Or you could use sedas follows:

或者你可以使用sed如下:

sed -i $'1 i\\nname\tage\tuniversity\tcity' file.txt

Note that I'm using $'...'quoting to allow me to use \tto represent tab and \nto represent newline (among other substitutions; see the bash man page for more). In this type of quoted string, \\represents a literal backslash. So the program passed to sed is actually

请注意,我正在使用$'...'引用来\t表示制表符和\n换行符(在其他替换中;有关更多信息,请参见 bash 手册页)。在这种类型的带引号的字符串中,\\表示文字反斜杠。所以传递给 sed 的程序实际上是

1 i\
name    age     university      city

回答by Vijay

perl -i -lne 'if($.==1){print "newline\n$_"}else{print}' your_file

回答by WCC

cat <(head -1 theFileWithHeader) theFileWithoutHeader > newfile;
mv newfile theFileWithoutHeader;

回答by Michael Hall

Customary awkanswer.

习惯性awk回答。

awk 'BEGIN { print "name\tage\tuniversity\tcity" } { print }' yourfile > /tmp/out && mv /tmp/out yourfile

回答by Digvijay S

Using sedno need of temp file

使用sed不需要临时文件

sed -i "s#^#name\tage\tuniversity\tcity#g#"

Demo:

演示:


$ cat file1.txt
roger\t18\tcolumbia\tnew york\n
albert\t21\tdartmouth\tnew london\n
etc...
$ sed -i "s#^#name\tage\tuniversity\tcity#g#" file1.txt $ cat file1.txt
name    age     university      cityroger\t18\tcolumbia\tnew york\n
name    age     university      cityalbert\t21\tdartmouth\tnew london\n
name    age     university      cityetc...
$