Reputation: 945
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
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
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
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