search string in a file and print

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cckramer
    New Member
    • Dec 2008
    • 11

    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.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • cckramer
      New Member
      • Dec 2008
      • 11

      #3
      code for a simpler example

      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.

      Code:
      ## assuming below is how file1.txt looks like ## 
      ## For Indicators, please visit b.x.com. 
      ## For unix_versions, visit bee.versions.com/new.html 
      ## For file_access issues contact [email]xxx@y.com[/email]. 
      ################################################################# 
      
      #!/usr/bin/perl 
      
      $request =<STDIN>; 
      chomp($request); 
      
      $file = "/nfs/.../.../.../file1.txt"; 
      
      open (FILE1, $file) || die ("unable to open file: $!"); 
      
      @file1 = <FILE1>; 
      
      foreach $line (@file1) { 
      chomp ($line); 
      @words = split(/\s+/,$line); 
      if ($request eq $words[1]) { print "$line \n" ; } } 
      
      close <FILE1>;
      Last edited by numberwhun; Dec 10 '08, 12:38 AM. Reason: Please use code tags.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        may as well post my solution here too:

        Code:
        $request =<STDIN>;   
        chomp($request);   
          
        $file = "/nfs/.../.../.../file1.txt";   
        my @lines = ();  
        open (FILE1, $file) || die ("unable to open file: $!");   
          
        LOOP: while (<FILE1>) {  
           if (/^$request :/i) {  
              while (<FILE1>) {  
                 last LOOP if /^\w+ :/;  
                 push @lines,$_;  
              }  
           }  
        }  
        close <FILE1>;  
        print "@lines";

        Comment

        Working...