我正在使用head
和awk
的组合来计算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
答案 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