0

I would like to convert every two rows to two columns using awk.

input.txt:

# Query: gi|11465907|ref|NC_001872.1| Chlamydomonas eugametos genome, complete genome
# 0 hits found
# Query: gi|11465922|ref|NC_000892.1| Pedinomonas minor genome, complete genome
# 1 hits found

output.txt:

Chlamydomonas eugametos genome 0
Pedinomonas minor genome 1
2

Assuming that your input file is consistent:

awk -F'[|,]' '/genome/ {printf "%s ", $5; next} {print substr($1,3,1)}' input.txt > output.txt
Chlamydomonas eugametos genome 0
Pedinomonas minor genome 1
2

Try:

awk -F'[|] |,' '{getline p; split(p,H," "); print $2,H[2]}' file

Which would rely on no additional commas in the | separated fields.

A bit more robust in that sense would be:

awk -F'[|] *' '{getline p; split($5,Q,","); split(p,H," "); print Q[1],H[2]}' file

A sed alternative:

sed 'N; s/.*| *//; s/,.*\n#//; s/ hits found//' file

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.