Gentoo Archives: gentoo-user

From: Rafael Barreto <rafaelmbarreto@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] About sed
Date: Mon, 07 Nov 2005 05:48:45
Message-Id: 5de801760511062142k14fad186q@mail.gmail.com
In Reply to: Re: [gentoo-user] About sed by Rafael Barreto
1 Other thing... Why was necessary to ^CLOCK= before
2 s/^\(CLOCK=".*"\).*$/\1/p? And which the necessity of the ( ) between the
3 regular expression?
4
5 Thanks again
6
7
8 2005/11/7, Rafael Barreto <rafaelmbarreto@×××××.com>:
9 >
10 > For that I understood, this command will return the line of CLOCK= in
11 > /etc/conf.f/clock without any comments. Is this right? Well, what I really
12 > want is replace just CLOCK="fool1" by CLOCK="fool2" keeping the comments in
13 > line.
14 >
15 > By the way, \1 do really what? If i put \0 the result is the entire line.
16 > So, could you explain me this a little more? Thanks...
17 >
18 > 2005/11/7, gentuxx < gentuxx@×××××.com>:
19 > >
20 > > -----BEGIN PGP SIGNED MESSAGE-----
21 > > Hash: SHA1
22 > >
23 > > Willie Wong wrote:
24 > >
25 > > >On Mon, Nov 07, 2005 at 01:44:42AM -0200, Rafael Barreto wrote:
26 > > >
27 > > >>Hi,
28 > > >>
29 > > >>I'm learning about the use of the sed command and I have some
30 > > questions. I'm
31 > > >>trying to read in /etc/conf.d/clock the CLOCK variable with:
32 > > >>
33 > > >>sed '/^CLOCK="*"$/p' /etc/conf.d/clock
34 > > >>
35 > > >>This command, in principe, must print in screen the line that contains
36 > > >>CLOCK= in the begin, contains anything between double quotes and ends.
37 > >
38 > > Well,
39 > > >>this doesn't return anything. If I enter the above command without $,
40 > > all is
41 > > >>ok. But, if I would like to return just that line contains
42 > > CLOCK="anything"
43 > > >>and nothing more? For example,
44 > > >
45 > > >
46 > > >No it doesn't. What you want is the regexp ^CLOCK=".*"$ if you want
47 > > >anything (including nothing) between the double quotes, or
48 > > >^CLOCK=".+"$ if you want something (excluding nothing) between the
49 > > >double quotes.
50 > > >
51 > > >The reason that removing the trailing $ worked is that it matched the
52 > > >CLOCK=" part, the * character specifies 0 or more iterates of the
53 > > >previous character, which is "
54 > > >
55 > > >HTH
56 > > >
57 > > >W
58 > >
59 > > Also, as you pointed out, lines with trailing comments would not be
60 > > returned based on the expression (even as modified):
61 > >
62 > > sed '/^CLOCK=".*"$/p /etc/conf.d/clock
63 > >
64 > > This is because the expression, as is, does not allow for anything
65 > > after the last double quote ("). The following expression should
66 > > match the line you want, and print out ONLY the 'CLOCK="foo"':
67 > >
68 > > sed -n '/^CLOCK=/s/^\(CLOCK=".*"\).*$/\1/p /etc/conf.d/clock
69 > >
70 > > How this works is as follows (since you're trying to learn sed):
71 > >
72 > > 1) the '-n' suppresses all output except that which was changed by
73 > > your expression/commands.
74 > > 2) the first expression ( /^CLOCK=/ ) gives sed the "address" at which
75 > > to make the changes.
76 > > 3) the second expression ( s/^\(CLOCK=".*"\).*$/\1/p )tells sed what
77 > > to do when it reaches that address. This is better broken down into
78 > > smaller steps:
79 > > a) the first half of the substitution expression (
80 > > s/^\(CLOCK=".*"\).*$/ ) tells sed to match the capital letters C
81 > > - -L-O-C-K which start a line ( ^ ),
82 > > b) followed by an equals sign (=), a double-quote ("),
83 > > c) followed by 0 or more of any character type - except newlines
84 > > - - ( .* ),
85 > > d) followed by another double-quote (").
86 > > e) Then, because of the parentheses metacharacters ( \( \) ),
87 > > store the match in the holding space (memory).
88 > > f) Then match 0 or more of any character type ( .* ), ending the
89 > > line ( $ ).
90 > > g) the second half ( /\1/ ) substitutes the characters "captured"
91 > > in the parentheses metacharacters, for the whole line
92 > > h) and prints ( /p ) the result
93 > >
94 > > So, while Willie's suggestion is correct, this should give you a more
95 > > complete solution.
96 > >
97 > > HTH
98 > >
99 > > - --
100 > > gentux
101 > > echo "hfouvyAdpy/ofu" | perl -pe 's/(.)/chr(ord($1)-1)/ge'
102 > >
103 > > gentux's gpg fingerprint ==> 34CE 2E97 40C7 EF6E EC40 9795 2D81 924A
104 > > 6996 0993
105 > > -----BEGIN PGP SIGNATURE-----
106 > > Version: GnuPG v1.4.1 (GNU/Linux)
107 > >
108 > > iD8DBQFDbuAELYGSSmmWCZMRAoxdAKDZTA89tDCO+I67qhZwba6oJ28TrgCdHIkT
109 > > Lctx2b5xRczC3bXl+emMrOs=
110 > > =780W
111 > > -----END PGP SIGNATURE-----
112 > >
113 > > --
114 > > gentoo-user@g.o mailing list
115 > >
116 > >
117 >

Replies

Subject Author
Re: [gentoo-user] About sed gentuxx <gentuxx@×××××.com>
Re: [gentoo-user] About sed Willie Wong <wwong@×××××××××.EDU>