David Tatarakis
David Tatarakis

Reputation: 19

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.

Upvotes: 0

Views: 10689

Answers (3)

Rahul Verma
Rahul Verma

Reputation: 3079

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

Upvotes: 0

ctac_
ctac_

Reputation: 2471

Why not simpler :

awk -F'\t' '{print $8}' inputfile.gtf  > newfile.tsv

Upvotes: 4

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

You have specified the wrong delimiter /t, the tab character typed as \t:

awk 'BEGIN{ FS=OFS="\t" }{ print $8 }' inputfile.gtf  > newfile.tsv

Upvotes: 1

Related Questions