将文件名和空列添加到awk中的现有文件 [英] Add file name and empty column to existing file in awk
问题描述
亲爱的我想在一个awk步骤中将"file name"和"empty column"及其标头添加到我的现有文件中:
Dear all I would like to add to my existing file "file name" and "empty column" including their header in one awk step:
输入:
head1 head2 head3
value1 value2 value3
输出:
filename note head1 head2 head3
file1 empty_column value1 value2 value3
因此,我仅将标题添加到文件名"和注释".文件名"下是我的文件名,注释下是空字段.
So I just add to header "Filename" and "Note". Under the Filename is the name of my file and under the note is empty field.
我可以使用两个awk命令来完成此操作并将其粘贴到一起,但是有一种方法可以做到这一点吗?
I can do this with two awk commands and paste it together, but is there way to do it in one-liner?
我的想法是:
awk -v OFS="\t" '{print FILENAME, $2="",$3 - end}' infile
但是不知道如何添加标题和如何打印移动的字段(窗体位置从$ 1到$ 3).
But do not know how to add header and how to print moved fields (form position $1 to $3).
注意:我大约有77列-不仅像示例中的三列.
Note: I have about 77 columns - not only three like in example.
谢谢您的任何想法.
推荐答案
尝试一下:
awk '{$0=FILENAME "\t" (NR==1?"note":"") "\t" $0}7' file
根据评论进行更新:
awk '{$0=(NR==1? "NAME\tnote":FILENAME"\t") "\t" $0}7' file
输出:
NAME note head1 head2 head3
file value1 value2 value3
这篇关于将文件名和空列添加到awk中的现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!