gpt4 book ai didi

perl - 搜索特定行的文件并存储它们

转载 作者:行者123 更新时间:2023-12-04 18:15:33 25 4
gpt4 key购买 nike

这是我的文本文件...我想搜索特定数据并存储它...
我想搜索输出需求历史然后打印它搜索所有*输出字段并仅保存其值=234并打印其数据,即abc,
dfg,
jh,

输入文件:

*output folk
.....
....
....
*output demand history
*output integ
sd,
lk,
pk,
*output field, value=234;hoxbay edt
abc,
dfg,
jh,
*output field, value=235;hoxbay edt
jh,
lk,
*output fix, value=555;deedfgh
re,
ds,
*fgh ,val=098;ghfd
dsp=pop
mike oop...


**i want this output only........**

输出:
*output field, value=234;hoxbay edt
abc,
dfg,
jh,
*output field, value=235;hoxbay edt
jh,
lk,
*output fix, value=555;deedfgh
re,
ds,

我试过这个......但我不知道如何停下来
output fix, value=555;deedfgh
re,
ds,

代码:
use strict;
use warnings;
use Data::Dumper;

open(IN , "<" , "a.txt");

my $flag=0;

foreach my $line(<IN>)
{
if($line=~/^\*output demand history/i)
{
print $line;
$flag=1;

}

if($line=~/^\*OUTPUT field/i && $flag==1)
{
print $line;
my @array1=split("," ,$line);
my $temp1=shift @array1;
my @array2=split(";",$temp1);
my $elset=shift @array2;

}

if($line=~/^\*OUTPUT FIX/i && $flag==1)
{
print $line;

my @array3=split("," ,$line);
my $temp2=shift @array3;
my @array4=split(";",$temp2);
my $nset=shift @array4;
}
}

最佳答案

很难准确说出您需要什么,但这个程序可能会有所帮助

use strict;
use warnings;

open my $fh, '<', 'a.txt' or die $!;

my @data;
while (<$fh>) {
chomp;
if (/^\*/) {
print "@data\n" if @data;
@data = ();
push @data, $1 if /^\*output\s+(?:field|fix),\s*(.+?)\s*;/;
}
else {
push @data, $_ if @data;
}
}
print "@data\n" if @data;

输出
value=234 abc, dfg, jh,
value=235 jh, lk,
value=555 re, ds,

从您的回复看来,您想要打印以 * 开头的行。并包含 value=直到以 * 开头的下一行.

试试这个代码
use strict;
use warnings;

open my $fh, '<', 'a.txt' or die $!;

my $wanted;
while (<$fh>) {
$wanted = /value/ if /^\*/;
print if $wanted;
}

输出
*output field, value=234;hoxbay edt
abc,
dfg,
jh,
*output field, value=235;hoxbay edt
jh,
lk,
*output fix, value=555;deedfgh
re,
ds,

关于perl - 搜索特定行的文件并存储它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11809907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com