[英]How to add row name in tab delimited file with header in Linux
I am dealing with a table file with thousands line and header. 我正在处理具有数千行和标题的表文件。 But I have to add row name (row name represent row number) in each line.
但是我必须在每行中添加行名(行名代表行号)。 How could I do this in linux?
我如何在Linux中执行此操作?
I can done this with excel, but there are too many files so I wish I could write down linux command to deal with it. 我可以使用excel完成此操作,但是文件太多,所以我希望可以写下linux命令来处理它。
My file looks like this. 我的文件看起来像这样。
parameter1 parameter2 paramter3
1.2 1.3 1.4
2.1 2.1 1.5
1.5 1.6 2.1
....
And I would like to added row name as following: 我想添加如下行名:
sim parameter1 parameter2 paramter3
AM_1_arp 1.2 1.3 1.4
AM_2_arp 2.1 2.1 1.5
AM_3_arp 1.5 1.6 2.1
....
How could I done these with linux commands? 我如何用linux命令完成这些操作?
With awk: 使用awk:
awk 'NR==1{print "sim "$0;next}{print "AM_"NR-1"_arp "$0}' yourfile
That says: 说的是:
NR==1
then print "sim " plus the entire line: print "sim "$0;
NR==1
则打印“ sim”加上整行: print "sim "$0;
next
next
NR
plus "_arp " then the entire line $0
. NR
加“ _arp”,然后打印整行$0
。 If you have many files in a directory for which you need to do this, you can reference them by wildcard and you can redirect the output inside of awk: 如果目录中有许多文件需要执行此操作,则可以通过通配符引用它们,并且可以将输出重定向到awk内部:
awk 'NR==1{print "sim "$0 > FILENAME"_out";next}{print "AM_"NR-1"_arp "$0 > FILENAME"_out"}' ./yourfile*
awk 'BEGIN {i=0} {print "AM_" i++ "_arp " $0}' <file> | sed 's/AM_0_arp/sim/g'
我只是将您选择的字符串添加到每行开头之间,并在其之间添加一个计数器,然后将第一个实例替换为sim作为标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.