Hello Murali_Newbee,

Being very new to Perl has happened to all of us at some time in the past, so welcome to the journey of learning it!

With Perl, you would usually not remove the parts before and after the id, but simply grab the id from every line. Grabbing interesting stuff is done with "capturing" it by using regular expressions - the starting point to read would be the tutorial at perlretut.

I highly recommend reading this tutorial because there might be some misinterpretation of your requirement in my suggestion. Save the following code in a file, say test.pl and run it with perl test.pl <your_input_file.

use 5.014; use strict; use warnings; while (defined (my $line = <STDIN>)) { my ($id) = $line =~ /\beid\s*-?\s*(\d+)/; say $id; }
Over time, if you get more familiar with Perl, you'll learn a lot of things how this could be made more compact, and in fact, this is one of the problems which can be pretty well solved with a one-liner:
perl -n -E '/\beid\s*-?\s*(\d+)/; say $1;' your_data_file
The fineprint of this invocation can be found in perlrun.

In reply to Re: How to search an substring and eliminate before and after the substring by haj
in thread How to search an substring and eliminate before and after the substring by Murali_Newbee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.