用perl,如何在每行的情况下,选择最后两行有相同的字? [英] using perl, how to select last 2 lines in the case of each row has same word?
问题描述
比尼 - -21.89753 -20.47853 -20.27835 -18.34952 -16.23454比尼 - -16.89753 -14.47853 -13.27835 -12.34952 -11.23454比尼 - -10.09014
我的文件有一个数组如上图所示。
而这个数组开始Bini的是具有多线阵,但我在这里只显示3行。
我想尝试是提取最后两行最后3个元素。
因此,-12.34952 -11.23454 -10.09014这三个要素是,我想。
有时,最后一行可以根据文件具有elments从2至5。但在这里,它已经从最后一行只有1个元素。
我试过如下
同时(我的$行=<文件>){
如果($行=〜/比尼/){#extract去年3,2,1元
我@entries =拆分(/ WS + /,$线);
$ =中element1(POP @项);
$ element2的=(POP @项);
$元素3 =(POP @项);
}
这样一来,我可以看到中element1是-10.09014,但不幸的是,我无法让元件2和3的元素。
任何人可以帮助我吗? ..
我要保持我原来的剧本。我的意思是,, creaing的日志,这是输出格式的Result.txt开放方法的过程。
块引用
块引用>#的/ usr / bin中/ perl的使用警告!;
使用严格的;
使用文件::统计;开(OUT,>中,结果\\ .TXT)或死无法打开文件\\,\\ n; #from这PLX,我想创造的Result.txt我的foreach $答案(水珠* .LOG){读取文件的格式#将日志开(FILE,<,$回答)或死无法打开文件\\ \\ n;
我@file =分(,$回答'\\');
块引用
块引用>文件引起的错误的开启方法(我@array = READ_FILE('input.txt的')
我不知道我该怎么办使用脚本从脚本我$行= 0开始。
虽然我改变了TXT的格式来记录(例如input.log),它仍然给了错误信息。
(READ_FILE'input.txt的' - 的sysopen:在text.plx 6行没有这样的文件或目录)......
解决方案您可以采取一切人数从每行,在
@entries
年底推他们,总是只保留为期。我@entries;
而(我的$行=<文件>){
如果下一行$〜/比尼/!;
推@entries中,grep / \\ D /,分流/ \\ s + /,$线;
@entries = @entries [-3 .. -1]如果@entries> 3;
}
打印加入\\ n,@entries;输出
-12.34952
-11.23454
-10.09014Bini -- -21.89753 -20.47853 -20.27835 -18.34952 -16.23454 Bini -- -16.89753 -14.47853 -13.27835 -12.34952 -11.23454 Bini -- -10.09014
my file has an array as shown above. And this array starting with Bini is array having multilines but I am showing only 3 lines here. What I wanted to try is to extract last 3 elements from the last 2 lines. So, -12.34952 -11.23454 -10.09014 these 3 elements were that I wanted. Sometimes, last line may have elments from 2 to 5 depending on the files. But here, it has only 1 elements from the last line.
What I tried was as follows
while(my $line = <FILE>) { if($line =~ /Bini/) { #extract last 3, 2, 1 element my @entries = split(/Ws+/,$line); $element1 = (pop@entries); $element2 = (pop@entries); $element3 = (pop@entries); }
As a result, I could see element1 which is -10.09014, but unfortunately, I couldn't get element 2 and element 3. Could anybody help me? ..
I want to keep my original script. I mean,, creaing process of result.txt and opening method of "log" which is output format.
Blockquote
#!/usr/bin/perl use warnings; use strict; use File::stat; open (OUT, ">", "result\.txt") or die "cannot open file\,\n"; #from this plx, I want to creat result.txt foreach my $answer (glob "*.log") { # format of reading file will be "log" open (FILE, "<", "$answer") or die "Cannot open file\.\n"; my @file = split ('\.', $answer);
Blockquote
your opening method of file induced error ( my @array = read_file('input.txt') I wonder how can I do using script starting from my $line = 0 from your script. Even though I changed format of txt to log (e.g. input.log), it still gave error message. (read_file 'input.txt' - sysopen: No such file or directory at text.plx line 6)
.....
解决方案You can take all numbers from every line, push them at the end of
@entries
, and always keep only last three.my @entries; while(my $line = <FILE>) { next if $line !~ /Bini/; push @entries, grep /\d/, split /\s+/,$line; @entries = @entries[-3 .. -1] if @entries > 3; } print join "\n", @entries;
output
-12.34952 -11.23454 -10.09014
这篇关于用perl,如何在每行的情况下,选择最后两行有相同的字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!