You are not logged in.
Pages: 1
G:
sentence_to_join
OK I can get this to work by
sed '/G:\s*$/ {
N
s#G:\s*\n#G: #
}'
This works but I don't want.
G: 12.34.56 more stuff
So I think I can either do this with a not next line begins numerical in the sed i.e !^[0-9] ... But how?(I'm not sure how to phrase this in combination with the one that initially works)
Or my other thought was to search after for G:\s[0-9] but how do I then turn that into a newline in between the pattern, to end up with, but again how?(I can't see how to add a newline in between a pattern match)
G:
12.34.56 more stuff
Last edited by FeatherMonkey (2010-03-15 19:15:09)
Offline
You can use brackets to separate the pattern and then \n to refer to the part in the brackets. So for the pattern
(abc)(def)(ghi)
Then
\0 = abcdefghi
\1 = abc
\2 = def
\3 = ghi
For your example you would use
sed '/G:\s*$/ {
N
s#\(G:\)\s*\n\([^0-9].*\)#\1 \2#
}'
Note that the brackets have to be escaped with a backslash in sed. IMO that looks nasty and is a pain to write, if you like you could enable extended regular expressions using "sed -r" which gives the brackets their special meaning without having to type in those extra backslashes.
If you google "sed tutorial" you will find several great documents to help you discover all of the exciting features of sed
Offline
Thank you that works fine. Seems I've got a lot more reading to do, not sure I really understand why it works as I was expecting a not in the expression.
Offline
The not appears as the caret (^) in [^0-9] which means "any character which is not in the range 0-9". So if the first character after the new line is a number, the substitution search pattern will not match and the lines will be left as they are without joining.
Offline
Pages: 1