使用awk编辑大文本文件时出错 [英] error when edditing big text file using awk

查看:474
本文介绍了使用awk编辑大文本文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,如下例所示:

I have a text file like the following small example:

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开头.每行都有一些列,分隔符可以是制表符或;". 我要从中创建一个新文件,其中仅第4列和第5列会有变化.实际上,新文件中的第4列为((column 4 in original file)-12),而新文件中的第5列为((column 4 in original file)+50).我尝试使用以下命令在awk中做到这一点:

in the file there are different lines. each line starts with chr. every line has some columns and separators are either tab or ";". I want to make a new file from this one in which there would be a change only in columns 4 and 5. in fact column 4 in the new file would be ((column 4 in original file)-12) and 5th column in the new file would be ((column 4 in original file)+50). I tried to do that in awk using the following command:

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

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

when I run the code, it would return this error:

awk: cmd. line:1: 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";" }
awk: cmd. line:1:                                                                                                                                                                                                                         ^ syntax error
awk: cmd. line:1: 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";" }
awk: cmd. line:1:                                                                                                                                                                                                                                      ^ syntax error

您知道如何解决吗?

推荐答案

您基本上要做的是:

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

您需要做的是:

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

^^^请注意,您需要在内联分配中加上括号.

^^^ Note that you need to parenthesize your inline assignments.

或者只是在打印前做作业:

Or just do the assignments before printing:

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

这篇关于使用awk编辑大文本文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆