AWK :Extract columns from file: Rows have variable columns
- by pulikot1
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?