Thanks again
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.
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...2005/11/7, gentuxx < [EMAIL PROTECTED]>:-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Willie Wong wrote:
>On Mon, Nov 07, 2005 at 01:44:42AM -0200, Rafael Barreto wrote:
>
>>Hi,
>>
>>I'm learning about the use of the sed command and I have some
questions. I'm
>>trying to read in /etc/conf.d/clock the CLOCK variable with:
>>
>>sed '/^CLOCK="*"$/p' /etc/conf.d/clock
>>
>>This command, in principe, must print in screen the line that contains
>>CLOCK= in the begin, contains anything between double quotes and ends.
Well,
>>this doesn't return anything. If I enter the above command without $,
all is
>>ok. But, if I would like to return just that line contains CLOCK="anything"
>>and nothing more? For example,
>
>
>No it doesn't. What you want is the regexp ^CLOCK=".*"$ if you want
>anything (including nothing) between the double quotes, or
>^CLOCK=".+"$ if you want something (excluding nothing) between the
>double quotes.
>
>The reason that removing the trailing $ worked is that it matched the
>CLOCK=" part, the * character specifies 0 or more iterates of the
>previous character, which is "
>
>HTH
>
>W
Also, as you pointed out, lines with trailing comments would not be
returned based on the _expression_ (even as modified):
sed '/^CLOCK=".*"$/p /etc/conf.d/clock
This is because the _expression_, as is, does not allow for anything
after the last double quote ("). The following _expression_ should
match the line you want, and print out ONLY the 'CLOCK="foo"':
sed -n '/^CLOCK=/s/^\(CLOCK=".*"\).*$/\1/p /etc/conf.d/clock
How this works is as follows (since you're trying to learn sed):
1) the '-n' suppresses all output except that which was changed by
your _expression_/commands.
2) the first _expression_ ( /^CLOCK=/ ) gives sed the "address" at which
to make the changes.
3) the second _expression_ ( s/^\(CLOCK=".*"\).*$/\1/p )tells sed what
to do when it reaches that address. This is better broken down into
smaller steps:
a) the first half of the substitution _expression_ (
s/^\(CLOCK=".*"\).*$/ ) tells sed to match the capital letters C
- -L-O-C-K which start a line ( ^ ),
b) followed by an equals sign (=), a double-quote ("),
c) followed by 0 or more of any character type - except newlines
- - ( .* ),
d) followed by another double-quote (").
e) Then, because of the parentheses metacharacters ( \( \) ),
store the match in the holding space (memory).
f) Then match 0 or more of any character type ( .* ), ending the
line ( $ ).
g) the second half ( /\1/ ) substitutes the characters "captured"
in the parentheses metacharacters, for the whole line
h) and prints ( /p ) the result
So, while Willie's suggestion is correct, this should give you a more
complete solution.
HTH
- --
gentux
echo "hfouvyAdpy/ofu" | perl -pe 's/(.)/chr(ord($1)-1)/ge'
gentux's gpg fingerprint ==> 34CE 2E97 40C7 EF6E EC40 9795 2D81 924A
6996 0993
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFDbuAELYGSSmmWCZMRAoxdAKDZTA89tDCO+I67qhZwba6oJ28TrgCdHIkT
Lctx2b5xRczC3bXl+emMrOs=
=780W
-----END PGP SIGNATURE-----
--
gentoo-user@gentoo.org mailing list