这是我的第一个代码高尔夫问题,因此,如果不合适的话,我谨此致歉,并欢迎您提供任何反馈。
我有一个具有这种格式的文件:
a | rest of first line
b | rest of second line
b | rest of third line
c | rest of fourth line
d | rest of fifth line
d | rest of sixth line
实际内容和定界符也有所不同。内容只是文本。分隔符每行仅出现一次。对于这个难题,可以随意更改定界符,例如,使用“%”作为定界符。
所需的输出:
a | rest of first line
b | rest of second line % rest of third line
c | rest of fourth line
d | rest of fifth line % rest of sixth line
我已经有了ruby和awk脚本来合并它,但是我怀疑有可能有一个较短的oneliner。也就是说,可以与命令行中的管道和其他命令一起使用的单线。我想不通,我自己的脚本很长,只能在命令行上压缩。
首选最短字符。输入不一定要排序,但是我们只对合并具有匹配的第一字段的连续行感兴趣。有无限行与匹配的第一字段。字段1可以是任何东西,例如水果名称,专有名称等。
(我在MacOS上运行,因此我个人对在Mac上运行的实现最感兴趣)。
这是第二个示例/测试。注意“ |” 是分隔符。“ |”前的空格 是无关紧要的,如果不满意,则应视为关键。我在输出中使用“%”作为定界符,但是同样,请随时更改定界符(但不要使用方括号)。
输入:
why|[may express] surprise, reluctance, impatience, annoyance, indignation
whom|[used in] questions, subordination
whom|[possessive] whose
whom|[subjective] who
whoever|[objective] whomever
whoever|[possessive] whosever
who|[possessive] whose
who|[objective] whom
所需的输出:
why|[may express] surprise, reluctance, impatience, annoyance, indignation
whom|[used in] questions, subordination%[possessive] whose%[subjective] who
whoever|[objective] whomever%[possessive] whosever
who|[possessive] whose%[objective] whom
["A|some text", "B|other text", "A|yet some other text"]
这不是测试所需的输入,因为for的关键字A
在列表中不是一个接一个的。