[英]Using AWK to extract one column from a tab separated file
I know this is a simple question, but the awk command is literally melting my brain. 我知道这是一个简单的问题,但是awk命令确实使我的大脑融化了。 I have a tab separated file "inputfile.gtf" and I need to extract one column from it and put it into a new file "newfile.tsv" I cannot for the life of me figure out the proper syntax to do this with awk. 我有一个制表符分隔的文件“ inputfile.gtf”,我需要从中提取一列并将其放入新文件“ newfile.tsv”中。我一生都无法找出用awk执行此操作的正确语法。 Here is what I've tried: 这是我尝试过的:
awk -F, 'BEGIN{OFS="/t"} {print $8}' inputfile.gtf > newfile.tsv
also 也
awk 'BEGIN{OFS="/t";FS="/t"};{print $8}' inputfile.gtf > newfile.tsv
Both of these just give me an empty file. 这两个都给我一个空文件。 Everywhere I search, people seem to have completely different ways of trying to achieve this simple task, and at this point I am completely lost. 在我到处搜寻的地方,人们似乎有完全不同的方式来尝试完成此简单任务,而此时我已完全迷失了方向。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。
您指定了错误的定界符/t
, 制表符输入\\t
:
awk 'BEGIN{ FS=OFS="\t" }{ print $8 }' inputfile.gtf > newfile.tsv
为什么不简单:
awk -F'\t' '{print $8}' inputfile.gtf > newfile.tsv
Your 1st command : 您的第一个命令:
awk -F, 'BEGIN{OFS="/t"} {print $8}' inputfile.gtf > newfile.tsv
You are setting -F,
which is not required, as your file is not ,
comma separated. 要设置-F,
它不是必需的,因为你的文件不是,
逗号分隔。
next, OFS="/t"
: Syntax is incorrect, it should be OFS="\\t"
, but again you don't need this as you don't want to set Output fields separator as \\t
since you're printing only a single record and OFS is not at all involved in this case; 接下来, OFS="/t"
:语法不正确,应为OFS="\\t"
,但由于您不想将输出字段分隔符设置为\\t
,因此您也不需要此,因为您正在打印在这种情况下,仅涉及一个记录,OFS完全不涉及; unless you print atleast two fields. 除非您至少打印两个字段。
Your 2nd command : 您的第二条命令:
awk 'BEGIN{OFS="/t";FS="/t"};{print $8}' inputfile.gtf > newfile.tsv
Again it's not /t
it should be \\t
. 同样,它不是/t
应该是\\t
。 Also FS="\\t"
is similar to -F "\\t"
FS="\\t"
也类似于-F "\\t"
What you actually need is : 您实际需要的是:
awk -F"\t" '{print $8}' inputfile.gtf > newfile.tsv
or 要么
awk -v FS="\t" '{print $8}' inputfile.gtf > newfile.tsv
And if your file has just tabs
and your fields don't have spaces in between then you can simply use : 如果文件中只有tabs
而字段之间没有空格,则可以使用:
awk '{print $8}' inputfile.gtf > newfile.tsv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.