我有一个这样的字符串:
2015-09-24 12:05:00 2015-09-24 12:10:00 2015-09-24 12:15:00 2015-09-24 12:20:00
我想插入一个逗号每个20个字符(最后一个字符除外),以便:
2015-09-24 12:05:00, 2015-09-24 12:10:00, 2015-09-24 12:15:00, 2015-09-24 12:20:00
我该怎么办?
答案 0 :(得分:1)
让我们定义你的字符串:
# layout.html.erb
<div class="heading"><%= yield :title %></div>
# template.html.erb
<%= provide :title, "Foo" %>
<%= provide :title, "bar" %>
# Generated HTML
<div class="heading">Foo</div>
现在,让我们每20个字符插入一个逗号:
$ dates='2015-09-24 12:05:00 2015-09-24 12:10:00 2015-09-24 12:15:00 2015-09-24 12:20:00'
逗号并不是您想要的地方,因为第一个日期不是以空格开头。所以,我们可以解决这个问题(GNU sed):
$ echo "$dates" | sed -r 's/.{20}/&,/g'
2015-09-24 12:05:00 ,2015-09-24 12:10:00 ,2015-09-24 12:15:00 ,2015-09-24 12:20:00
以GNU sed格式:
$ echo "$dates" | sed -r 's/^/ /; s/.{20}/&,/g; s/^ //'
2015-09-24 12:05:00, 2015-09-24 12:10:00, 2015-09-24 12:15:00, 2015-09-24 12:20:00,