简体   繁体   中英

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. 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. 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.

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; 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 . Also FS="\\t" is similar to -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 :

awk '{print $8}' inputfile.gtf  > newfile.tsv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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