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

how to Skip first few lines until you find a delimiter ">>"
  • Comment on Skip lines until you find a delimiter >>

Replies are listed 'Best First'.
Re: Skip lines until you find a delimiter >>
by tybalt89 (Monsignor) on Mar 14, 2018 at 08:02 UTC

    Guessing...

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1210866 use strict; use warnings; $/ = '>>'; <DATA>; $/ = '"'; chomp( my $variable = <DATA> ); print "variable = '$variable'\n"; __DATA__ <"Session Date:Mar 13/2017 ":>>test", 1,2,3,4 ":>>test1", 5,6,7,8 end>

    Outputs:

    variable = 'test'
      so this will skip first few lines and read from ">>"?

        Did on read the post or try the code provided?

      i want to sum all the numbers in a line.. except the first field.. could you please let me know how to do that... i am basically inserting data to 2 columns in a table.. the first field should be into 1st column and the sum of all values should be into 2nd column of my table. my data looks like below. <"Session" Attribute Date: 1/2/18, ">>:ABC",1,1.3,1,3,23, ">>:BCD",2,3,4,5,2, > So i need to load ABC to first column of table and next set of interger values to be summed and inserted to 2nd column of the table.
        Your question is still not completely clear to me. A good tool for this job is "regular expressions" or regex for short. If the regex succeeds, the left hand side assignment is defined otherwise it is not.

        As an update note: If for some reason this doesn't do exactly what you want, I expect you to spend some time studying the code and for you to make a serious attempt at modifications. I think you are doomed if you just copy code without understanding how it works.

        #!/usr/bin/perl use strict; use warnings; use List::Util qw(sum); while (<DATA>) { my ($var) = /:\>\>(\w+)/; # could have been done my (@nums) = /,\s*(\d+)/g; # on one line... next unless $var; # skips bogus lines print "$var,",sum(@nums),"\n"; } #prints: #test,10 #test1,26 __DATA__ <"Session Date:Mar 13/2017 ":>>test", 1,2,3,4 ":>>test1", 5,6,7,8 end>
Re: Skip lines until you find a delimiter >>
by Anonymous Monk on Mar 14, 2018 at 07:24 UTC

    Please show us what you've tried and give a sample of your data

      i have file with some data like below. <"Session Date:Mar 13/2017 ":>>test", 1,2,3,4 ":>>test1", 5,6,7,8 end> i want to skip first few lines until i encounter line with ":>>" and load the first value (test) to a variable..

        What did you try and in what way did it fail to meet your expectations?

        As you are new here you may not yet have read all the FAQ and maybe not even How do I post a question effectively? or Is PM a good place to get answers for homework? It's probably a good idea to have a quick read through those now.

        There would be many literal answers to your question as posed but here's one that I do not particularly recommend and is therefore presented only for amusement as it probably isn't what you wanted anyway.

        echo '<"Session Date:Mar 13/2017 ":>>test", 1,2,3,4 ":>>test1", 5,6,7,8 end>' | perl -ne '/\x3E{2}(\w+)/&& die "Variable is $1\n";'

        You'll probably want to start with perlintro and then move on to perlrun and perlretut. Enjoy.