Operating System - Linux
1752781 Members
6543 Online
108789 Solutions
New Discussion

Re: extracting data from string

 
SOLVED
Go to solution
Simon Hargrave
Honored Contributor

Re: extracting data from string

Who's script do you refer to? If you mean mine (the use of awk), then the input filename is after the code, eg you will see "infile" at the end of my post, that is the name of the input file. If you want the output in an output file then append >outfile at the end of the last line.
Hein van den Heuvel
Honored Contributor

Re: extracting data from string


Still not too clear.
But here is an other PERL variation.
This one takes the serial number desired as input variable.

If you cut & paste the perl below into a file called for example 'extract.p' then you execute as:

#perl extract.p Serial_number1 < all.data > extracted.data

Between my first example and this tweak you should have all possible needs covered (the second outputs start and stop... I'm sure you can figure out how)

My first example just execute as:

#perl extract.p < all.data

it will create: Serial_number1.tmp and Serial_number2.tmp


Good luck!
Hein

$serial = shift @ARGV or die "please provide serial number to select on";
while (<>) {
if (/^flag_start/) {
$save = 1;
$file = 0;
undef @lines;
}
if (/^flag_stop/) {
$save = 0;
push @lines, $_;
if ($file) {
print while ($_ = shift @lines );
}
print "\n";
}
push @lines, $_ if $save;
$file++ if /$serial/;
}
Chartier Jerome
Frequent Advisor

Re: extracting data from string

Hello all,

Thanks for your answers.
Hein, your second script seems to run .. congratulations and thanks for all.
A last question Hein, If I want to do this on all files in the same directory?
For example files starting with a patern ABC.

Thanks again

Jérôme C
J@Y
Chartier Jerome
Frequent Advisor

Re: extracting data from string

Hi All,

Thanks a lot for your help.
Is it possible to extend the second perl script to pick every file in a directory starting with a common pattern and store all the results in one file?

Thanks all again

Best Regards

Jérôme
J@Y
Muthukumar_5
Honored Contributor
Solution

Re: extracting data from string

You can execute perl script as,


perl extract.pl ABC*.log

It will check all the files with ABC*.log pattern.

You can also use this script as,

for file in `ls `
do
perl -ne '{if(/^Serial_number1/){undef @arr;next;}if(/^flag_stop/){exit;}push @arr,$_;}END{print @arr;}' $file
done

For example:

test.log test1.log test2.log

for file in `ls test*.log`
do
perl -ne '{if(/^Serial_number1/){undef @arr;next;}if(/^flag_stop/){exit;}push @arr,$_;}END{print @arr;}' $file
done > Serial_number1.tmp

hth.
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: extracting data from string

Jérôme, Good to see you are all set now.
I kanda had read the requirement for multiple input files, but wanted to leave somethign for you to do :-).

Muthukumar, I discarded a one liner along the lines you showed because it was my understanding that the 'serial number' was after an arbitrary number of data lines that would also be needed. So you need to start remembering at flag_start, not just when you see you are reading data for the right serial number.

Cheers,
Hein.