Gentoo Archives: gentoo-user

From: Willie Wong <wwong@×××××××××.EDU>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] About sed
Date: Mon, 07 Nov 2005 07:23:58
Message-Id: 20051107071916.GB30529@princeton.edu
In Reply to: Re: [gentoo-user] About sed by Rafael Barreto
1 On Mon, Nov 07, 2005 at 03:42:05AM -0200, Rafael Barreto wrote:
2 > Other thing... Why was necessary to ^CLOCK= before
3 > s/^\(CLOCK=".*"\).*$/\1/p? And which the necessity of the ( ) between the
4 > regular expression?
5
6 as I just posted in another post, the /^CLOCK/ should not be strictly
7 necessary.
8
9 See below for an explanation of the ()
10 >
11 >
12 > 2005/11/7, Rafael Barreto <rafaelmbarreto@×××××.com>:
13 > >
14 > > For that I understood, this command will return the line of CLOCK= in
15 > > /etc/conf.f/clock without any comments. Is this right? Well, what I really
16 > > want is replace just CLOCK="fool1" by CLOCK="fool2" keeping the comments in
17 > > line.
18 > >
19
20 Do you know what fool2 would be? If so:
21
22 s/^\(CLOCK="\)[^["]]*\(".*\)$/\1fool2\2/p
23
24 should do what you want. (I think sed greedy matches by default? Hum,
25 I honestly don't remember, better safe than sorry.)
26
27 See below for the use of the \( .. \) and \1 \2 variables.
28
29 The [^["]] specifies a class of characters to match, in this case, it
30 means anything except for the quotation mark. (The caret ^ negates the
31 class ["] which means only the quotation mark.) So what this sed
32 expression is really saying is that we want to keep the beginning of
33 the line up to the first quotation mark the same, and keep the end of
34 the line starting from the second quotation mark the same, and change
35 everything in between to 'fool2'.
36
37 > > By the way, \1 do really what? If i put \0 the result is the entire line.
38 > > So, could you explain me this a little more? Thanks...
39 > >
40
41 \0 is the entire matching expression/line.
42 \n is tokenizing. By using parentheses in the regexp, you can reuse
43 the texts in the parentheses in the substitution.
44
45 For example, consider the expression
46
47 s/\(.at\).*\(.ox\)/\2 not \1/p
48
49 If we send it
50
51 The bat scared the fox.
52
53 it would print
54
55 fox not bat
56
57 because \2 gets replaced by the second matching token, which in this
58 case, is fox, because it matches ".ox". Similarly for bat and the
59 first token.
60
61 If we send the same expression
62
63 The cat died in the box.
64
65 it would print
66
67 box not cat
68
69 I can't explain the concept very well, and I hope the examples are
70 good enough.
71
72 Seriously, one of the best way to learn sed is to read the manual from
73
74 `info sed'
75
76 W
77 --
78 Here lies Lester Moore
79 Four slugs from a .44
80 No Les No more.
81 Sortir en Pantoufles: up 4 days, 8:14
82 --
83 gentoo-user@g.o mailing list