You are not logged in.

#1 2010-03-13 12:07:56

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

[solved]Regexp problem sed

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

#2 2010-03-13 16:27:23

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: [solved]Regexp problem sed

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 smile

Offline

#3 2010-03-15 19:14:54

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

Re: [solved]Regexp problem sed

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

#4 2010-03-15 21:31:18

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: [solved]Regexp problem sed

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

Board footer

Powered by FluxBB