Naidu
Naidu

Reputation: 149

How do I parse or print data after matching a particular line in Perl?

i have file with dat as below

      extern const
      extern const

     /*****************************************************************************
      *                Data                                         *
      *****************************************************************************/
     #ifdef SELECTED
     /*****************************************************************************
      *                  comments                                         *
      *****************************************************************************/

      int test_data1_value;
      int test_data2_value;
      int test_data3_value;
      int test_data4_value;


     #endif
      random data
      random data

    /**********************************************************************
    *                       definitions                     *
    **********************************************************************/
    #define
    #define

i need to extract the data line starting with "uint8" between to comments "test data buffer" and "receive signal definations"
how can this be done i have written a script like

   while (<DATA>) {
   chomp;
   if (/^\*\s* Data / ... /^\*\s*definitions/) {
    if (m/^\s*int/) {
       print "$_";

         }
      }
     }

i need out put to be as

      int test_data1_value;
      int test_data2_value;
      int test_data3_value;
      int test_data4_value;

after matching particular comment string it should check for line starting with int and print that whole line . if there is no int at the beginning of line it should exclude that particular line . but this code is not working for me

Upvotes: 0

Views: 109

Answers (1)

Lee Duhem
Lee Duhem

Reputation: 15121

I do not know what you want to do with those data between #ifdef and #endif, but looks like how to filter those data is your main problem, so here is how to do it by using operator ..., which can be used to indicate a line range, exactly what we need:

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
    chomp;
    if (/^\s*\*\s*test Data Buffer/ ... /^\s*\*\s*Receive signal definitions/) {
        if (m/^\s*uint8/) {
            print "$_\n";
        }
    }
}

__DATA__
      random data

     #ifdef CAN_CH0_SELECTED
     /*****************************************************************************
      *                  test Data Buffer                                         *
      *****************************************************************************/
      uint8 test_data1_value;
      uint8 test_data2_value;
      uint8 test_data3_value;
      uint8 test_data4_value;


     #endif
      random data
      random data

     /*****************************************************************************
      *                  test Data Buffer                                         *
      *****************************************************************************/

Upvotes: 2

Related Questions