问题
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