By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,617 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,617 IT Pros & Developers. It's quick & easy.

Find the string using grep command from the huge data fil

P: 14
I want to locate a string 'Local=IN' from a file and I am sure that this string is located within 100 lines(assumption) 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


Expand|Select|Wrap|Line Numbers
  1. $fname="file.txt";
  2.  
  3. open(FH,$fname);
  4. @file_content=<FH>;
  5.  
  6. foreach (@file_content){
  7.  
  8. my $match_text=grep {"Local=IN"},@file_content;
  9.  
  10. Last if($match_text);
  11.  
  12.   else {
  13.  
  14. open(WH,"temp.log");
  15. print WH "$\n";
  16. if (/Testing Option 0 ok/){
  17. print "Local=IN\n";
  18.    }
  19.  
  20. unlink($fname);
  21. rename(temp.log,"$fname");
  22. }


Find the string using grep command from the huge data file
Aug 28 '10 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 785
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
Expand|Select|Wrap|Line Numbers
  1. my $lineFound=`head -100 file.txt | grep 'local=IN'`; # backticks! 
Aug 31 '10 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
@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
Aug 31 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.