简体   繁体   English

在比赛后使用awk连接线

[英]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: 而且我想使用awk将“(+)”或“(-)”之后的下一行与逗号分隔符连接起来,使其看起来像这样:

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

I have already tried the following (in bash): 我已经尝试了以下内容(以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 谢谢

This awk one-liner should work for your example: 这个awk单行代码应适用于您的示例:

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. 如果(+/-)是键,则可以将模式更改为您感兴趣的键。

另一个极简主义的awk

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

With gnu awk you may also do it like this : 使用gnu awk您也可以这样做:

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

如果您的文件完全由成对的行组成,则粘贴将起作用,如您的示例所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM