0

This is my first program in perl. I have more than 1000 files and I want to extract specific data from a file. The structure of all the files are same. Its really difficult to open every file and then copy a specific data,

How can I achieve this using perl.

The structure looks like this.

    LensMode=Normal
    MicronMarker=500
    DataDisplayCombine=1
    Voltage=0 Volt
    PixelSize=1.586612

I want to extract MicronMarker and PixelSize from each file. Any help in the right direction is appreciated.

the location is D:\Files\Folder1

2
  • 2
    What do you want to do with these values once you have extracted them Commented Aug 26, 2015 at 3:49
  • It doesn't look like you've made an attempt to solve this problem yourself. You'll usually get better feedback if you at least make an effort, rather than just asking people to come up with the entire solution for you. Commented Aug 26, 2015 at 15:49

1 Answer 1

2

Try this

Use glob to read the directory

while (my $files = glob(" D:\Files\Folder1\*"))
{
    open my $handler,"<","$files";
    my @extract = grep{ m/^(MicronMarker|PixelSize)/g} <$handler>;  
    print @extract;
}

Extract the word from a file using the while loop by opendir.

opendir(my $dir, " D:\Files\Folder1");

while (my $ech = readdir($dir))
{
    open my $handler,"<","test/$ech";
    while(my $l = <$handler>)
    {
        if($l =~m/^(?:MicronMarker|PixelSize)/g)
        {
            print "$l";

        }

    }
    close ($handler);
}

This is easy way to extract a words from a file using grep

while (my $ech = readdir($dir))
{
    open my $handler,"<","test/$ech";
    my @extract = grep{ m/^(MicronMarker|PixelSize)/g} <$handler>;
    print @extract;
    close($handler);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.