in reply to How to search an substring and eliminate before and after the substring
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.
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:use 5.014; use strict; use warnings; while (defined (my $line = <STDIN>)) { my ($id) = $line =~ /\beid\s*-?\s*(\d+)/; say $id; }
The fineprint of this invocation can be found in perlrun.perl -n -E '/\beid\s*-?\s*(\d+)/; say $1;' your_data_file
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to search an substring and eliminate before and after the substring
by Murali_Newbee (Novice) on Jul 27, 2018 at 08:02 UTC |