On Oct 23, 2014, at 14:25, [email protected] wrote:
> 1. Run multi finds using  a series of grep searches (not replaces). There 
> might be up to 10 or more searches.
> 2. Put the output found (results) into one document so they are all together 
> (prefixed by document path where they were found)
______________________________________________________________________

Hey Mark,

I believe you initially said you wanted to delete or move the matched files, so 
I'm going to start with that.

I have commented-out delete in the script below, so you can see how it works 
without sending anything to the trash.  Uncomment # delete foundFiles and 
remove foundFiles to send the found files to the trash.

I've employed the long form of command-line switches for clarity.

-------------------------------------------------------------------------------------------
set targetFolder to quoted form of "/Users/chris/test_directory/folders_nested/"
set _patterns to quoted form of "
one.*four
three.*nine
ten.*twenty
"

set shCMD to "grep --extended-regexp --ignore-case --recursive" & " \"" & 
_patterns & "\" " & targetFolder & " \\
| sed -En '/:/{
s!:[^:]+$!!
p
}
' \\
| sort -u"

set foundFiles to do shell script shCMD

if foundFiles ≠ "" then
  
  set foundFiles to paragraphs of foundFiles
  repeat with i in foundFiles
    set contents of i to alias POSIX file i
  end repeat
  
  tell application "Finder"
    # delete foundFiles
  end tell
  
  foundFiles
  
end if
-----------------------------------------------------------------------------------------

Move the found files instead of deleting them.

The destination folder must already exist, although it wouldn't be difficult to 
make one on-the-fly.

The Finder's move command has a replacing option that will replace any 
collisions.  I have NOT used that in the script.

-------------------------------------------------------------------------------------------
set targetFolder to quoted form of "/Users/chris/test_directory/folders_nested/"
set destFolder to alias "Ryoko:Users:chris:Downloads:dest:"
set _patterns to quoted form of "
one.*four
three.*nine
ten.*twenty
"

set shCMD to "grep --extended-regexp --ignore-case --recursive" & " \"" & 
_patterns & "\" " & targetFolder & " \\
| sed -En '/:/{
s!:[^:]+$!!
p
}
' \\
| sort -u"

set foundFiles to do shell script shCMD

if foundFiles ≠ "" then
  
  set foundFiles to paragraphs of foundFiles
  repeat with i in foundFiles
    set contents of i to alias POSIX file i
  end repeat
  
  tell application "Finder"
    move foundFiles to destFolder
  end tell
  
end if
-----------------------------------------------------------------------------------------

Now let's use `bbfind` instead of `grep` to send the file-path plus found text 
to a new document in BBEdit:

-------------------------------------------------------------------------------------------
set targetFolder to quoted form of "/Users/chris/test_directory/folders_nested/"
set _patterns to "one.*four|three.*nine|ten.*twenty"
set shCMD to "PATH=/usr/local/bin:$PATH;
bbfind \"" & _patterns & "\" --grep " & targetFolder & " \\
| bbedit"
do shell script shCMD
-------------------------------------------------------------------------------------------

Now let's rewrite that as a pure shell script.

It could be written as a one-liner, but I've broken it up to be easier to read 
(hopefully).

#! /usr/bin/env bash

grepPatterns="one.*four|three.*nine|ten.*twenty";

targetFolder="/Users/chris/test_directory/folders_nested/";

bbfind "$grepPatterns" --grep "$targetFolder" | bbedit

As you can see your task isn't overly difficult.

--
Best Regards,
Chris

-- 
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email
"[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to