I am attempting to search each line of each file in a directory for a specific string, and if found, grab a pertinent piece of data in the same line and then search for all the instances of that data and put them into another file.
The files I'm searching are delimited by the pipe ("|") character. I am attempting to use split with the pipe as the delimiter but it seems to return each character into my array instead of each substring between pipes as if the delimiter is undefined. I am relatively new to PERL so I may be missing something quite obvious. My code is below...Any ideas?
#string to search for
my($target1) = '6169600047';
#files and directories
my($indir) = "c:/FAMD_EDI";
my($outdir) = "c:/FAMD_EDI";
my($outfile) = "FAMD_EDI_Master.txt";
open(OUTFILE,">$outdir/$outfile") or die "Cannot create output File";
opendir(MYDIR, $indir) or die "can't open dir $indir: $!";
while (defined($infile = readdir(MYDIR)))
next if $infile =~ /^\.\.?$/;
open(FILEHNDL, "$indir/$infile") or die "Cannot open input file $!"
while ($line = <FILEHNDL>)
#print "line= $line\n";
@cols = split(/|/, $line);
if ($cols = $target1)
Are you posting in the right place? Check out Where do I post X? to know for sure.
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
Want more info? How to link or
or How to display code and escape characters
are good places to start.