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

search string in a file and print

P: 11
Hi,

I have a file which has contents like this :

Indicators :

for issues with indicators contact a@x.com

for access to indicators contact b@x.com

Unix_Versions :

for latest unix versions check out u.versions.com/new.html

for past versions please see u.versions.com/old.html

-----------------

The keywords are indicators & unix versions

if somebody runs a perl script with any of the keywords, they should the lines corresponding to that.

Say they do, test.pl unix_versions, the 2 lines in the above file below the keyword unix_versions should be printed on screen.

---------------



how to do that ?

I think I need to read in the above file into an array.But am not sure how to go about after that.
Dec 9 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, so what have you tried thus far? Why not post your code here (encosed in code tags) and we will help you from there.

Regards,

Jeff
Dec 9 '08 #2

P: 11
Note: This could be the code if the content was one line each and the keyword was enclosed(2nd word in ech line) as part of that line.But what I need is if that key word is separate line as mentioned in the original question and there is more than one line below the keyword.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ## assuming below is how file1.txt looks like ## 
  3. ## For Indicators, please visit b.x.com. 
  4. ## For unix_versions, visit bee.versions.com/new.html 
  5. ## For file_access issues contact xxx@y.com. 
  6. ################################################################# 
  7.  
  8. #!/usr/bin/perl 
  9.  
  10. $request =<STDIN>; 
  11. chomp($request); 
  12.  
  13. $file = "/nfs/.../.../.../file1.txt"; 
  14.  
  15. open (FILE1, $file) || die ("unable to open file: $!"); 
  16.  
  17. @file1 = <FILE1>; 
  18.  
  19. foreach $line (@file1) { 
  20. chomp ($line); 
  21. @words = split(/\s+/,$line); 
  22. if ($request eq $words[1]) { print "$line \n" ; } } 
  23.  
  24. close <FILE1>;
  25.  
Dec 9 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
may as well post my solution here too:

Expand|Select|Wrap|Line Numbers
  1. $request =<STDIN>;   
  2. chomp($request);   
  3.  
  4. $file = "/nfs/.../.../.../file1.txt";   
  5. my @lines = ();  
  6. open (FILE1, $file) || die ("unable to open file: $!");   
  7.  
  8. LOOP: while (<FILE1>) {  
  9.    if (/^$request :/i) {  
  10.       while (<FILE1>) {  
  11.          last LOOP if /^\w+ :/;  
  12.          push @lines,$_;  
  13.       }  
  14.    }  
  15. }  
  16. close <FILE1>;  
  17. print "@lines";
  18.  
Dec 10 '08 #4

Post your reply

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