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

Replies

Subject Author
Re: [gentoo-user] About sed Rafael Barreto <rafaelmbarreto@×××××.com>
Re: [gentoo-user] About sed Walter Dnes <waltdnes@××××××××.org>