blu.org  wiki

grep, maybe

View: New views
7 Messages — Rating Filter:   Alert me  

grep, maybe

by Maurice-10 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Looking for some guidance;

I have several files within several folders (5 files per folder, and
thousands of folders) that I need to search a text file within each
folder for a word match (like three_little_pigs.txt, and I need to find
"moe", if he's listed) and then when a match is found I need to move
(not copy) that entire folder (and it's 3~5 files contained within) to
another location...

I'm thinking grep, but don't know the correct syntax to make all this
happen.
I can easily find all the folders (1949 of them) and the word match 3923
times within the text file(s)...


Any ideas???


--
-Maurice Pelletier
Child Development Services - Cumberland County
50 Depot Road
Falmouth, ME 04105
207-781-8881 (voice)
207-781-8855 (fax)

www.cds-cumberland.org


"Linux -- it's not just for breakfast anymore..."
-Moe



_______________________________________________
Discuss mailing list
Discuss@...
http://lists.blu.org/mailman/listinfo/discuss

Re: grep, maybe

by David Hummel-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 29, 2009 at 11:38 AM, Maurice <mauricep@...> wrote:
>
> I have several files within several folders (5 files per folder, and
> thousands of folders) that I need to search a text file within each
> folder for a word match (like three_little_pigs.txt, and I need to find
> "moe", if he's listed) and then when a match is found I need to move
> (not copy) that entire folder (and it's 3~5 files contained within) to
> another location...

You'll want to script this with something like:

pig=moe
dest=/some/dir
for d in $*; do
  grep -q $pig $d/*.txt && mv $d $dest
done
_______________________________________________
Discuss mailing list
Discuss@...
http://lists.blu.org/mailman/listinfo/discuss

Re: grep, maybe

by John Abreau-18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Assuming no spaces or other troublesome metacharacters
in the filenames and folder names:

    mv $( find /path/to/src/root -type f | xargs grep -l moe |
        sed 's-/[^/]*$--' | sort -u ) /path/to/destination/

The example should all be on one line, of course.




On Thu, Oct 29, 2009 at 11:38 AM, Maurice <mauricep@...> wrote:

> Looking for some guidance;
>
> I have several files within several folders (5 files per folder, and
> thousands of folders) that I need to search a text file within each
> folder for a word match (like three_little_pigs.txt, and I need to find
> "moe", if he's listed) and then when a match is found I need to move
> (not copy) that entire folder (and it's 3~5 files contained within) to
> another location...
>
> I'm thinking grep, but don't know the correct syntax to make all this
> happen.
> I can easily find all the folders (1949 of them) and the word match 3923
> times within the text file(s)...
>
>
> Any ideas???
>
>
> --
> -Maurice Pelletier
> Child Development Services - Cumberland County
> 50 Depot Road
> Falmouth, ME 04105
> 207-781-8881 (voice)
> 207-781-8855 (fax)
>
> www.cds-cumberland.org
>
>
> "Linux -- it's not just for breakfast anymore..."
> -Moe
>
>
>
> _______________________________________________
> Discuss mailing list
> Discuss@...
> http://lists.blu.org/mailman/listinfo/discuss
>



--
John Abreau / Executive Director, Boston Linux & Unix
AIM abreauj / JABBER jabr@... / YAHOO abreauj / SKYPE zusa_it_mgr
Email jabr@... / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99
_______________________________________________
Discuss mailing list
Discuss@...
http://lists.blu.org/mailman/listinfo/discuss

Re: grep, maybe

by Dan Ritter-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 29, 2009 at 11:38:03AM -0400, Maurice wrote:

> Looking for some guidance;
>
> I have several files within several folders (5 files per folder, and
> thousands of folders) that I need to search a text file within each
> folder for a word match (like three_little_pigs.txt, and I need to find
> "moe", if he's listed) and then when a match is found I need to move
> (not copy) that entire folder (and it's 3~5 files contained within) to
> another location...
>
> I'm thinking grep, but don't know the correct syntax to make all this
> happen.
> I can easily find all the folders (1949 of them) and the word match 3923
> times within the text file(s)...


grep -lr STRING | xargs -n1 -I QZQ mv QZQ /path/to/destination/

grep -l means print the filenames where a match is found
     -r means recursively

xargs -n1 means supply one argument per command
      -I QZQ means replace QZQ with the argument

untested, but probably close.


--
http://tao.merseine.nu/~dsr/eula.html is hereby incorporated by reference.
You can't defend freedom by getting rid of it.
_______________________________________________
Discuss mailing list
Discuss@...
http://lists.blu.org/mailman/listinfo/discuss

Re: grep, maybe

by darose :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10/29/2009 11:47 AM, John Abreau wrote:
> Assuming no spaces or other troublesome metacharacters
> in the filenames and folder names:
>
>     mv $( find /path/to/src/root -type f | xargs grep -l moe |
>         sed 's-/[^/]*$--' | sort -u ) /path/to/destination/

If there are spaces, then change it to:

... find /path/to/src/root -type f -print0 | xargs -0 grep -l moe ...

DR
_______________________________________________
Discuss mailing list
Discuss@...
http://lists.blu.org/mailman/listinfo/discuss

Re: grep, maybe

by John Abreau-18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Actually, this will fail due to the "mv" command line being too long.
However, the part between the $( ... ) gives you a list of all folders
to be moved.

In case there are nested folders, the "sort -u" should be changed
to "sort -ru" to reverse-sort the list, so inner folders get moved
before their parent is moved.



On Thu, Oct 29, 2009 at 11:47 AM, John Abreau <jabr@...> wrote:

> Assuming no spaces or other troublesome metacharacters
> in the filenames and folder names:
>
>    mv $( find /path/to/src/root -type f | xargs grep -l moe |
>        sed 's-/[^/]*$--' | sort -u ) /path/to/destination/
>
> The example should all be on one line, of course.
>
>
>
>
> On Thu, Oct 29, 2009 at 11:38 AM, Maurice <mauricep@...> wrote:
>> Looking for some guidance;
>>
>> I have several files within several folders (5 files per folder, and
>> thousands of folders) that I need to search a text file within each
>> folder for a word match (like three_little_pigs.txt, and I need to find
>> "moe", if he's listed) and then when a match is found I need to move
>> (not copy) that entire folder (and it's 3~5 files contained within) to
>> another location...
>>
>> I'm thinking grep, but don't know the correct syntax to make all this
>> happen.
>> I can easily find all the folders (1949 of them) and the word match 3923
>> times within the text file(s)...
>>
>>
>> Any ideas???
>>
>>
>> --
>> -Maurice Pelletier
>> Child Development Services - Cumberland County
>> 50 Depot Road
>> Falmouth, ME 04105
>> 207-781-8881 (voice)
>> 207-781-8855 (fax)
>>
>> www.cds-cumberland.org
>>
>>
>> "Linux -- it's not just for breakfast anymore..."
>> -Moe
>>
>>
>>
>> _______________________________________________
>> Discuss mailing list
>> Discuss@...
>> http://lists.blu.org/mailman/listinfo/discuss
>>
>
>
>
> --
> John Abreau / Executive Director, Boston Linux & Unix
> AIM abreauj / JABBER jabr@... / YAHOO abreauj / SKYPE zusa_it_mgr
> Email jabr@... / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
> PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99
>



--
John Abreau / Executive Director, Boston Linux & Unix
AIM abreauj / JABBER jabr@... / YAHOO abreauj / SKYPE zusa_it_mgr
Email jabr@... / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99

_______________________________________________
Discuss mailing list
Discuss@...
http://lists.blu.org/mailman/listinfo/discuss

Re: grep, maybe

by John Abreau-18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The move can be done via xargs.

    find ... | xargs grep -l ... | sed ... | xargs -I '{}' mv '{}'
/path/to/destination/



On Thu, Oct 29, 2009 at 11:56 AM, John Abreau <jabr@...> wrote:

> Actually, this will fail due to the "mv" command line being too long.
> However, the part between the $( ... ) gives you a list of all folders
> to be moved.
>
> In case there are nested folders, the "sort -u" should be changed
> to "sort -ru" to reverse-sort the list, so inner folders get moved
> before their parent is moved.
>
>
>
> On Thu, Oct 29, 2009 at 11:47 AM, John Abreau <jabr@...> wrote:
>> Assuming no spaces or other troublesome metacharacters
>> in the filenames and folder names:
>>
>>    mv $( find /path/to/src/root -type f | xargs grep -l moe |
>>        sed 's-/[^/]*$--' | sort -u ) /path/to/destination/
>>
>> The example should all be on one line, of course.
>>
>>
>>
>>
>> On Thu, Oct 29, 2009 at 11:38 AM, Maurice <mauricep@...> wrote:
>>> Looking for some guidance;
>>>
>>> I have several files within several folders (5 files per folder, and
>>> thousands of folders) that I need to search a text file within each
>>> folder for a word match (like three_little_pigs.txt, and I need to find
>>> "moe", if he's listed) and then when a match is found I need to move
>>> (not copy) that entire folder (and it's 3~5 files contained within) to
>>> another location...
>>>
>>> I'm thinking grep, but don't know the correct syntax to make all this
>>> happen.
>>> I can easily find all the folders (1949 of them) and the word match 3923
>>> times within the text file(s)...
>>>
>>>
>>> Any ideas???
>>>
>>>
>>> --
>>> -Maurice Pelletier
>>> Child Development Services - Cumberland County
>>> 50 Depot Road
>>> Falmouth, ME 04105
>>> 207-781-8881 (voice)
>>> 207-781-8855 (fax)
>>>
>>> www.cds-cumberland.org
>>>
>>>
>>> "Linux -- it's not just for breakfast anymore..."
>>> -Moe
>>>
>>>
>>>
>>> _______________________________________________
>>> Discuss mailing list
>>> Discuss@...
>>> http://lists.blu.org/mailman/listinfo/discuss
>>>
>>
>>
>>
>> --
>> John Abreau / Executive Director, Boston Linux & Unix
>> AIM abreauj / JABBER jabr@... / YAHOO abreauj / SKYPE zusa_it_mgr
>> Email jabr@... / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
>> PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99
>>
>
>
>
> --
> John Abreau / Executive Director, Boston Linux & Unix
> AIM abreauj / JABBER jabr@... / YAHOO abreauj / SKYPE zusa_it_mgr
> Email jabr@... / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
> PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99
>



--
John Abreau / Executive Director, Boston Linux & Unix
AIM abreauj / JABBER jabr@... / YAHOO abreauj / SKYPE zusa_it_mgr
Email jabr@... / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99

_______________________________________________
Discuss mailing list
Discuss@...
http://lists.blu.org/mailman/listinfo/discuss