Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by madtoperl (Hermit)
on Jan 27, 2017 at 11:14 UTC ( [id://1180464]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^2: How can I print part of a line between two different lines
by ramukavuri (Initiate) on Jan 27, 2017 at 12:07 UTC

    #!/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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1180464]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-24 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found