469,356 Members | 1,973 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

search string in a file and print

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
3 9859
numberwhun
3,503 Expert Mod 2GB
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
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
4,059 Expert 2GB
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.

Similar topics

10 posts views Thread by Anand Pillai | last post: by
10 posts views Thread by Bulba! | last post: by
4 posts views Thread by Tarique Jawed | last post: by
1 post views Thread by Eric | last post: by
10 posts views Thread by B. Williams | last post: by
2 posts views Thread by Ola K | last post: by
15 posts views Thread by djtosh | last post: by
1 post views Thread by nganglove | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.