在终端中使用AWK在CSV中添加列标题 [英] Adding Column Headers in CSV with AWK in Terminal

查看:76
本文介绍了在终端中使用AWK在CSV中添加列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想在终端中使用awk在CSV文件中添加列标题.

Hello I'm trying to add column headers in my CSV file with awk in terminal.

当前显示:

Michael, Michael@email.com, Seattle, Washington, detail1, detail2, detail3

所需格式:

Name, Email, City, State, more1, more2, more3etc
Michael, Michael@email.com, Seattle, Washington, detail1, detail2, detail3,

已经尝试过类似的操作,但是最终我收到源代码行2上的非法声明":

Have tried something like this, but I end up getting "Illegal Statement at Source line 2":

awk 'BEGIN {FS=",";OFS=","; print "Rating,Restaurant,Address,Landline,Mobile,Cuisine,Website,LatDD,LonDD"}
NR >1 {print ",",,,",",",", }' Restaurants.txt > Restaurants_newer.txt

推荐答案

可以做到

sed '1iName, Email, City, State, more1, more2, more3etc' file > newfile

也可以就地完成

sed -i '1iName, Email, City, State, more1, more2, more3etc' file

{ echo 'Name, Email, City, State, more1, more2, more3etc'; cat file; } > newfile

这篇关于在终端中使用AWK在CSV中添加列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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