Smart-rule action: split at delimiter

How boring :wink:

Because that way I wouldn’t have a snowball’s chance in hell of learning how to script this stuff :upside_down_face:

Yes. Strictly speaking, the -e is not necessary here because there’s only one command. I tend to use it because it makes adding more stuff easier.

substitute: s/// replace the regular expression between the first // with the stuff before the closing /
s/a/b/ : substitute a with b
s/a+/b/ : substitute a sequence of at least one a (but possibly any number of them) with a single b
And so on

Nope. \ marks the following h as “any horizontal space”

&& means: execute the 2nd command of the first one ran successfully. This is a bit tricky here: command are executed in a subshell, each one separately in general. So
cd blah would change the directory to blah in the subshell. After that, the next command would know nothing of the cd because the subshell has terminated. && binds the two command together so that they’re executed in the same subshell. I hope that makes it clearer.

1 Like

I’m intensely grateful for your explanation :relieved:

Because that way I wouldn’t have a snowball’s chance in hell of learning how to script this stuff

Don’t be too quick to rush into the shell. As I have said for a very long time: In UNIX, there is no forgiveness. (Not really a joke either.)

I once wiped out about 4 years of research in about 30 seconds by misplacing a single forward slash in a UNIX command. And not, “Oh! It’s just in the system Trash.”. I mean - gone.

Tread cautiously.

2 Likes

I have a Scrivener project with my writing. Every now and then, I want to compile the project and have it replace some files in my Wiki in DT3. My hope is to save the markdown file to a folder where it will trigger a sequence of action: split at H1, rename all files to first line (minus # ) and then move them to the folder where the wiki files are indexed in order to replace them.

If I am not missing something here, were I to import them to DT3 before any of this, they would receive a new UUID and I would end up with a bunch of duplicate files.

were I to import them to DT3 before any of this, they would receive a new UUID and I would end up with a bunch of duplicate files.

Why would you need to import them if you’re indexing the location? :thinking:

(Or add them to another indexed folder.)
To perform the smart-rule action as you suggested.

To make it clearer:

Scrivener will export the project (some +1000 files that correspond to my wiki files) as a single md file. I need to split and rename them to have them replace the original files that are in Finder.

It seems that I have to do this first outside of DT and then have them replace the original files. Were I to index or import them into DT to perform the smart-rule action of renaming then I would end up with duplicates.

The day before yesterday I renamed all my files to the first line of the content with an extra .md at the end.

All
of
them.

Took a whole day of work to create a new user and restore everything back to where it was.
So yeah, tread cautiously. (That is why I am taking making sure to ask around as much as possible before attempting anything again.)

2 Likes

Thanks too, for that - I will tread cautiously. I followed advice here on the forum and in addition to my previous 4 backup strategies I additionally save important data to Blu-ray. I’m paranoid about data loss. Still going to go for understanding shell script and regex better though :slight_smile:

This is a good place to be - thanks, you three, for caring, teaching and helping.

As I said somewhere else: my biggest blunder was

cd /
rm -r *

run as root. Oh, the joys of setting up a complete Unix system from tape again.
Fortunately, it’s not so easy to do this nowadays.

You’re welcome.

This is also a reason @cgrunenberg prefers I don’t *nix it up too much on the forums. Not only is AppleScript simpler to learn and understand for new users, it is usually far safer. The shell has it’s place - and it’s incredibly fast - but that also means you can screw things up at an unbelievable rate too! :open_mouth:

@Bernardo_V: Ouch! I feel your pain. :wink:

Ugh!
It still ticks me off when I see people suggesting a command like that to new users, just to be malicious. :rage:

Any suggestions from your hard-earned wisdom? :smile:

It seems that I have to do this first outside of DT and then have them replace the original files. Were I to index or import them into DT to perform the smart-rule action of renaming then I would end up with duplicates.

True since we generally don’t like the idea of overwriting files ilke this, especially when it comes to indexed files.

1 Like

What am I doing wrong here?

# is still showing up.

Does escaping # with a \ help?

Not really :confused:

Edit: Ok, abandoning \h for a simple space appears to work.

Thanks again, @chrillek!

Apple’s sed implementation is peculiar. Swear words would describe it better.

sed -e 's/# //'

works

sed -e 's/# +//'

doesn’t. \h is not recognized at all (see man re_format). And sed seems to behave itself only if you feed it the “-E” option (cf https://unix.stackexchange.com/questions/345604/why-is-my-regex-not-working-using-sed-in-bash-script-on-mac-osx).
So
sed -E -e 's/#[ \t]+//'
should do the trick. Dis-disclaimer: it indeed does, I just tried it.

@Blanc: [ \t] is a character class consisting of a space and a tab character (\t). The following + sign means: at least one of those, but as many as you want.

@BLUEFROG: Indeed, one shouldn’t probably revert to the command line on Mac OS if it provides tools as crappy as this one.

1 Like

BSD sed is not GNU sed but it’s still an amazing tool. One of my favorites actually.