r/applescript • u/jonwku • Nov 01 '21
Changing ">abc" to ">def"
I have a massive text file with X number of entries, each titles beginning with ">abc" and consisting otherwise of alphanumeric characters. I would like to change ">abc" to ">def" and retain the rest. Am I getting in over my head?
2
Upvotes
1
u/jonwku Nov 02 '21
Thanks everyone for the suggestions! I'm going to try to implement some tomorrow.
2
u/musicmusket Nov 01 '21
The >abc text appears in places other than the beginning of each line? You don’t want to use TextEdit and just find/replace because the other >abcs would change too?
If that’s right you could do it easily in a regex app. CotEditor and BBEdit are both free and excellent.
Open the file. Search for
^>abcThe ^ means only at the beginning of the line, so subsequent >abcs will be ignored. BBEdit gives you a view of the lines with hits, which helps. Once you’re happy, replace the hits with >def.1
u/copperdomebodha Nov 01 '21 edited Nov 01 '21
If you know that the only occurrences of the "<abc" key are ones that should be replaced then this code is almost instantaneous on large files...
If it is possible that the text contains instances of the ">abc" key that should be preserved then you can use the following code that will only remove instances of ">abc" that begin a line.
0
u/Kina_Kai Nov 01 '21
You're better off using Ruby, Python or Perl (or even sed). In my experience, AppleScript text processing support is nonexistent.
You can certainly try and do some of it in AppleScript and pawn the hard parts off to Ruby or Python through a
do shell scriptcall, but trying to do this in pure AppleScript is going to be painful.