将标题添加到制表符分隔的文件 [英] Add a header to a tab delimited file

查看:87
本文介绍了将标题添加到制表符分隔的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将标题添加到制表符分隔的文件中,但是我不确定如何在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

这篇关于将标题添加到制表符分隔的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆