将标题添加到制表符分隔的文件

问题描述:

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

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.

让我们说我的文件是:

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

我将如何在linux的一行中做到这一点?我对awk,sed,cat等没什么好感,但是对perl一点都不熟悉.

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.

没有像"append"运算符>>这样的"prepend"运算符,但是您可以将标头写入临时文件,然后复制之后将文件的内容放入临时文件,然后将其移回:

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