我有一个文件,它是多个管道命令的输出。像这样的东西
command1 input.txt| command2 | command3 | input file
文件以制表符分隔
命令 3 之后,我的输入文件如下所示
chr6 116732135 116741866 116732135 116732368 116741505 116741866 + 0.79 0.51 0.97 0.77 0.48 0.97 0.02 0.37 'chr6:116732136-116732368:+@chr6:116741506-116741866:+.A.withRI','chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.up_chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.dn' (0,0):10,(1,0):147,(1,1):1 0:148 (0,0):36,(1,0):161,(1,1):3 0:163,1:1 chr6 + 116732136,116732136 116741866,116741866 gene_id "ENSG00000196911.10"; transcript_id "ENST00000368564.6"; gene_type "protein_coding"; gene_name "KPNA5"; transcript_type "protein_coding"; transcript_name "KPNA5-202"; level 2; protein_id "ENSP00000357552.1"; transcript_support_level "1"; tag "basic"; tag "appris_principal_1"; tag "CCDS"; ccdsid "CCDS5111.1"; havana_gene "OTTHUMG00000015448.4"; havana_transcript "OTTHUMT00000041967.2";
chr6 116732135 116741866 116732135 116732368 116741505 116741866 + 0.79 0.51 0.97 0.77 0.48 0.97 0.02 0.37 'chr6:116732136-116732368:+@chr6:116741506-116741866:+.A.withRI','chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.up_chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.dn' (0,0):10,(1,0):147,(1,1):1 0:148 (0,0):36,(1,0):161,(1,1):3 0:163,1:1 chr6 + 116732136,116732136 116741866,116741866 gene_id "ENSG00000196911.10"; transcript_id "ENST00000356348.6"; gene_type "protein_coding"; gene_name "KPNA5"; transcript_type "protein_coding"; transcript_name "KPNA5-201"; level 2; protein_id "ENSP00000348704.1"; transcript_support_level "1"; tag "basic"; tag "appris_principal_1"; tag "CCDS"; ccdsid "CCDS5111.1"; havana_gene "OTTHUMG00000015448.4"; havana_transcript "OTTHUMT00000041969.2";
在命令 3 之后,我使用 awk 命令来分割最后一列,使用;
以下命令
command1 input.txt| command2 | command3 | awk -F "\t" -v OFS="\t" '{split($NF,a,";"); $NF=""; print $0,a[1],a[4]}'
我想分割从 command3 获取的文件的最后一个字段,然后打印除最后一个字段之外的所有字段,然后打印分割字段 a[1] 和 a[4],但这会在第 1-25 列之间添加一个选项卡一个[1],一个[4]。我怎样才能避免这种情况?
谢谢
这是输出
chr6 116732135 116741866 116732135 116732368 116741505 116741866 + 0.79 0.51 0.97 0.77 0.48 0.97 0.02 0.37 'chr6:116732136-116732368:+@chr6:116741506-116741866:+.A.withRI','chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.up_chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.dn' (0,0):10,(1,0):147,(1,1):1 0:148 (0,0):36,(1,0):161,(1,1):3 0:163,1:1 chr6 + 116732136,116732136 116741866,116741866 gene_id "ENSG00000196911.10" gene_name "KPNA5"
chr6 116732135 116741866 116732135 116732368 116741505 116741866 + 0.79 0.51 0.97 0.77 0.48 0.97 0.02 0.37 'chr6:116732136-116732368:+@chr6:116741506-116741866:+.A.withRI','chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.up_chr6:116732136-116732368:+@chr6:116741506-116741866:+.B.dn' (0,0):10,(1,0):147,(1,1):1 0:148 (0,0):36,(1,0):161,(1,1):3 0:163,1:1 chr6 + 116732136,116732136 116741866,116741866 gene_id "ENSG00000196911.10" gene_name "KPNA5"
答案1
所以,给定
$ printf 'foo\tbar\ta;b;c;d' |
awk -F "\t" -v OFS="\t" '{split($NF,a,";"); $NF=""; print $0,a[1],a[4]}' | cat -A
foo^Ibar^I^Ia^Id$
(我用来cat -A
显示选项卡以^I
方便可视化)您想消除双选项卡吗?
如果是这样,一种方法是递减NF
而不是将空字符串分配给$NF
:
$ printf 'foo\tbar\ta;b;c;d' |
awk -F "\t" -v OFS="\t" '{split($NF,a,";"); NF--; print $0,a[1],a[4]}' | cat -A
foo^Ibar^Ia^Id$
另一种方法是连接字符串而不是将它们打印为字段 - 您可以通过删除,
它们之间的内容来做到这一点:
$ printf 'foo\tbar\ta;b;c;d' |
awk -F "\t" -v OFS="\t" '{split($NF,a,";"); $NF=""; print $0 a[1],a[4]}' | cat -A
foo^Ibar^Ia^Id$