Re: [GTALUG] Hardcore sed foo
On Sat, Nov 09, 2019 at 01:55:24PM -0500, Alvin Starr via talk wrote: > It was hard to write. It should be hard to understand. "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian W. Kernighan -- Len Sorensen --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On 11/9/19 9:34 AM, Anthony de Boer via talk wrote: Dave Collier-Brown via talk wrote: On 2019-11-08 11:41 a.m., Giles Orr via talk wrote: Eyeballing simple regex is hard enough, but mentally parsing out a complex full line regex that looks like inline garbage without the benefit of comments isn't something I recommend or appreciate. An early boss described it as "programming in line noise". I've seen too many cases where the comments explaining what a block of code did were *wrong*, and debugging took far too long due to coders reading, trusting, or simply being distracted by this alternate theory of what the code was doing. It was hard to write. It should be hard to understand. -- Alvin Starr || land: (647)478-6285 Netvel Inc. || Cell: (416)806-0133 al...@netvel.net || --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
Dave Collier-Brown via talk wrote: > On 2019-11-08 11:41 a.m., Giles Orr via talk wrote: > > Eyeballing simple regex is hard enough, but mentally parsing out a > > complex full line regex that looks like inline garbage without the > > benefit of comments isn't something I recommend or appreciate. > > An early boss described it as "programming in line noise". I've seen too many cases where the comments explaining what a block of code did were *wrong*, and debugging took far too long due to coders reading, trusting, or simply being distracted by this alternate theory of what the code was doing. Being able to read the code itself and figure out what it's doing is a primary programmer skill, and writing code that's not harder to figure out than it needs to be is important too. With regexen for eg matching logfile lines, a comment with a few different example lines that should match can be very helpful. Some explanation of *why* you're going to the trouble of doing something, or a simple explanation of what task a block of code encapsulates, are useful things for a comment block. But line-by-line attempts at translating code to English are counterproductive. -- Anthony de Boer --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On 2019-11-08 3:13 p.m., Lennart Sorensen wrote: > On Fri, Nov 08, 2019 at 07:27:47PM +, Dave Collier-Brown via talk wrote: >> On 2019-11-08 11:41 a.m., Giles Orr via talk wrote: >> >>> Eyeballing simple regex is hard enough, but mentally parsing out a >>> complex full line regex that looks like inline garbage without the >>> benefit of comments isn't something I recommend or appreciate. >> An early boss described it as "programming in line noise". > I thought that was perl. > No, that was "you can say anything 27 ways", but you have to use words. --dave -- David Collier-Brown, | Always do right. This will gratify System Programmer and Author | some people and astonish the rest dave.collier-br...@indexexchange.com | -- Mark Twain CONFIDENTIALITY NOTICE AND DISCLAIMER : This telecommunication, including any and all attachments, contains confidential information intended only for the person(s) to whom it is addressed. Any dissemination, distribution, copying or disclosure is strictly prohibited and is not a waiver of confidentiality. If you have received this telecommunication in error, please notify the sender immediately by return electronic mail and delete the message from your inbox and deleted items folders. This telecommunication does not constitute an express or implied agreement to conduct transactions by electronic means, nor does it constitute a contract offer, a contract amendment or an acceptance of a contract offer. Contract terms contained in this telecommunication are subject to legal review and the completion of formal documentation and are not binding until same is confirmed in writing and has been signed by an authorized signatory. --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
> On Fri, Nov 08, 2019 at 07:27:47PM +, Dave Collier-Brown via talk wrote: > > On 2019-11-08 11:41 a.m., Giles Orr via talk wrote: > > An early boss described it as "programming in line noise". > > I thought that was perl. Or maybe APL. -- Scott --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On Fri, Nov 08, 2019 at 07:27:47PM +, Dave Collier-Brown via talk wrote: > On 2019-11-08 11:41 a.m., Giles Orr via talk wrote: > > > Eyeballing simple regex is hard enough, but mentally parsing out a > > complex full line regex that looks like inline garbage without the > > benefit of comments isn't something I recommend or appreciate. > > An early boss described it as "programming in line noise". I thought that was perl. -- Len Sorensen --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On 2019-11-08 11:41 a.m., Giles Orr via talk wrote: > Eyeballing simple regex is hard enough, but mentally parsing out a > complex full line regex that looks like inline garbage without the > benefit of comments isn't something I recommend or appreciate. An early boss described it as "programming in line noise". --dave -- David Collier-Brown, | Always do right. This will gratify System Programmer and Author | some people and astonish the rest dave.collier-br...@indexexchange.com | -- Mark Twain CONFIDENTIALITY NOTICE AND DISCLAIMER : This telecommunication, including any and all attachments, contains confidential information intended only for the person(s) to whom it is addressed. Any dissemination, distribution, copying or disclosure is strictly prohibited and is not a waiver of confidentiality. If you have received this telecommunication in error, please notify the sender immediately by return electronic mail and delete the message from your inbox and deleted items folders. This telecommunication does not constitute an express or implied agreement to conduct transactions by electronic means, nor does it constitute a contract offer, a contract amendment or an acceptance of a contract offer. Contract terms contained in this telecommunication are subject to legal review and the completion of formal documentation and are not binding until same is confirmed in writing and has been signed by an authorized signatory. --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On Thu, 7 Nov 2019 at 23:26, Anthony de Boer via talk wrote: > Lennart Sorensen via talk wrote: > > On Tue, Nov 05, 2019 at 06:42:44AM -0500, Giles Orr via talk wrote: > > > ... > > > Jeez regex is a pain - it took three minutes of staring at that just to > > > figure it out, even though I knew what we were both trying to achieve. > > > > Practice time! > > > > https://regexcrossword.com > > Oooh, fun! The Hamlet puzzle took a few minutes to sort out. But > then I'm not a regular crossworder. > > One of the guys I work with claims that any use of regexen requires > multiple lines of comment to explain, but I'm firmly of the opinion > that being able to sight-read them is a basic Unix programming skill. > Eyeballing simple regex is hard enough, but mentally parsing out a complex full line regex that looks like inline garbage without the benefit of comments isn't something I recommend or appreciate. Some forms of regex support comments within the regex itself: https://www.regular-expressions.info/freespacing.html However ... "Of the flavors discussed in this tutorial, only ... the POSIX and GNU flavors don't support it." Which is where I most need it. Of course, you can still make standard shell script comments before or after the 'sed' block, and I'm very much in favour of those. -- Giles https://www.gilesorr.com/ giles...@gmail.com --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
Lennart Sorensen via talk wrote: > On Tue, Nov 05, 2019 at 06:42:44AM -0500, Giles Orr via talk wrote: > > ... > > Jeez regex is a pain - it took three minutes of staring at that just to > > figure it out, even though I knew what we were both trying to achieve. > > Practice time! > > https://regexcrossword.com Oooh, fun! The Hamlet puzzle took a few minutes to sort out. But then I'm not a regular crossworder. One of the guys I work with claims that any use of regexen requires multiple lines of comment to explain, but I'm firmly of the opinion that being able to sight-read them is a basic Unix programming skill. -- Anthony de Boer --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On Tue, Nov 05, 2019 at 06:42:44AM -0500, Giles Orr via talk wrote: > On Tue, 5 Nov 2019 at 01:47, William Park via talk wrote: > > > 0 or 1 match is ? > > Eg. echo /aaa/.bbb//ccc/123 | sed 's,\(/\.\?[^/]\)[^/]*,\1,g' > > --William > > > > Nice - that works too. > > Jeez regex is a pain - it took three minutes of staring at that just to > figure it out, even though I knew what we were both trying to achieve. Practice time! https://regexcrossword.com -- Len Sorensen --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On Tue, 5 Nov 2019 at 06:42, Giles Orr wrote: > On Tue, 5 Nov 2019 at 01:47, William Park via talk > wrote: > >> 0 or 1 match is ? >> Eg. echo /aaa/.bbb//ccc/123 | sed 's,\(/\.\?[^/]\)[^/]*,\1,g' >> --William >> > > Nice - that works too. > > Jeez regex is a pain - it took three minutes of staring at that just to > figure it out, even though I knew what we were both trying to achieve. > PS. Yours works better: mine fails on the double slash '//' because it insists on looking for a non-slash item right after it and so produces '/a/.b///1' when it should have given me '/a/.b//c/1' as yours did. It's somewhat academic as in processing prompts the regex should never encounter two slashes with nothing between them, but ... I prefer the more correct answer. Thanks. -- Giles https://www.gilesorr.com/ giles...@gmail.com --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On Tue, 5 Nov 2019 at 01:47, William Park via talk wrote: > 0 or 1 match is ? > Eg. echo /aaa/.bbb//ccc/123 | sed 's,\(/\.\?[^/]\)[^/]*,\1,g' > --William > Nice - that works too. Jeez regex is a pain - it took three minutes of staring at that just to figure it out, even though I knew what we were both trying to achieve. -- Giles https://www.gilesorr.com/ giles...@gmail.com --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
0 or 1 match is ?Eg. echo /aaa/.bbb//ccc/123 | sed 's,\(/\.\?[^/]\)[^/]*,\1,g'--William Sent from Yahoo Mail on Android On Mon, Nov 4, 2019 at 8:24 PM, Giles Orr via talk wrote: --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
On Mon, 4 Nov 2019 at 18:36, William Witteman wrote: > No sed knowledge here, but what if you turn it around, and grab two > characters and conditionally truncate, rather than the other way around? > > My only useful programming advice is when you get stuck, turn your problem > around. > > On Mon., Nov. 4, 2019, 18:31 Giles Orr via talk, wrote: > >> I've tinkered with Bash prompts for a lot of years. That's where this >> problem originates, but it can be considered just as a thought experiment >> if you prefer. >> >> We have the directory we're in, for example: >> >> /Users/gorr/.bashprompt/really/deep/directory/structure/even/deeper >> >> (I keep my prompts in ~/.bashprompt/ , and a very long directory name is >> often a cause of breakage, so I keep one around to break them ...) >> >> I want to shorten the directory name to just the first letters: >> >> export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@ >> \(/.\)[^/]*@\1@g') >> echo $newPWD >> ~/./r/d/d/s/e/d >> >> This does '~' replacement for $HOME and then substitutes the first letter >> of each directory for the full directory name. >> >> Here's the question: if the first letter of the directory name is a dot >> '.', can sed then capture one character more so that the output would >> become: >> >> ~/.b/r/d/d/s/e/d >> >> I think this would be pretty easy with Bash and a loop, but that's a lot >> of processing so I'd rather not go down that road. I suspect sed is >> capable of this, but I haven't delved deeply enough into the tool to even >> know where to start. This may in fact be a regex problem more than a sed >> problem - either way I'm kind of stumped. I'm open to simpler >> implementations using other (standard system) tools as well. >> > All that was needed was a walk home to think and solve it: export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@ \(/[.].\|/.\)[^/]*@\1@g') ; echo $newPWD ~/.b/r/d/d/s/e/d It's a regex problem: capture a group that's a slash, a dot, and a character, OR a slash and a character. -- Giles https://www.gilesorr.com/ giles...@gmail.com --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
Re: [GTALUG] Hardcore sed foo
No sed knowledge here, but what if you turn it around, and grab two characters and conditionally truncate, rather than the other way around? My only useful programming advice is when you get stuck, turn your problem around. On Mon., Nov. 4, 2019, 18:31 Giles Orr via talk, wrote: > I've tinkered with Bash prompts for a lot of years. That's where this > problem originates, but it can be considered just as a thought experiment > if you prefer. > > We have the directory we're in, for example: > > /Users/gorr/.bashprompt/really/deep/directory/structure/even/deeper > > (I keep my prompts in ~/.bashprompt/ , and a very long directory name is > often a cause of breakage, so I keep one around to break them ...) > > I want to shorten the directory name to just the first letters: > > export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@ > \(/.\)[^/]*@\1@g') > echo $newPWD > ~/./r/d/d/s/e/d > > This does '~' replacement for $HOME and then substitutes the first letter > of each directory for the full directory name. > > Here's the question: if the first letter of the directory name is a dot > '.', can sed then capture one character more so that the output would > become: > > ~/.b/r/d/d/s/e/d > > I think this would be pretty easy with Bash and a loop, but that's a lot > of processing so I'd rather not go down that road. I suspect sed is > capable of this, but I haven't delved deeply enough into the tool to even > know where to start. This may in fact be a regex problem more than a sed > problem - either way I'm kind of stumped. I'm open to simpler > implementations using other (standard system) tools as well. > > -- > Giles > https://www.gilesorr.com/ > giles...@gmail.com > --- > Post to this mailing list talk@gtalug.org > Unsubscribe from this mailing list > https://gtalug.org/mailman/listinfo/talk > --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
[GTALUG] Hardcore sed foo
I've tinkered with Bash prompts for a lot of years. That's where this problem originates, but it can be considered just as a thought experiment if you prefer. We have the directory we're in, for example: /Users/gorr/.bashprompt/really/deep/directory/structure/even/deeper (I keep my prompts in ~/.bashprompt/ , and a very long directory name is often a cause of breakage, so I keep one around to break them ...) I want to shorten the directory name to just the first letters: export newPWD=$(echo ${PWD} | sed -e "s@${HOME}@~@" -e 's@ \(/.\)[^/]*@\1@g') echo $newPWD ~/./r/d/d/s/e/d This does '~' replacement for $HOME and then substitutes the first letter of each directory for the full directory name. Here's the question: if the first letter of the directory name is a dot '.', can sed then capture one character more so that the output would become: ~/.b/r/d/d/s/e/d I think this would be pretty easy with Bash and a loop, but that's a lot of processing so I'd rather not go down that road. I suspect sed is capable of this, but I haven't delved deeply enough into the tool to even know where to start. This may in fact be a regex problem more than a sed problem - either way I'm kind of stumped. I'm open to simpler implementations using other (standard system) tools as well. -- Giles https://www.gilesorr.com/ giles...@gmail.com --- Post to this mailing list talk@gtalug.org Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk