简体   繁体   English

您如何在 Perl 中使用 sed(第 2 部分)?

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

This is a similar question to this thread How do you use sed from Perl?这是与此线程类似的问题How do you use sed from Perl?

My question is: I have a pipeline of sed in a csh statement such as this:我的问题是:我在 csh 语句中有一个 sed 管道,例如:

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

I would like to solve this issue without using s2p module such as this:我想在不使用 s2p 模块的情况下解决这个问题,例如:

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.但不确定如何在不使用模块的情况下以这种方式包含 sed 管道。 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.在 chomp 之后包括 sed 管道的任何帮助都是可观的。

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.是当前文件句柄所在行的 perl 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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