On Mon, Nov 07, 2005 at 03:42:05AM -0200, Rafael Barreto wrote: > Other thing... Why was necessary to ^CLOCK= before > s/^\(CLOCK=".*"\).*$/\1/p? And which the necessity of the ( ) between the > regular expression?
as I just posted in another post, the /^CLOCK/ should not be strictly necessary. See below for an explanation of the () > > > 2005/11/7, Rafael Barreto <[EMAIL PROTECTED]>: > > > > For that I understood, this command will return the line of CLOCK= in > > /etc/conf.f/clock without any comments. Is this right? Well, what I really > > want is replace just CLOCK="fool1" by CLOCK="fool2" keeping the comments in > > line. > > Do you know what fool2 would be? If so: s/^\(CLOCK="\)[^["]]*\(".*\)$/\1fool2\2/p should do what you want. (I think sed greedy matches by default? Hum, I honestly don't remember, better safe than sorry.) See below for the use of the \( .. \) and \1 \2 variables. The [^["]] specifies a class of characters to match, in this case, it means anything except for the quotation mark. (The caret ^ negates the class ["] which means only the quotation mark.) So what this sed expression is really saying is that we want to keep the beginning of the line up to the first quotation mark the same, and keep the end of the line starting from the second quotation mark the same, and change everything in between to 'fool2'. > > By the way, \1 do really what? If i put \0 the result is the entire line. > > So, could you explain me this a little more? Thanks... > > \0 is the entire matching expression/line. \n is tokenizing. By using parentheses in the regexp, you can reuse the texts in the parentheses in the substitution. For example, consider the expression s/\(.at\).*\(.ox\)/\2 not \1/p If we send it The bat scared the fox. it would print fox not bat because \2 gets replaced by the second matching token, which in this case, is fox, because it matches ".ox". Similarly for bat and the first token. If we send the same expression The cat died in the box. it would print box not cat I can't explain the concept very well, and I hope the examples are good enough. Seriously, one of the best way to learn sed is to read the manual from `info sed' W -- Here lies Lester Moore Four slugs from a .44 No Les No more. Sortir en Pantoufles: up 4 days, 8:14 -- gentoo-user@gentoo.org mailing list