Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Useful sed scripts (Read 9606 times)
Paul Walters
Full Member
*
Offline



Posts: 267

Useful sed scripts
Oct 14th, 2012, 6:42am
 
Useful one-line scripts for sed

(thanks to Gabe Weatherhead for the link)
Back to top
 
« Last Edit: Oct 14th, 2012, 12:45pm by Paul Walters »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Useful sed scripts
Reply #1 - Oct 14th, 2012, 7:44am
 
From experience, unless using a really short/simple command line with runCommand (or in $HTMLCommand, etc.), you are invariably better off using a 'code note'. A happy benison of such an approach is it tends to avoid the problem of runCommand() needing to use a quoted string for the CL and the latter needing to use both single/double quotes itself.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Useful sed scripts
Reply #2 - Oct 14th, 2012, 12:15pm
 
My own preference differs from Mark Anderson's , though he is not wrong.

His sense is, I think, that a complex runCommand is saying,"make me a code note".

What I hear the same complex expressing saying, though, is "male me a script."  Take your complex runCommand, test it in Terminal, and store it in a file in some convenient place.  perl and ruby were (literally) made for this.  

Back to top
 
 
WWW   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Useful sed scripts
Reply #3 - Oct 14th, 2012, 9:28pm
 
Thanks to Paul Walters for the useful link!

What are the main advantages/disadvantages of "make me a code note (and store a script there)" vs. "make me a script (and store it in a separate file)"?
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Useful sed scripts
Reply #4 - Oct 15th, 2012, 12:51pm
 
They're alternatives and I think the pros/cons rather depend on your expertise and style of TB use.

runCommand()/$AutoFetchCommand/$HTMLEXportCommand.
  • Pro. No setup needed just add the CL string and use.
  • Con. Can be hard to use with other than simple CLs especially if needing to use lots of quotes.
Using a shell script:
  • Pro. Work as any other shell script and can be stored in the same palace.
  • Pro. Easy re-use by multiple TBXs.
  • Pro. In latter case, one edit (to script) affects/corrects for all users.
  • Pro. No problems dealing with single vs double quotes - just pass data to script as input parameters (assuming you know how!).
  • Con. Assumes user understands Unix permissions, environment variables, where to store the script
Using a code note:
  • Pro. Portable, will work from any Mac as script is stored in the TBX.
  • Pro. No external set-up problems (e.g. how to call script), so good for CL neophytes.
  • Pro. Once set up and tested can be replicated to other TBXs via drag drop.
  • Pro/Con. Easy to set up a command line without having to worry about quoting within an actual runCommand() action - instead the evaluation of the code note becomes the runCommand() string. The downside is it's not suitable for writing complex shell functions/scripts.
  • Con. Some internal set-up required (prototypes, etc.) though this isn't totally required and only needed once per TBX.
  • Con. The most complicated tasks may need an external script to avoid any TB parsing issues.
There's absolutely no right/wrong here, use whatever suits you, but it is certainly true that for anything other than very simple CL tasks either of the latter options is a good idea. A techie person well versed in Unix, and CL will likely go the script route. Someone helping other users, especially non-tech types and where files need to be configured for others to use, will likely tend to code notes. Use whatever suits your needs. The main thing is to understand that if runCommand() doesn't suffice, there are more powerful options to tap into the shell - even if only to access UI-based tools like AppleScript or Automator.

Edited:
In the light of subsequent discussion - now split to a separate thread, 'code notes' are best seen as an easier way to assemble the string to be used by runCommand() rather than doing such assembly in the command itself. For anything more complex, use an external shell script to to do the heavy lift.
Back to top
 
« Last Edit: Oct 16th, 2012, 2:07pm by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Useful sed scripts
Reply #5 - Oct 16th, 2012, 5:55am
 
Off-Topic replies have been moved to this Topic.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Useful sed scripts
Reply #6 - Oct 16th, 2012, 2:22pm
 
Thanks, Mark A. "Interacting with the Mac shell" is indeed a much better description for the replies in this thread.  However, the cutoff to this broader topic begins at Reply #1, no? Yet the only replies you have banished as "off topic" (mine) were in direct response to #1, #2, and #4 above.  I believe these and mine belong together under the same rubric, whatever and wherever that may be.  Why resemble the territory behind the GFC (Great Firewall of China) where posts that might "confuse" the people tend to get isolated and otherwise "harmonized" in various ways?

Back on the original narrow topic of this thread, sed...

This seems to be an amazingly concise and efficient way to do lots of things, though the "picket fence" of slashes and backslashes certainly intimidates. One sed tip I've come across that has helped me (a little) is here. The separators don't have to be slashes; they can be any (ascii, I presume) character one wants to use.

Another thing I've discovered is that if one needs to use sed in AppleScript to output text from DEVONthink or elsewhere that Tinderbox can import without complaint (e.g. change straight quotes to curlies) and one has the sed working on the command line or via the two-argument form of runCommand() in Tinderbox, one can get the quoted form of the sed command that AppleScript will accept by saving the sed command in a script file and then reading into AppleScript from the file. For example, for Mark A's quotify:

sed 's:"\([^"]*\)":“\1”:g' | sed "s:'\([d|ll|re|s|t|ve]\):’\1:g" | sed "s:'\([^']*\)':‘\1’:g"

When I put it in a file called quotify.txt on my desktop I can get the AppleScript-friendly form as follows in AppleScript Editor, ready for copying and pasting into an AppleScript do shell script():

Back to top
 
« Last Edit: Oct 16th, 2012, 2:26pm by Sumner Gerard »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Useful sed scripts
Reply #7 - Oct 16th, 2012, 6:40pm
 
Oh dear. It's not personal! The thread swung off topic and with a thought to future readers/searchers I broke it at what I (for better or worse) saw as the point of divergence. It's all cross-linked, so nothing's lost but it will, I believe, make better sense to those looking at this some months down the line.

As to the sed issue, I'm confused, if the string from the 'codify' code note turns up without enclosing quotes, why not add them in AppleScript** without using the extra text file? Or, as Mark B suggests run the sed in shell script and simply pass it the string to be cleaned as a script parameter.

** AppleScript can escape quotes in strings.
Back to top
 
« Last Edit: Oct 16th, 2012, 6:42pm by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Sumner Gerard
Full Member
*
Offline



Posts: 359

Re: Useful sed scripts
Reply #8 - Oct 17th, 2012, 1:34pm
 
Quote:
I'm confused, if the string from the 'codify' code note turns up without enclosing quotes, why not add them in AppleScript** without using the extra text file?

See this post.  Lest it be marginalized as overkill or something that might confuse later readers, let me say that the script there is nothing fancy, just something eminently practical for bringing linked DEVONthink material into Tinderbox without a lot of fiddling, and is based directly on your valuable contributions. Smiley

Note that the script there does in fact "run the sed in shell script and simply pass it the string to be cleaned as a script parameter."

Because it's AppleScript, and as far as I can tell there is no equivalent of the two-argument form of runCommand(), the script there uses the AppleScript-friendly form of your quotify sed. Try putting your quotify sed directly in an AppleScript (surrounded by quotes, of course) and I think you'll find that, as on my machine, it throws off an error because it needs to be escaped. I got miserably lost in the sed picket fence trying to add backslashes in the appropriate places.

Then I stumbled on an easy, practical solution. I put your quotify, which I knew worked in Tinderbox's two-argument form of runCommand(), in a text file and read the file into AppleScript as described in reply #6 above (at least it's "above" as I write this Smiley).  I noticed an escaped form of quotify in the AppleScript editor result panel and pasted that into the appropriate place in the DEVONthink script to be fed into the do shell script statement.  AppleScript Editor accepted it!  I could then discard the unneeded external text file. Problem solved for me.

... Of course those with true prowess with sed may enjoy going in manually and spending time mucking around. But that's not for me, and in case there are other general users like me who are learning a little AppleScript to do useful things and occasionally need a do shell script there to get stuff into Tinderbox, I thought I'd share a practical tip.

BTW, I found quite a bit on the net about converting curlies to straight quotes, but your quotify is the only reliable method I've found for going the other way. The AppleScript-friendly version of it has been helpful in solving perplexing problems that I was having when putting tab-separated-data tables on the clipboard for pasting into Tinderbox, which I've found a supremely efficient way for getting data into Tinderbox since it creates new notes automatically, complete with the appropriate attributes, etc.  Thanks.
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Useful sed scripts
Reply #9 - Oct 17th, 2012, 3:37pm
 
Glad it all works.  Like you, I'm no expert at shell stuff and I'd agree shell tools aren't that accessible to the general user (me included!).
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print