简体   繁体   中英

How do you use sed in Perl (part 2)?

This is a similar question to this thread How do you use sed from Perl?

My question is: I have a pipeline of sed in a csh statement such as this:

set lineNumbers= `grep "textToFind" $fileToProcess | s/.*textToFind //' | sed 's/;.*//'`

I would like to solve this issue without using s2p module such as this:

my @linenumbers;
open FH "<$fileToProcess";
while (<FH>)
{
   next if (!m/textToFind/);
   chomp;
   s/.*textToFind //, s/;.*//;
   push @lineNumbers, $_;
}    

But not sure how to include the sed pipeline in this way without using the module. I separated the second pipeline with a comma. Not sure if that is a right syntax. Any help to include both the pipeline of sed after the chomp would be appreciable.

Thank you!

What I think you are asking for is something like:

my @linenumbers;
open my $INPUT_FH, '<', $fileToProcess;
while (<$INPUT_FH>)
{
   next if (!m/textToFind/);
   push @lineNumbers, $.;
}

$. is the perl variable for the line of the current file handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM