Remove new line / line break characters only for specific lines

霸气de小男生 提交于 2019-12-10 10:26:41

问题


I have the following output:

02:01:31
OFFLINE
02:02:31
ONLINE

and I would like it to become:

02:01:31 OFFLINE
02:02:31 ONLINE

I found a way of removing all newline / line breaks with sed ':a;N;$!ba;s/\n/ /g' however it gives me what follows which is not really what I'm after:

02:01:31 OFFLINE 02:02:31 ONLINE

Q: How can I remove newline / line breaks only for lines that match a certain pattern (in this case [0-9] would be enough?

PS: It doesn't have to be with sed as long as I can apply a regex pattern to the matching of lines


回答1:


Make your s/\n/ /g be s/\([0-9]\S*\)\n/\1 /g (a digit followed by any amount of non-whitespace followed by a newline).




回答2:


awk oneliner:

awk '/[0-9]$/{printf $0" ";next;}1'

test:

kent$  echo "02:01:31
OFFLINE
02:02:31
ONLINE"|awk '/[0-9]$/{printf $0" ";next;}1'
02:01:31 OFFLINE
02:02:31 ONLINE


来源:https://stackoverflow.com/questions/9307802/remove-new-line-line-break-characters-only-for-specific-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!