in reply to Re: grep fields
in thread grep fields
Is it ever possible that the email address you're searching for could occur in some other field but not in the 4th field? (I would assume that if "target@email.address" is in field 6 and not in field 4, then it's not a "goodrec".)
If the records containing the target string always have that string in field 4, then you don't have to split:
But if that condition does not hold for your data, then split is the best approach, as others have suggested. And in that regard, if you're actually looking for one specific string at a time (as opposed to, say, all email strings that have digits before the "@"), then it may be better not to use a regex:@goodrecs = grep /:target\@email\.address:/, @recs; # (note the delimiters are part of the match)
This avoids getting "bigjoe@hotmail.com" and "dumbjoe@hotmail.com" when you were looking for just "joe@hotmail.com", in case that matters to you.@goodrecs = grep { @f=split/:/; $f[4] eq $target } @recs;
(update: Maybe the latter snippet should say $f[3], if "field 4" is based on the first field being "field 1".)
|
|---|