user1568209
user1568209

Reputation: 44

Search file for specific lines and store them

This is my text file...i want to search specific data and store it.... i want to search output demand history and then print it search all *output field and save its value=234 only and print its data i.e abc, dfg, jh,

input file:

*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:

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

I have tried this.....but i don't know how to stop after

output fix, value=555;deedfgh
re,
ds,

code:

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;
  }
}

Upvotes: 2

Views: 133

Answers (4)

Borodin
Borodin

Reputation: 126742

It is hard to tell exactly what you need, but this program may help

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;

output

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

From your responses it looks like you want to print lines beginning with a * and containing value= up to the next line starting with a *.

Try this code

use strict;
use warnings;

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

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

output

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

Upvotes: 1

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26131

One version using flip-flop:

perl -ne'print if (/^\*output .*value=/ .. ($a = (/^\*/ && ! /value=/))) && ! $a'

Upvotes: 1

cdtits
cdtits

Reputation: 1128

Maybe it's you want:

use 5.010;
$flag;
while (<IN>) {
    given ($_) {
        when (/^\*output/)  { $flag= 0; continue; }
        when (/value/)      { $flag = 1; }
    }
    print if $flag;
}

Upvotes: 1

Shmuel Fomberg
Shmuel Fomberg

Reputation: 546

I don't see where you simply print the inputted lines, when all the conditions are met.

you need somewhere in the loop:

if ($flag2) {
   print $line;
}

Upvotes: 1

Related Questions