Find the string using grep command from the huge data fil

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • powerfulperl
    New Member
    • Feb 2010
    • 14

    Find the string using grep command from the huge data fil

    I want to locate a string 'Local=IN' from a file and I am sure that this string is located within 100 lines(assumptio n) from the beginning of the file out of 5000 lines. The 100th line start with the word '--Begin'. So in order to locate the string from only 100 line, I think copying all the file content into any array is not a good idea.

    I want the grep command to exist in the script.

    I have the following code. I need this to be modified as per the above condition.

    Your help will be appreciated.

    Thanks


    Code:
    $fname="file.txt";
    
    open(FH,$fname);
    @file_content=<FH>;
    
    foreach (@file_content){
    
    my $match_text=grep {"Local=IN"},@file_content;
     
    Last if($match_text);
    
      else {
    
    open(WH,"temp.log");
    print WH "$\n";
    if (/Testing Option 0 ok/){
    print "Local=IN\n";
       }
    
    unlink($fname);
    rename(temp.log,"$fname");
    }


    Find the string using grep command from the huge data file
    Last edited by numberwhun; Aug 31 '10, 12:09 PM. Reason: Please Use CODE TAGS!
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You have done already 14 postings, you should know about code tags. So why don't you use them?

    You wrote "Find the string using grep command from the huge data file". Is this line copied from a homework assignment?
    Anyway, to do it this way, just use
    Code:
    my $lineFound=`head -100 file.txt | grep 'local=IN'`; # backticks!

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      @powerfulperl I fully concur with @chaarmann. This is the last time I nicely add the code tags to your posts. Next time will result in being action being taken.

      Code tags are not hard to use and are REQUIRED when posting code in the forums. You can either read the FAQ to figure out how to do it manually, or, after you input your code, highlight the code and hit the "#" symbol in the top of the editor box as it will automatically add them when you click it.

      It is not difficult and is required. Again, this is your last warning!

      Regards,

      Jeff

      Comment

      Working...