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开发者_JAVA技巧;s/\n//g'
What's the right way to do it?
$ awk '/^>/&&NR>1{print "";}{ printf "%s",/^>/ ? $0" ":$0 }' file
> sq1 foofoofoobarfoofoofoo
> sq2 quxquxquxbarquxquxquxbarquxx
> sq3 paxpaxpaxpax
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'
perl -ne '!/^>/ ? chomp($p) : (chomp $_, $_.=" "); print $p; $p = $_; END{print $p}
... which, of course, could be written a lot shorter if desired.
This is might work for you:
sed ':a;$!{N;ba};s/\n//g;s/> sq[0-9]*/\n& /g;s/.//' file
or this:
sed ':a;$!N;s/\n\([^>]\)/\a\1/;ta;s/\a/ /;s///g;P;D' file
or this:
awk -vRS='> sq' '{sub(/^/,RS);sub(/\n/," ");gsub(/\n/,"")};NR>1' file
Shortest sed
solution what I was able to find:
sed -n '/^>/!{H;$!b};s/$/ /;x;1b;s/\n//g;p'
精彩评论