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

Dear Perl monks

How can I print part of a line between two different lines

Example input file contains below lines

___DATA___ sample = 123 sample_sub = info sample_char = abc sample_char = abc1 sample_char = abc2 sample_end

Replies are listed 'Best First'.
Re: How can I print part of a line between two different lines
by hippo (Archbishop) on Jan 27, 2017 at 10:17 UTC
    Kindly help me to write a perl programme in this regard.
    1. Construct a detailed specification of the problem.
    2. Design an algorithm to fit the specification.
    3. Write Perl code to implement the algorithm.
    4. Write a test suite against the specification to prove that it has been met. (Once you become more proficient at this it is advisable to write the tests before the implementation.)
    5. Document everything.

    Use strict and warnings always. Read perlintro for the basics and then the other relevant parts of the documentation. Post back here with details if/when you get stuck.

    Happy landings!

Re: How can I print part of a line between two different lines
by Discipulus (Canon) on Jan 27, 2017 at 10:31 UTC
    Hello ramukavuri and welcome to the monastery and to the wonderful world of Perl

    Now that you have edited your original post and it is readable, consider that is auspicable to present what you have already written to achieve your goal: infact many monks are not so propense (and with reason!) to answer back with complete, working code when no effort is shown by the original poster.

    That said it usually help to have a procedure in plain english and then translate it to Perl as for your grade of experience: infact is not useful have back answers you are not ready to understand.

    Looking at you data (and your title) it seems that you want to capture lines between a start one and and end one. This can be accomplished via the flip-flop operator: see range operators in perlop.

    This funny named operator is bistable: id est it acts like a switch on and off. Consider the following example:

    perl -e " foreach (1..$ARGV[0]){ print qq($_\n) if $_ == 5 .. $_==7} +" 15 5 6 7

    The if part is true after a 5 (included) is encountered and remains true until a 7 (included) is reached.

    So you can parse your data using a flip flop adjusted to your needs and for these lines strip out the unnecessary parts and print what it reamis join -ing them with the char you want.

    Remeber to use strict and use warnings to play with a safe net!

    L*

    PS: see also flip flop at my homenode

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      #!/usr/bin/perl -w open IN, "data"; while(<IN>) { Array having all the sample list (ie. 123, 124, 125) }
Re: How can I print part of a line between two different lines
by tybalt89 (Monsignor) on Jan 27, 2017 at 19:38 UTC
    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1180437 use strict; use warnings; $/ = "sample_end\n"; print join ' | ', map { join '!', /sample_char = (.*)/g } <DATA>; __DATA__ sample = 123 sample_sub = info sample_char = abc sample_char = abc1 sample_char = abc2 sample_end sample = 124 sample_sub = info sample_char = bbc sample_char = bbc1 sample_char = bbc2 sample_end sample = 125 sample_sub = info sample_char = cbc sample_char = cbc1 sample_char = cbc2 sample_end
Re: How can I print part of a line between two different lines
by tusker (Novice) on Jan 27, 2017 at 09:07 UTC
    Could you please format your post?
    It's pretty tricky to answer on line-related questions when all the text is precisely on one line. ;-)

      I formatted my post. Thank you for your concern

Re: How can I print part of a line between two different lines
by madtoperl (Hermit) on Jan 27, 2017 at 11:14 UTC
    I can give you some inputs. Also like other monks suggested, please try what you have tried so far and where you are stuck
    1. Read the line one by one from input file. You have to read more on +how to read input files in perl. 2. If the line matches word sample_char, then split it based on = and +take the second part. You have to read more on regular expression and + split functions to implement this. 3. Store the second part in an array. 4. Once all the lines are completed, display the output from array

      #!/usr/bin/perl -w

      open IN, "data";

      while($file=<IN>)

      {

      @line = split(/ \= /,$file);

      if ($line[0] =~ m/^sample$/g)

      {

      @sample = $line[1];

      foreach (@sample)

      {

      print @line; unable to proceed from here

      }

      }

      }

        #!/usr/bin/perl use strict; use warnings; my @finalArray = (); while(<DATA>){ if ($_=~/^sample_char/){ my @line = split(/\=/,$_); #push (@finalArray, $line[1]); push @finalArray, $line[1]; } } print "Final data :: @finalArray"; __DATA__ sample = 123 sample_sub = info sample_char = abc sample_char = abc1 sample_char = abc2 sample_end sample = 124 sample_sub = info sample_char = bbc sample_char = bbc1 sample_char = bbc2 sample_end sample = 125 sample_sub = info sample_char = cbc sample_char = cbc1 sample_char = cbc2 sample_end
        OUTPUT
        Final data :: abc abc1 abc2 bbc bbc1 bbc2 cbc cbc1 cbc2
Re: How can I print part of a line between two different lines
by Corion (Patriarch) on Jan 27, 2017 at 07:39 UTC

    What code have you already written and how does it fail to do what you want?