views:

245

answers:

3

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?

+2  A: 
$ awk '/^>/&&NR>1{print "";}{ printf "%s",/^>/ ? $0" ":$0 }' file 
> sq1 foofoofoobarfoofoofoo
> sq2 quxquxquxbarquxquxquxbarquxx
> sq3 paxpaxpaxpax
ghostdog74
there is remaining whitespace in the sequence, e.g. "paxpaxpax pax" instead of "paxpaxpaxpax". How can I remove that?
neversaint
i see, so you want to leave a space after >, see edit
ghostdog74
+2  A: 

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'
Dennis Williamson
+1. nice, but too ugly and complicated for my taste.
ghostdog74
+1  A: 
perl -ne '!/^>/ ? chomp($p) : (chomp $_, $_.=" "); print $p; $p = $_; END{print $p}

... which, of course, could be written a lot shorter if desired.

tsee