Kadam has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am a beginner in perl. I need to search for a particular string in a file and then print the string in the next line.
for ex:
1st line -->125 09082010 093627.953624:1.01.00.27144.Info .CC: *GCID:0x00000013--Ingress PSP-Ri
2nd line -->206 09082010 093627.953812:1.01.00.27145.Info .CC: *GCID:0x00000013--Codec G711 (0x1) pktSize 20 attrib 0x1c00
If PSP-Ri is present in the first line then print the Codec G711 pktSize from the next line as..
Codec = G711,
pktSize =20,
attrib = 0x1c00 etc..
Can you please give some input?
  • Comment on Search for a string in a file and get the string in the next line

Replies are listed 'Best First'.
Re: Search for a string in a file and get the string in the next line
by moritz (Cardinal) on Oct 11, 2010 at 11:32 UTC
    Read the file line by line, and if one line matches your pattern, store that information in a variable. Previously to matching, you print out the current line if that same variable indicates the the previous line matched.

    See also: perlintro, Super Search

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Search for a string in a file and get the string in the next line
by cdarke (Prior) on Oct 11, 2010 at 11:53 UTC
    TMTOWTDI. This example has embedded data, to read from a file then replace DATA with a file handle, see open to open a file and obtain the filehandle:
    use warnings; use strict; while(<DATA>) { if (/PSP-Ri/) { $_ = <DATA>; # Grab the text after '--' my $pairs = (split('--'))[1]; # Remove that pesky (0x1) $pairs =~ s/\(.*?\)//; # Create a hash from (key<spaces>value) my %hash = split(/\s+/,$pairs); # Print the hash while (my($k,$v) = each(%hash)) { print "$k = $v\n" } } } __DATA__ 125 09082010 093627.953624:1.01.00.27144.Info .CC: *GCID:0x00000013--I +ngress PSP-Ri 206 09082010 093627.953812:1.01.00.27145.Info .CC: *GCID:0x00000013--C +odec G711 (0x1) pktSize 20 attrib 0x1c00
      There are two problems with reading from the file handle inside the while loop:
      1. To get robust code, you have to check if you're at the end of the file while reading from the file
      2. If you have two matching lines in a row, and both the second and the following line to be printed, you have to take special precautions

      Which is why I generally prefer the approach of using a flag

      use strict; use warnings; my $flag = 0; while (<DATA>) { if ($flag) { # print parts of $_ as necessary } $flag = /Ingress PSP/; }
      Perl 6 - links to (nearly) everything that is Perl 6.
Re: Search for a string in a file and get the string in the next line
by mjscott2702 (Pilgrim) on Oct 11, 2010 at 12:02 UTC
    Also using embedded DATA (see above). BTW, you should really show what you have tried before asking people to do this for you....

    while($line = <DATA>) { if ($line =~ m/PSP-Ri/) { $line = <DATA>; print "Codec = $1\npktSize = $2\nattrib = $3\n\n" if $line =~ m/ +Codec\s(\S+).+pktSize\s(\d+)\sattrib\s(\S+)/; } } __DATA__ 125 09082010 093627.953624:1.01.00.27144.Info .CC: *GCID:0x00000013--I +ngress PSP-Ri 206 09082010 093627.953812:1.01.00.27145.Info .CC: *GCID:0x00000013--C +odec G711 (0x1) pktSize 20 attrib 0x1c00
      Thanks for your help, its working.
      I will definitely put the things i have tried before asking next time..
Re: Search for a string in a file and get the string in the next line
by dasgar (Priest) on Oct 11, 2010 at 14:09 UTC

    Although it looks like you've found a solution, I thought I'd toss out another idea.

    You could use Tie::File. As you iterate through the array (aka lines of the file), search for the PSP-Ri string and then if it's found, print the next element (aka line of the file) or formatted portions of it as desired.

    Not advocating this to be a better approach, just a different approach.

Re: Search for a string in a file and get the string in the next line
by locked_user sundialsvc4 (Abbot) on Oct 11, 2010 at 14:50 UTC

    Maybe it’s a travesty to suggest it here, but you can also use a simpler tool like awk in cases like these.   It strictly depends on what you are trying to do, of course.   Just a sometimes-useful tool to consider.