Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to read the regular expression from another file?

by sumathigokul (Acolyte)
on May 07, 2015 at 09:09 UTC ( #1125976=perlquestion: print w/replies, xml ) Need Help??

sumathigokul has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

Before asking my question, i have to say this....Perlmonks is really a great resource for the beginner in perl, i am really thank full to all for your kind replies.

I have a file "file1" with three lines as follows

c_c b_c a_c

I have another file "file2" with following lines

enable_pad : INBUF port map (PAD => enable, Y => enable_c); c_pad : INBUF port map(PAD => c, Y => c_c); b_pad : INBUF port map(PAD => b, Y => b_c); sum_pad : OUTBUF port map(D => sum_c, PAD => sum); VCC_i_0 : VCC port map(Y => VCC_0); VCC_i : VCC port map(Y => \VCC\); Sum_O : XOR3 port map(A => b_c, B => a_c, C => enableandc, Y => sum_c) +; carry_pad : OUTBUF port map(D => N_5, PAD => carry); a_pad : INBUF port map(PAD => a, Y => a_c); GND_i_0 : GND port map(Y => GND_0); GND_i : GND port map(Y => \GND\); Carry_O : MAJ3 port map(A => a_c, B => enableandc, C => b_c, Y => N +_5); modify : XOR2 port map (A=> c_c, B=> enable_c, Y => enableandc);

i want a perl script that reads file2 and print the lines which has any of the words listed in file1 (i.e. either a_c or b_c or c_c) and save those lines in new file.

My perl code for this is as follows

use strict; use warnings; open (F1, "<new4.txt") or die; open (F2, ">new5.txt") or die; while (<F1>) { open (F3, "<new3.txt") or die; my $lines = <F3>; print F2 if (/$lines/); close (F3); } close (F1); close (F2);

But, its not copying any of the matched lines in new5.txt file.

Please correct the mistake....Thank you all once again...

Replies are listed 'Best First'.
Re: How to read the regular expression from another file?
by hippo (Chancellor) on May 07, 2015 at 09:57 UTC
    i want a perl script that reads file2 and print the lines which has any of the words listed in file1 (i.e. either a_c or b_c or c_c) and save those lines in new file.

    Just on the off-chance that this isn't just a programming exercise it is worth pointing out that grep can do this already:

    grep -f file1 file2 > newfile

      You beat me to the punch. If this is on a *nix machine, grep is FAR cleaner.

      --
      “For the Present is the point at which time touches eternity.” - CS Lewis
Re: How to read the regular expression from another file?
by pme (Prior) on May 07, 2015 at 09:37 UTC
    Hi sumathigokul,
    my @pattern = <STDIN>; # slurp patterns chomp @pattern; # remove end-of-line my $pattern = join '|', @pattern; # create regexp open F2, "<file2" or die "Cannot open file: $!\n": while (<F2>) { # read DATA print if /$pattern/; } close F2;
    You can run this little script like this:
    $ ./myscript.pl <file1

      In the spirit of TIMTOWDI, lines 6-8 could also be replaced by:

      print grep { /$pattern/ } <F2>;

      --MidLifeXis

      Hi pme,

      Thank you so much, its working now....

Re: How to read the regular expression from another file?
by Anonymous Monk on May 07, 2015 at 09:15 UTC

    Please correct the mistake....Thank you all once again...

    You can do it :) see Basic debugging checklist and use Data::Dump::dd() to ddumper your "data" at certain points

    Also you should give variables meaningful names, F1/F2... aren't descriptive like $infh, $outfh, $cashfh...

Re: How to read the regular expression from another file?
by Laurent_R (Canon) on May 07, 2015 at 17:46 UTC
    Hi, you have a working solution by now, but just a few comments on some mistakes in your code, to help you progressing:
    while (<F1>) { open (F3, "<new3.txt") or die; my $lines = <F3>; print F2 if (/$lines/); close (F3); }

    First it is a very bad idea to open the same file repeatedly within a while loop. This is very inefficient. Whenever you have something like that, read the file with the elements to be found prior to the main loop and store these items in memory (an array, a hash, a regex, a string, something, whatever).

    Second, when you read F3, you need to chomp the line to remove the trailing end of line. Else you will be looking for "c_c\n", not "c_c", so that you won't find it.

    Third, this code line:

    my $lines = <F3>;
    is reading only one line from the file (the first one). So that if the other errors were corrected, you would still be able to catch only the first intemof the file.

    Finally, this is not an error, your code will work on that, but it is considered better practice to use the so-called three-argument syntax for opening a file and to use lexical file handles:

    open my $SEARCH_ITEMS, "<", "new3.txt") or die "Could not open new +3.txt $!";; my @lines = <$SEARCH_ITEMS>;
    Note that I also added a message to the die statement: 1. saying what the problem is ("Could not open..."), 2. telling which file the script could not open, and 3. displaying the error message reported by the OS (the $! special variable). Believe me, this helps when you have scripts opening dozens of files.

    I hope this helps.

    Update: Corrected a couple of typos and added a little bit of further explanations.

    Je suis Charlie.
Re: How to read the regular expression from another file?
by vinoth.ree (Monsignor) on May 07, 2015 at 10:18 UTC
    Sorry, Wrongly understood the question.

    Another approach is read the file2 content and split each line based on ':', and save in a hash, key will be 'enable_pad','c_pad','b_pad',etc and other string as value.

    Read file1 and check each line exists in hash or not, if exists print it.

    use strict; use warnings; open my $FILE2, "<", "file2.txt" or die "Cannot open file2 file: $!"; open my $FILE1, "<", "file1.txt" or die "Cannot open file1 file: $!"; my %hash; while (my $eachline = <$FILE2>) { chomp($eachline); my($key,$value)=split(':',$eachline,2); $hash{$key}=$value; } while (my $line = <$FILE>) { chomp($line) if(exists $hash{$line}) { print $line:$hash{$line} } else{print "$line does not exist in file2"} }

    All is well. I learn by answering your questions...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1125976]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2020-11-30 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?