I have a file with tab-separated fields in the following format:

2-micron    251 1523    R0010W  .   +   SGD gene    .   ID=R0010W;Name=R0010W;gene=FLP1;Alias=FLP1;Ontology_term=GO:0003690,GO:0003697,GO:0005575,GO:0008301,GO:0009009,GO:0042150;Note=Site-specific%20recombinase%20encoded%20on%20the%202-micron%20plasmid%2C%20required%20for%202-micron%20plasmid%20propagation%20as%20part%20of%20a%20plasmid%20amplification%20system%20that%20compensates%20for%20any%20copy%20number%20decreases%20caused%20by%20missegregation%20events;dbxref=SGD:S000029654;orf_classification=Verified  0

I need to extract 2 columns (4th and last), which I have successfully done. But I also need to extract specific information from a column with more details. For example, I need to extract gene=foo from the 10th column.

So, in results I want the 4th column, gene information from 10th column and the last column, a total of 3 columns. How do I do that ?

  • 3
    I edited to make this clearer but next time, make sure you mention what separates your columns (a tab in this case), that is essential since most people here won't be familiar with the GFF format. – terdon Jul 3 '15 at 10:47

Here's a Perl way:

$ perl -lane '$F[9]=~s/.*(gene=.+?;).*/$1/; print "$F[3]\t$F[9]\t$F[$#F]"' file
R0010W  gene=FLP1;  0

The -a enables automatic field splitting at whitespace, saving the fields in the array @F. This will work on both spaces and tabs. The -l adds a newline to each print() call and the -n reads the input file line by line, applying the script given by -e.

The script will delete everything in the 10th field (arrays start at 0 so the 10th field is $F[9]) except the gene= until the 1st ;. Then, the 4th, last and whatever is left int the 10th are printed.

  • 2
    @user121954 If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – terdon Jul 3 '15 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.