简体   繁体   English

perl中的系统命令(sed)

[英]System command ( sed ) in perl

I am inside a perl input file handle loop,(while FH), and I need to perform search and delete operation to another file in the that loop.我在一个 perl 输入文件句柄循环中,(同时 FH),我需要对该循环中的另一个文件执行搜索和删除操作。

I want to perform sed operation on a file from the loop and want to capture the output of command/sed to an array variable.我想对循环中的文件执行 sed 操作,并希望将命令/sed 的 output 捕获到数组变量。

something like this:是这样的:

@flops_ = `sed '/$clkname[-2]/d' occ.txt'` 

What should be the best way to this operation?这个操作最好的方法应该是什么?

occ.txt : occ.txt

/server/home/ramneek/kings/abc_occ/flop0/Q
/server/home/ramneek/kings/abc_occ/flop1/Q
/server/home/ramneek/kings/def_occ/flop0/Q
/server/home/ramneek/kings/def_occ/flop1/Q
/server/home/ramneek/kings/xyz_occ/flop0/Q
/server/home/ramneek/kings/xyz_occ/flop1/Q

abc,def, xyz are the variables. abc、def、xyz 是变量。 In each parent loop, clkname[-2] will get these variables.在每个父循环中,clkname[-2] 将获取这些变量。 In each loop, I need to delete respective variable matching lines.在每个循环中,我需要删除相应的变量匹配行。

In 1st iteration of parent while loop, @flops_ should look like and array separated by \n charachter在父 while 循环的第一次迭代中, @flops_应该看起来像并且数组由\n字符分隔

/server/home/ramneek/kings/def_occ/flop0/Q
/server/home/ramneek/kings/def_occ/flop1/Q
/server/home/ramneek/kings/xyz_occ/flop0/Q
/server/home/ramneek/kings/xyz_occ/flop1/Q

In 2nd iteration @flops_ should be在第二次迭代@flops_应该是

/server/home/ramneek/kings/abc_occ/flop0/Q
/server/home/ramneek/kings/abc_occ/flop1/Q
/server/home/ramneek/kings/xyz_occ/flop0/Q
/server/home/ramneek/kings/xyz_occ/flop1/Q

and so on.等等。 TIA. TIA。

The best way is to do it all in perl;最好的办法是全部在perl中完成; read the lines of the file into an array once, and in your loop use grep to filter out the elements you don't want.将文件的行读入数组一次,然后在循环中使用grep过滤掉不需要的元素。

# read the lines of occ.txt into an array 
# (or use File::Slurper or Path::Tiny to do it)
open my $focc, "<", "occ.txt" or die "Couldn't open occ.txt: $!";
my @occ = <$focc>;
chomp @occ;
close $focc;

...;

while (<$fh>) {
    ...;
    # Filter out elements
    my @flops_ = grep { not /$clkname[-2]/ } @occ;
    ...;
}

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

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