AWK :Extract columns from file: Rows have variable columns

Posted by pulikot1 on Stack Overflow See other posts from Stack Overflow or by pulikot1
Published on 2012-10-29T13:37:07Z Indexed on 2012/12/04 5:04 UTC
Read the original article Hit count: 429

Filed under:
|
|

I have a text file in the following format. Each row has variable number of columns.

File:

gi|269201691|ref|YP_003280960.1| chromosomal replication initiation protein                                                            gi|57651109|ref|YP_184912.1| chromosomal replication initiation protein                                                                   %           1        0.0           2296      100.0
gi|269201692|ref|YP_003280961.1| DNA polymerase III subunit beta                                                                       gi|57651110|ref|YP_184913.1| DNA polymerase III subunit beta                                                                              %           1        0.0           1964      100.0

The resulting file should look like the following:

gi|269201691|ref|YP_003280960.1| gi|57651109|ref|YP_184912.1| % 1        0.0           2296      100.0
gi|269201694|ref|YP_003280963.1| gi|57651112|ref|YP_184915.1| % 1        0.0           1767      100.0

The code below helps find columns in each row with the pattern 'ref'.

awk '{for (i=1;i<=NF;i++) if ($i ~ /ref/) print $i }'

Any ideas on how to do the same?

© Stack Overflow or respective owner

Related posts about awk

Related posts about gawk