Your "sample find results" don't seem to be consistent with the command line you use to create them. The output of a pipeline like this:
find path -type f -print0 | xargs -0 grep -i -n pattern
should be a set of zero or more lines lines like this:
path/filename:line#:content of line containing pattern path/subdir/filename:line#:another line with pattern in it
(Note the addition of "-print0" on the find command, and "-0" on xargs; someday those will save you a lot of grief, e.g. when you have file names containing spaces or other shell-magic characters -- BTW, it's possible to create a file on a unix box with line-feed and/or carriage-return characters in the file name; I've seen it happen.)

But none of your sample results would match those templates. Anyway, given the command line that you are using, and the presence of some files with the pattern ":\d+:" as part of the file name (and there may be some grepped lines from data files that also contain matches for ":\d+:"), I don't think you want to use "xargs grep -n" that way -- the results cannot be parsed reliably.

Take the time to let perl do the grepping on the files:

#!/usr/bin/perl use strict; # use Data::Dumper; # you might want this die "Usage: $0 search_path search_pattern\n" unless ( @ARGV == 2 and -d $ARGV[0] ); my ( $path, $pattern ) = @ARGV; my @filelist; open( my $find, "-|", "find $path -type f -depth -print0" ) or die "Unable to run 'find $path ...'\n"; { local $/ = chr(0); # set input record separator to null byte @filelist = <$find>; chomp @filelist; # remove null byte terminations } close $find; my %found; for my $filename ( @filelist ) { # $. = 0; # (update: this line is not needed) open( my $fh, $filename ); while (<$fh>) { $found{$filename}{$.} = $_ if ( /$pattern/ ); } close $fh; } # check out how the data is stored if you want: # print Dumper( \%found ); # or pretty-print it: for my $file ( sort keys %found ) { # parse $file into directory and filename if you want for my $line ( sort {$a<=>$b} keys %{$found{$file}} ) { printf( "File <<%s>> LineNo <<%d>> matches: %s", $file, $line, $found{$file}{$line} ); } }
(update: removed some misleading stuff from one of the "die" messages)

There are two features that result from using the search_pattern string within perl, and you'll probably like them:

  1. You don't need to worry about properly quoting the match string in order to make it work in the "xargs grep ..." shell command; having things like spaces, angle-brackets, etc, in the search pattern will be safe.
  2. You can leverage the extra power of perl regular expressions -- they provide some special tricks you don't get with a standard "grep" shell command. (In using the above script, some regex patterns would need to be quoted on the command line when the script is run, in order to get past shell interpretation and directly into @ARGV.)

In reply to Re: RegEx to get file name from find results by graff
in thread RegEx to get file name from find results by vsailas

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.