Ashley
Ashley

Reputation: 21

Append lines to a previous line

I am trying to append all lines that begin with > to the previous line that did not begin with >

cat tmp

ATAAACGGAAAAACACTACTTTAGCTTACGGGATCCGGT
>Aa_816
>Aa_817
>Aa_818
CCAAACGGAAAAACACTACTTGAGCTTACGGGATCCGGT
>Aa_940
>Aa_941
CTAAAAGGAAAAACACTACTTTAGCTTTTGGGATCCGGT

What I want is this:

ATAAACGGAAAAACACTACTTTAGCTTACGGGATCCGGT >Aa_816 >Aa_817 >Aa_818
CCAAACGGAAAAACACTACTTGAGCTTACGGGATCCGGT >Aa_940 >Aa_941
CTAAAAGGAAAAACACTACTTTAGCTTTTGGGATCCGGT

This almost gets me there:

cat tmp |awk '!/>/ {sub(/\\$/,""); getline t; print $0 t; next}; 1'

Upvotes: 2

Views: 1032

Answers (4)

Steve
Steve

Reputation: 54392

I think all you need is a little sed:

sed ':a; N; $!ba; s/\n>/ >/g' file

Results:

ATAAACGGAAAAACACTACTTTAGCTTACGGGATCCGGT >Aa_816 >Aa_817 >Aa_818
CCAAACGGAAAAACACTACTTGAGCTTACGGGATCCGGT >Aa_940 >Aa_941
CTAAAAGGAAAAACACTACTTTAGCTTTTGGGATCCGGT

Upvotes: 2

BMW
BMW

Reputation: 45223

Using awk

awk '!/>/{printf (NR==1)?$0:RS $0;next}{printf FS $0}' file

If you don't care the output has new line generated on the first line, here is the shorter one.

awk '{printf (/>/?FS $0:RS $0)}' file

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77075

With awk:

awk '!/^>/{printf "%s%s", (NR==1)?"":RS,$0;next}{printf "%s", FS $0}END{print ""}' file

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

awk '/^[^>]/ { if (length(old) > 0) print old; old = $0 }
     /^>/    { old = old " " $0 }
     END     { if (length(old) > 0) print old }'

Upvotes: 0

Related Questions