-----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-----
--
[email protected] mailing list