简体   繁体   English

如何在bash脚本输出中包含tsv / csv标头

[英]How to include tsv/csv header in bash script output

I have thousands of .tsv files where I am extracting the rows where column 2 is equal column 6. 我有成千上万个.tsv文件,在其中提取第2列等于第6列的行。

I can use the below bash script, but I could not append column names (header) in the output. 我可以使用下面的bash脚本,但是无法在输出中追加列名(标题)。

What is the way to include header? 包含标头的方式是什么?

for x in *.tsv; do
   awk '$2==$6' <"$x" >"$x.tmp"
   mv "$x.tmp" "$x"
done

If you want to print based on two conditions, say so: 如果要基于两个条件进行打印,请说出:

awk 'FNR==1 || $2==$6' file

This will print those lines that either of these: 这将打印以下任一行:

  • match the $2==$6 condition. 匹配$2==$6条件。
  • are the first line. 是第一行。

Also, note you don't need to loop with bash, awk can do it: 另外,请注意,您不需要循环使用bash, awk可以做到:

awk '(FNR==1 || $2==$6) {print > FILENAME".bk"}' *.tsv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM