In all the files in my project, where a certain string of characters is present, I'm trying to replace a string with the a part of the file name containing said string.
For example, files are called ace[1-12].xml, and for everytime there is a the block: "mc\d+(.*\n.*True)" I want to replace mc\d+ with tf0(number prefix from filename). I know this hits on a couple more adavanced concepts I don't have a full grasp on yet, but this editor is goading me on to learn regex and basic command line skills...
The command I have now is:
Save: nothing
Command: eval arr=("$TM_SELECTED_FILES") for (( i = 0; i < ${#arr[@]}; i++ )); do perl -pe 'while ($string =~ m/mc\d+(.*\n.*True)/g) {$string =~ s/mc\d+/tf$TM_FILENAME/g;}'; done
Input: Entire Doc
Output: Replace selected text
It's choking big time... any pointers to the numerous places I'm screwing up?
Thanks~
On 19. Oct 2006, at 22:12, J Fishwick wrote:
Command: eval arr=("$TM_SELECTED_FILES") for (( i = 0; i < ${#arr[@]}; i++ )); do perl -pe 'while ($string =~ m/mc\d+(.*\n.*True)/g) {$string =~ s/mc\d+/tf$TM_FILENAME/g;}'; done
Input: Entire Doc Output: Replace selected text
You need to set input/output to none/discard (or show tool tip, or maybe show as HTML and make your command output some progress info).
Then your actual command needs to load the file itself, it already iterators over the selected files, it just doesn’t tell perl to load/ overwrite each of them.
So a simple example would instead be:
eval arr=("$TM_SELECTED_FILES") for f in "${arr[@]}"; do perl -i.bak -pe 's/bar/bar/' "$f" done rescan_project # do this after you make changes to files, so TM will rescan
This will replace foo with bar in all selected files (and create a .bak file with the old contents).
Two other things: 1) inside single quoted strings, shell variables are not expanded, so if you want to use $TM_… then you need to use double quotes, and 2) TM_FILENAME is the filename of the file open when you called the file, it won’t update inside the loop. Here you would instead use the current file given by the loop (in my example $f).