awk来计算和打印标题行的文件

时间:2017-04-14 12:36:10

标签: awk

我正在使用headawk的组合来计算tab-delimited file标题行中的字段数。下面似乎非常接近,但我还想在换行符上打印file中的标题名称。可能有更好的方法,但希望它是一个开始。谢谢你:)。

文件

Index   Chr Start   End Ref ALT
带有当前输出的

awk

head -n 1 file | awk -F'\t' '{print NF " fields detected in file"}'
6 fields detected in file

所需的输出

6 fields detected in file
Index   Chr Start   End Ref ALT

3 个答案:

答案 0 :(得分:2)

试试这个 -

$ awk ' {print NF " fields detected in file"} END {print}' f
6 fields detected in file
Index   Chr Start   End Ref ALT

$ awk ' {print NF " fields detected in file"RS $0;exit}' f
6 fields detected in file
Index   Chr Start   End Ref ALT

答案 1 :(得分:1)

如果您的文件不仅仅是标题,还包含数据

awk -F'\t' '{print NF " fields detected in file"; print; exit}' file

您不需要head声明。但是,您只是检查标题,更好的QC脚本应检查所有记录长度。

答案 2 :(得分:1)

awk -F'\t' '{print NF, "fields detected in file" ORS $0; exit}' file