使用awk编辑大文本文件时出错

时间:2018-05-09 09:12:24

标签: awk

我有一个文本文件,如下面的小例子:

chr1    HAVANA  transcript  12010   13670   .   +   .   gene_id "ENSG00000223972.4"; transcript_id "ENST00000450305.2"; gene_type "pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; tr
anscript_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; havana_gene "OTTHUMG00000000961.2"; havana_tran
script "OTTHUMT00000002844.2";
chr2    HAVANA  exon    12010   12057   .   +   .   gene_id "ENSG00000223972.4"; transcript_id "ENST00000450305.2"; gene_type "pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript
_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 1; exon_id "ENSE00001948541.1"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; havana_gene
 "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";
chr3    HAVANA  exon    12179   12227   .   +   .   gene_id "ENSG00000223972.4"; transcript_id "ENST00000450305.2"; gene_type "pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript
_type "transcribed_unprocessed_pseudogene"; transcript_status "KNOWN"; transcript_name "DDX11L1-001"; exon_number 2; exon_id "ENSE00001671638.2"; level 2; ont "PGO:0000005"; ont "PGO:0000019"; havana_gene
 "OTTHUMG00000000961.2"; havana_transcript "OTTHUMT00000002844.2";

在文件中有不同的行。每行以chr开头。每行都有一些列,分隔符是tab或“;”。 我想从这个文件中创建一个新文件,其中仅在第4列和第5列中进行更改。事实上,新文件中的第4列将是((column 4 in original file)-12),新文件中的第5列将是{{ 1}}。我尝试使用以下命令在((column 4 in original file)+50)中执行此操作:

awk

当我运行代码时,它会返回此错误:

awk 'BEGIN { FS="\t;" } {print  $1"\t"$2"\t"$3"\t"$4=$4-12"\t"$5=$4+50"\t"$6"\t"$7"\t"$8"\t"$9" "$10";"$11" "$12";"$13" "$14";"$15" "$16";"$17" "$18";"$19" "$20";"$21" "$22";"$23" "$24";"$25" "$26";"$27" "$28";"$29" "$30";"$31" "$32";"$33" "$34";"$35" "$36";"$37" "$38";" }' input.txt > test2.txt

你知道怎么解决吗?

3 个答案:

答案 0 :(得分:0)

你基本上做的是:

awk '{print $1=$1+2 ";" $2=$1+2}' < input

您需要做的是:

awk '{print ($1=$1+2) ";" ($2=$1+2)}' < input

^^^请注意,您需要为内联作业添加括号。

或者只是在打印前进行分配:

awk '{$1=$1+2;$2=$1+2;print ... }' < input

答案 1 :(得分:0)

以下内容可能对您有所帮助,但要求不是那么明确。

awk -F"\t|;" '{val=$4;$4=$4-12;$5=$4+50} 1'  Input_file

答案 2 :(得分:0)

试试这个

awk ' {print $1 "\t" $2"\t"$3"\t"($4 - 12)"\t" ($5 + 50)"\t" $6 "\t"$7"\t"$8"\t"$9"\t"$10"\t"$11"\t"$12"\t"$13"\t"$14"\t"$15"\t"$16"\t"$17"\t"$18""$19" "$20""$21" "$22";"$23" "$24""$25" "$26""$27" "$28""$29" "$30""$31" "$32""$33" "$34""$35" "$36""$37" "$38""  }' input.txt > output.txt