The Unix way is to use find. (Though, of course, you're allowed to drop the backslashes and have it all on one line.) (It's a lot easier if the directory containing the files has no subdirectories.)

find $DIR \ -exec egrep 'N+' {} > /dev/null\; -o \ -exec cp {} $otherDir\;

1 - Run the find command, starting the search at the required directory

3 - After the nodes above, everyone knows how to find the files which don't contain NNNN. Instead of feeding the names to xargs, we can also implement the action in find>. The return value of egrep is true if the pattern was found, false if it wasn't found, regardless of whether options reverse the send of the search.

3b - If the pattern matches the file, we want to stop there. That's what the -o option does. We only continue further if the pattern did NOT match anywhere in the file.

4 - Copy the file to the other directory.

Note: -exec takes an ordinary command with curly braces where you would have the name of the file, and an escaped semi-colon to mark the end of the -exec.

More Notes: If the directory has subdirectories, and you don't want to descend into them, it becomes more complicated. You want to -prune directories (-type d), but that stops the search with the first thing found, unless you search depth first (-deepth>) .


In reply to Re: grep or perl by TomDLux
in thread grep or perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.