Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining Line Breaks in FASTA file With Condition in SED/AWK/Perl one-liner

I have a data that looks like this

> sq1
foofoofoobar
foofoofoo
> sq2
quxquxquxbar
quxquxquxbar
quxx
> sq3
paxpaxpax
pax

What I want to do is to join them into one lines:

> sq1 foofoofoobarfoofoofoo
> sq2 quxquxquxbarquxquxquxbarquxx
> sq3 paxpaxpaxpax

I tried this code but fail.

sed -e 'te' -e 'H;$!d;:e' -e 'x;/^$/d;s/\n//g'

What's the right way to do it?

like image 679
neversaint Avatar asked Mar 02 '10 01:03

neversaint


2 Answers

$ awk '/^>/&&NR>1{print "";}{ printf "%s",/^>/ ? $0" ":$0 }' file 
> sq1 foofoofoobarfoofoofoo
> sq2 quxquxquxbarquxquxquxbarquxx
> sq3 paxpaxpaxpax
like image 161
ghostdog74 Avatar answered Sep 30 '22 17:09

ghostdog74


This is one way to do what you want using sed:

sed -n '1{x;d;x};${H;x;s/\n/ /1;s/\n//g;p;b};/^>/{x;s/\n/ /1;s/\n//g;p;b};H'
like image 34
Dennis Williamson Avatar answered Sep 30 '22 18:09

Dennis Williamson