Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Specific String from File - Following match

Status
Not open for further replies.

not4google

Programmer
Nov 6, 2006
15
GB
Hi all,

I have a csv file which is comma seperated and around 500mb. The string that I am searching on is _INDEX" in the file and what I need to do is find this string and then print out the full word that contains _INDEX.

The issue is that the word _INDEX may not occur in the same place on the line where it occurs else I would have accessed field x and printed that out.

An example of 2 lines is given below:

,abc,def,ghi,test_INDEX,asds,asdasd,aa,awes
,asd,def,ghi,rtg,asds,asdasd,ABty_INDEX,awe

Any suggestions would be appreciated,
 
your previous questions have several replies but you have not posted a response. Your current question makes no sense:

>> The issue is that the word _INDEX may not occur in the same place on the line where it occurs



- Kevin, perl coder unexceptional!
 
Hi,

Currently I am doing the following:

Code:
open (COMMFILE,"<$commfile") || perror ( "Could not open $commfile for input\n");
@commArray = <COMMFILE>;

#Loop through the file to get list of the allowed comm types
foreach $commElem (@commArray)
{
  @allowedCommodities = split(/,/, $commElem);
  if ($allowedCommodities[20] =~ m/_INDEX/i){
  #do something;  
  }
}

The problem with this is that I cannot rely on the required field being in the field 20. So what I need to do is match the line where string = _INDEX and then print this out bearing in mind the field could be in any position per line.

Thanks again,
 
I don't understand why you specifically check element 20.
You could check the whole line for the required text.
If match iterate through the elements to find the target
else
Ignore the line

Keith
 
Yeah, I'm a bit confused too. Using grep might be helpful?
Code:
my @results = grep {/_INDEX/} @commArray
 
Thanks for the suggestions,

The above suggestion matches the lines with _INDEX but then I need to get the word on that line (which in the following example is HELLO_TEST) and print that out,

e.g. if the line contains:

abc,def,ghi,klm,HELLO_TEST,xyz

I need to identify that this line contains _TEST which the above grep will do and then print HELLO_TEST on the comma seperated line.

I hope Im being clear about the issue here,

Thanks again,
 
The line in the source file is:

abc,def,ghi,klm,HELLO_TEST,xyz

The following identifies that this line contains _TEST in one of the fields.

Code:
grep {/_TEST/} @commArray

Once its been identified that the line contains _TEST all I need to do is print out the word on the line that contains _TEST...In this case the desired match would be:

HELLO_TEST
 
Ok, this might be more what you're looking for:
Code:
foreach my $record (@commArray) {
    my @temp = split ',', $record;
    foreach (@temp) {
        if (/_TEST/) {
            print $_, "\n";
            last;
        }
    }
}
 
Code:
foreach my $record (@commArray) {
   print "$1\n" if (/([a-zA-Z]+_TEST)/);
}

- Kevin, perl coder unexceptional!
 
After reading this thread (and taking some asprin) [upsidedown] I thought I might add that it doesn't look like there is much sense in reading the whole 500M into memory at one time. You might change that to
Code:
open (COMMFILE,"<$commfile") || perror ( "Could not open $commfile for input\n");

#Loop through the file to get list of the allowed comm types
foreach $commElem (<COMMFILE>) {
Then it will just loop through one line at a time in memory. Just a suggestion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top