* [gentoo-user] About sed @ 2005-11-07 3:44 Rafael Barreto 2005-11-07 4:07 ` Willie Wong 2005-11-07 7:49 ` Alexander Skwar 0 siblings, 2 replies; 14+ messages in thread From: Rafael Barreto @ 2005-11-07 3:44 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 909 bytes --] 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, CLOCK="anything" # set clock of the system and CLOCK="anything" are different. Other thing... if i put: sed '/^CLOCK=*/p' /etc/conf.d/clock the return will be anything that contains CLOCK. Why? I suppose that I didn't undestand the use of regular expression metacharacters. So, please, anyone to explain me that? Thanks a lot and sorry by my english... [-- Attachment #2: Type: text/html, Size: 1049 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 3:44 [gentoo-user] About sed Rafael Barreto @ 2005-11-07 4:07 ` Willie Wong 2005-11-07 4:19 ` Rafael Barreto 2005-11-07 5:03 ` gentuxx 2005-11-07 7:49 ` Alexander Skwar 1 sibling, 2 replies; 14+ messages in thread From: Willie Wong @ 2005-11-07 4:07 UTC (permalink / raw To: gentoo-user 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 -- Q: Why won't Heisenberg's operators live in the suburbs? A: They don't commute. Sortir en Pantoufles: up 4 days, 5:24 -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 4:07 ` Willie Wong @ 2005-11-07 4:19 ` Rafael Barreto 2005-11-07 5:03 ` gentuxx 1 sibling, 0 replies; 14+ messages in thread From: Rafael Barreto @ 2005-11-07 4:19 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 1457 bytes --] :) Thanks very much. Now that I read your answear I searched in a book the significant of *,+ and others and I suppose I understood. Thanks 2005/11/7, Willie Wong <wwong@princeton.edu>: > > 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 > -- > Q: Why won't Heisenberg's operators live in the suburbs? > A: They don't commute. > Sortir en Pantoufles: up 4 days, 5:24 > -- > gentoo-user@gentoo.org mailing list > > [-- Attachment #2: Type: text/html, Size: 1855 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 4:07 ` Willie Wong 2005-11-07 4:19 ` Rafael Barreto @ 2005-11-07 5:03 ` gentuxx 2005-11-07 5:38 ` Rafael Barreto 2005-11-07 6:55 ` Willie Wong 1 sibling, 2 replies; 14+ messages in thread From: gentuxx @ 2005-11-07 5:03 UTC (permalink / raw To: gentoo-user -----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 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 5:03 ` gentuxx @ 2005-11-07 5:38 ` Rafael Barreto 2005-11-07 5:42 ` Rafael Barreto 2005-11-09 4:37 ` Walter Dnes 2005-11-07 6:55 ` Willie Wong 1 sibling, 2 replies; 14+ messages in thread From: Rafael Barreto @ 2005-11-07 5:38 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 3723 bytes --] 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 <gentuxx@gmail.com>: > > -----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 > > [-- Attachment #2: Type: text/html, Size: 4588 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 5:38 ` Rafael Barreto @ 2005-11-07 5:42 ` Rafael Barreto 2005-11-07 6:37 ` gentuxx 2005-11-07 7:19 ` [gentoo-user] " Willie Wong 2005-11-09 4:37 ` Walter Dnes 1 sibling, 2 replies; 14+ messages in thread From: Rafael Barreto @ 2005-11-07 5:42 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 4168 bytes --] Other thing... Why was necessary to ^CLOCK= before s/^\(CLOCK=".*"\).*$/\1/p? And which the necessity of the ( ) between the regular expression? Thanks again 2005/11/7, Rafael Barreto <rafaelmbarreto@gmail.com>: > > 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 < gentuxx@gmail.com>: > > > > -----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 > > > > > [-- Attachment #2: Type: text/html, Size: 5295 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 5:42 ` Rafael Barreto @ 2005-11-07 6:37 ` gentuxx 2005-11-07 7:25 ` [gentoo-user] [OT] " Willie Wong 2005-11-07 7:19 ` [gentoo-user] " Willie Wong 1 sibling, 1 reply; 14+ messages in thread From: gentuxx @ 2005-11-07 6:37 UTC (permalink / raw To: gentoo-user -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Rafael Barreto wrote: > Other thing... Why was necessary to ^CLOCK= before s/^\(CLOCK=".*"\).*$/\1/p? And which the necessity of the ( ) between the regular expression? > > Thanks again > Sorry for things being out of order, but you top-posted. The answer to your first question is below. The /^CLOCK=/ is NOT necessary, in this instance, and may actually cause problems if you're not 100% sure of your regular expression. It is there, simply because I was expanding your original pattern. What this does, though, is it tells sed to match the line with the regular expression before making any substitutions and/or replacements. Say for example, you had 2 lines in the same file with different values but similar structure, and you only wanted to change one of them. Let's go with the example you used. Let's assume that our example file looks something like this: # This is a comment CLOCK="foo1" # This is another comment TIMEZONE="GMT" # This is yet another comment CLOCK="${CLOCK}foo2" So, let's say that you wanted to change the second instance. The /^CLOCK=/ tells sed to ignore all lines that do NOT begin with 'CLOCK='. So the comments ( # This is a comment ) and the 'TIMEZONE' directive will be ignored. Actions will only be taken on the lines: CLOCK="foo1" CLOCK="${CLOCK}foo2" ...because they match the expression we gave sed as an "address" ( /^CLOCK=/ ). We would furthur specify how to change that line by our search and replace command (as described below): s/^\(CLOCK=\"\)\${.*}\(\".*\)$/\1foo3\2/ It is important to note here that the left and right brace characters ( { } ) are not escaped. If they were, they would have been handled as metacharacters, which produces a drastically different result. If you really want to get to know sed (and awk), I HIGHLY recommend getting the 2 O'reilley books: Sed & Awk, and Mastering Regular Expressions. Both of these two books have taught me almost everything I know on the matter, and I refer to them frequently. HTH > 2005/11/7, Rafael Barreto <rafaelmbarreto@gmail.com <mailto:rafaelmbarreto@gmail.com>>: > > 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 < gentuxx@gmail.com <mailto:gentuxx@gmail.com>>: > I will try to answer both of your questions with this one email. The '\1' takes the first element, or element group, captured by the parentheses metacharacters ( \( \) ) and uses it as a variable. The variable '\0', as you found out, represents the entire contents of the "pattern space" or basically, the entire line since you're matching beginning ( ^ ) to end ( $ ). The variable '\0' is also synonymous with the ampersand metacharacter ( & ). So, if you want to change the value of the "CLOCK" variable in /etc/conf.d/clock, while leaving any comments intact, you could do something like this: sed 's/^\(CLOCK=\"\).*\(\".*\)$/\1foo2\2/' /etc/conf.d/clock This matches and captures the contents of the first set of parentheses, and places it into a variable accessible by '\1'. Then it matches 0 or more of any character type, until it matches a literal double-quote ( \" ). Then it matches and captures the contents of the second set of parentheses, and places it into a variable accessible by '\2'. So, to change the value, you would issue a substitution command ( s/// ) which matches the first set and replaces it with the second set. The variables '\1' and '\2' are interpolated, so the values captured are printed. I.e. the result should be: was: CLOCK="foo1" # some comments here now: CLOCK="foo2" # some comments here > 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 - -- gentoo-user@gentoo.org <mailto:gentoo-user@gentoo.org> mailing list - -- 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) iD8DBQFDbvZBLYGSSmmWCZMRAu2TAJ9QluvFRh2Hvu2T1p6DxOkhsD+gUgCgvYjC qQ12BqJsAQHxMEfBkLTodAw= =LvhF -----END PGP SIGNATURE----- -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] [OT] About sed 2005-11-07 6:37 ` gentuxx @ 2005-11-07 7:25 ` Willie Wong 0 siblings, 0 replies; 14+ messages in thread From: Willie Wong @ 2005-11-07 7:25 UTC (permalink / raw To: gentoo-user On Sun, Nov 06, 2005 at 10:37:53PM -0800, gentuxx wrote: > If you really want to get to know sed (and awk), I HIGHLY recommend > getting the 2 O'reilley books: Sed & Awk, and Mastering Regular > Expressions. Both of these two books have taught me almost everything > I know on the matter, and I refer to them frequently. > Ah yes. I definitely agree. My copy disappeared when I moved out of the dorm last. That fact has caused me much pain and inconvenience in the past few months. Useful as 'info sed' maybe, the book makes looking for help that much easier. W -- Ever stop to think, and forget to start again? Sortir en Pantoufles: up 4 days, 8:41 -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 5:42 ` Rafael Barreto 2005-11-07 6:37 ` gentuxx @ 2005-11-07 7:19 ` Willie Wong 1 sibling, 0 replies; 14+ messages in thread From: Willie Wong @ 2005-11-07 7:19 UTC (permalink / raw To: gentoo-user On Mon, Nov 07, 2005 at 03:42:05AM -0200, Rafael Barreto wrote: > Other thing... Why was necessary to ^CLOCK= before > s/^\(CLOCK=".*"\).*$/\1/p? And which the necessity of the ( ) between the > regular expression? as I just posted in another post, the /^CLOCK/ should not be strictly necessary. See below for an explanation of the () > > > 2005/11/7, Rafael Barreto <rafaelmbarreto@gmail.com>: > > > > 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. > > Do you know what fool2 would be? If so: s/^\(CLOCK="\)[^["]]*\(".*\)$/\1fool2\2/p should do what you want. (I think sed greedy matches by default? Hum, I honestly don't remember, better safe than sorry.) See below for the use of the \( .. \) and \1 \2 variables. The [^["]] specifies a class of characters to match, in this case, it means anything except for the quotation mark. (The caret ^ negates the class ["] which means only the quotation mark.) So what this sed expression is really saying is that we want to keep the beginning of the line up to the first quotation mark the same, and keep the end of the line starting from the second quotation mark the same, and change everything in between to 'fool2'. > > 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... > > \0 is the entire matching expression/line. \n is tokenizing. By using parentheses in the regexp, you can reuse the texts in the parentheses in the substitution. For example, consider the expression s/\(.at\).*\(.ox\)/\2 not \1/p If we send it The bat scared the fox. it would print fox not bat because \2 gets replaced by the second matching token, which in this case, is fox, because it matches ".ox". Similarly for bat and the first token. If we send the same expression The cat died in the box. it would print box not cat I can't explain the concept very well, and I hope the examples are good enough. Seriously, one of the best way to learn sed is to read the manual from `info sed' W -- Here lies Lester Moore Four slugs from a .44 No Les No more. Sortir en Pantoufles: up 4 days, 8:14 -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 5:38 ` Rafael Barreto 2005-11-07 5:42 ` Rafael Barreto @ 2005-11-09 4:37 ` Walter Dnes 2005-11-09 8:56 ` Neil Bothwick 1 sibling, 1 reply; 14+ messages in thread From: Walter Dnes @ 2005-11-09 4:37 UTC (permalink / raw To: gentoo-user On Mon, Nov 07, 2005 at 03:38:00AM -0200, Rafael Barreto wrote > Is this right? Well, what I really want is replace just CLOCK="fool1" > by CLOCK="fool2" keeping the comments in line. That is not what sed is designed to do. "sed" is "Streaming EDitor". You specify an input file, and the changed file goes to STDOUT. If you want to change the original file, you need to use "ed". For details, "man ed". -- Walter Dnes <waltdnes@waltdnes.org> In linux /sbin/init is Job #1 My musings on technology and security at http://tech_sec.blog.ca -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-09 4:37 ` Walter Dnes @ 2005-11-09 8:56 ` Neil Bothwick 0 siblings, 0 replies; 14+ messages in thread From: Neil Bothwick @ 2005-11-09 8:56 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 389 bytes --] On Tue, 8 Nov 2005 23:37:52 -0500, Walter Dnes wrote: > That is not what sed is designed to do. "sed" is "Streaming EDitor". > You specify an input file, and the changed file goes to STDOUT. If you > want to change the original file, you need to use "ed". Or use sed's -i or --in-place argument. -- Neil Bothwick Never drink coffee that's been anywhere near a fish. [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 189 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 5:03 ` gentuxx 2005-11-07 5:38 ` Rafael Barreto @ 2005-11-07 6:55 ` Willie Wong 2005-11-07 7:35 ` gentuxx 1 sibling, 1 reply; 14+ messages in thread From: Willie Wong @ 2005-11-07 6:55 UTC (permalink / raw To: gentoo-user On Sun, Nov 06, 2005 at 09:03:01PM -0800, gentuxx wrote: > sed -n '/^CLOCK=/s/^\(CLOCK=".*"\).*$/\1/p /etc/conf.d/clock > Ah, yes, I misunderstood the OP. I thought he didn't want the lines with trailing comments at all. But is it necessary to give the address for an s// replacement? As I understand it that sed is a stream editor and will try the replacement on every "line" it encounters. The -n flag would guarantee only the line changed would be printed anyway. I guess what I am saying is that sed -n 's/^\(CLOCK=".*"\).*$/\1/p' /etc/conf.d/clock would do just as fine, no? W -- The particle physicists use order parameter fields, too. Their order parameter fields also hide lots of details about what their quarks and gluons are composed of. The main difference is that they don't know of what their fields are composed. It ought to be reassuring to them that we don't always find our greater knowledge very helpful. ~James P. Sethna "Order Parameters, Broken Symmetry, and Topology" Sortir en Pantoufles: up 4 days, 8:10 -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 6:55 ` Willie Wong @ 2005-11-07 7:35 ` gentuxx 0 siblings, 0 replies; 14+ messages in thread From: gentuxx @ 2005-11-07 7:35 UTC (permalink / raw To: gentoo-user -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Willie Wong wrote: >On Sun, Nov 06, 2005 at 09:03:01PM -0800, gentuxx wrote: > >>sed -n '/^CLOCK=/s/^\(CLOCK=".*"\).*$/\1/p /etc/conf.d/clock >> > >Ah, yes, I misunderstood the OP. I thought he didn't want the lines >with trailing comments at all. > He didn't really make that clear in his first post. ;-) >But is it necessary to give the address for an s// replacement? As I >understand it that sed is a stream editor and will try the replacement >on every "line" it encounters. The -n flag would guarantee only the >line changed would be printed anyway. > No, the address is NOT necessary for this case. I stated that in my reply to his post. However, sed will only replace the first instance found, unless the 'g' flag is specified ( s///g ). Then it'll search and replace globally. The '-n' option suppresses unchanged output, so if the output of the sed command was being redirected to a file, only the changed lines would be printed. So, in this case, you would NOT want to use the '-n' option, because you would want the entire file output *with* the changes. >I guess what I am saying is that > sed -n 's/^\(CLOCK=".*"\).*$/\1/p' /etc/conf.d/clock >would do just as fine, no? > >W (See above). To actually make changes to the file, once you had tested your regular expressions to STDOUT, you would most likely want to redirect it to a file to replace the original file. If you include the '-n' option, all of the unchanged lines would not be printed or redirected - so you would have your changes, but nothing else. So for your final "run", you would want something without the '-n' option. And without the '-n' option, the print command ( s///p ) is moot. (See my most recent reply to the OP for a final solution.) - -- 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) iD8DBQFDbwOjLYGSSmmWCZMRAmG9AJ46OE4xLI3pCKnb3RTQSCIGWNKSBgCfWOOl V9zFRF6QNqmVSK0LHjt4e9c= =tnMC -----END PGP SIGNATURE----- -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [gentoo-user] About sed 2005-11-07 3:44 [gentoo-user] About sed Rafael Barreto 2005-11-07 4:07 ` Willie Wong @ 2005-11-07 7:49 ` Alexander Skwar 1 sibling, 0 replies; 14+ messages in thread From: Alexander Skwar @ 2005-11-07 7:49 UTC (permalink / raw To: gentoo-user 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. No, that's not what it does. It matches "CLOCK" at "beginning of line" (^) followed by = followed by any number of " followed by another ", which is the last character of the line. You forgot the . before the *. > Other thing... if i put: > > sed '/^CLOCK=*/p' /etc/conf.d/clock > > the return will be anything that contains CLOCK. Why? No, it will return all lines, that start with CLOCK, followed by any numbers of =. You again missed the . before the *. > Thanks a lot and sorry by my english... No problem at all, but please do not send HTML junk to the list. That's not so much excusable (well, that's of course an exaggeration by me...). Alexander Skwar -- Men of quality are not afraid of women for equality. -- gentoo-user@gentoo.org mailing list ^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2005-11-09 11:11 UTC | newest] Thread overview: 14+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2005-11-07 3:44 [gentoo-user] About sed Rafael Barreto 2005-11-07 4:07 ` Willie Wong 2005-11-07 4:19 ` Rafael Barreto 2005-11-07 5:03 ` gentuxx 2005-11-07 5:38 ` Rafael Barreto 2005-11-07 5:42 ` Rafael Barreto 2005-11-07 6:37 ` gentuxx 2005-11-07 7:25 ` [gentoo-user] [OT] " Willie Wong 2005-11-07 7:19 ` [gentoo-user] " Willie Wong 2005-11-09 4:37 ` Walter Dnes 2005-11-09 8:56 ` Neil Bothwick 2005-11-07 6:55 ` Willie Wong 2005-11-07 7:35 ` gentuxx 2005-11-07 7:49 ` Alexander Skwar
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox