合并sed中的多行,awk

时间:2014-10-15 16:36:00

标签: awk sed grep

我有以下数据集。带标题的部分和带副标题的部分。每个部分都有多行,两个部分的行数相同。我可以在每行的末尾或开头打印一个符号,每个部分都可以不同 - 在示例中为^,%。

title^
another title^
other title^
one more title^
(... continues)
subtitle%
other subtitle%
some subtitle%
one more subtitle%
(... continues)

我想将其输出为

title subtitle 
another title other subtitle 
other title some subtitle
one more title one more subtitle

基本上我想加入第一个标题,第一个字幕在一行,然后是第二个标题和第二个副标题。

这可能是sed,awk吗?

3 个答案:

答案 0 :(得分:0)

如果您拥有正确数量的相应行,则不需要任何特殊符号:使用pr工具将数据写入2列:

pr --columns=2 --separator=" " -T <<END
title
another title
other title
one more title
(... continues)
subtitle
other subtitle
some subtitle
one more subtitle
(... continues)
END
title subtitle
another title other subtitle
other title some subtitle
one more title one more subtitle
(... continues) (... continues)

答案 1 :(得分:0)

更清洁的awk脚本就像

awk '/\^/{sub("\\^"," ",$0);a[i++]=$0} /%/{sub("%"," ",$0); print a[NR-i-1],$0}'

将产生输出

title  subtitle 
another title  other subtitle 
other title  some subtitle 
one more title  one more subtitle 

它的作用是什么?

/\^/匹配模式^,即文件的第一部分。

sub("\\^"," ",$0)用空格替换^

a[i++]=$0将记录保存在数组

/%/与文件的%第二部分匹配。

print a[NR-i-1],$0此处NR是记录数。 NR不断增加NR-i-1将指向a数组中的相应行

答案 2 :(得分:0)

您可以使用pastegrepsed的组合:

$ paste <(grep "\^$" file | sed -e 's/\^$//')   <(grep "\%$" file | sed -e 's/\%$//')
title   subtitle
another title   other subtitle
other title some subtitle
one more title  one more subtitle

说明:

  1. paste:合并两个文件

  2. <():Bash进程替换,基本上创建一个文件描述符,其中包含()内的命令输出

  3. grep "\^$" file | sed -e 's/\^$//':选择以^结尾的行,并使用sed将其删除

  4. grep "\%$" file | sed -e 's/\%$//':选择以%结尾的行并使用sed删除