arielle
arielle

Reputation: 945

Using awk to join lines following a match

I have a list that looks like this:

>aaa(+)
AAAAAAAAAA
>bbb(+)
BBBBBBBBBBBBBBBB
>ccc(-)
CCCCCCC

And I want to use awk to join the next line after either '(+)' or '(-)', with a comma delimiter, so that it looks like this:

>aaa(+),AAAAAAAAAAA
>bbb(+),BBBBBBBBBBBBBBBB
>ccc(-),CCCCCCC

I have already tried the following (in bash):

cat $file | awk '/(-)/||/(+)/{if (x)print x;x"";}{x=(!x)?$0:x","$0;}END{print x;}' > $new_file

but this appears to give a result like this:

>aaa(+),AAAAAAAAAAA
>aaa(+),AAAAAAAAAAA,>bbb(+),BBBBBBBBBBBBBBBB
>aaa(+),AAAAAAAAAAA,>bbb(+),BBBBBBBBBBBBBBBB,>ccc(-),CCCCCCC

which is obviously not what I am trying to do.

Any help would be very appreciated!

Thanks

Upvotes: 3

Views: 2404

Answers (5)

user2138595
user2138595

Reputation: 352

paste -d, - - < file

Paste will do the job if your file consists entirely of pairs of lines, as in your example.

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203229

awk '{printf "%s%s", $0, (NR%2 ? "," : ORS)}' file

Upvotes: 0

karakfa
karakfa

Reputation: 67467

another minimalist awk

$ awk 'ORS=/^>/?",":RS' file 

Upvotes: 6

sjsam
sjsam

Reputation: 21955

With gnu awk you may also do it like this :

$ awk -v RS=">"  '$0 != ""{ printf ">%s",gensub(/\)\n/,"),","g")}' file
>aaa(+),AAAAAAAAAA
>bbb(+),BBBBBBBBBBBBBBBB
>ccc(-),CCCCCCC

Upvotes: 1

Kent
Kent

Reputation: 195039

This awk one-liner should work for your example:

awk '/^>/{printf "%s,",$0;next}7' file

It joins the line beginning with > with the line below it. If the (+/-) is the key, you can change the pattern to your interested key.

Upvotes: 6

Related Questions