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