Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Hex string search all files in directory

by starface245 (Novice)
on Dec 03, 2012 at 20:48 UTC ( #1006948=perlquestion: print w/replies, xml ) Need Help??
starface245 has asked for the wisdom of the Perl Monks concerning the following question:

In my folder I have 8 text files and each of those files I want to do hex string search of "hello"
In my folder:
I would the output
test.txt match
test1.txt match
foo2.txt not match
boo3.txt match
bar4.txt match
fish5.txt not match
cat6.txt notmatch
dog7.txt match
So far, it only does output = "test.txt match" no other ouputs.
#!/usr/bin/perl use strict; my @files =glob ("*.txt"); my $hex_out = "hex.txt"; for my $file(@files) { open (HEX_IN, "$file") or die; open (HEX_OUT, ">$hex_out") or die; binmode(HEX_IN); binmode(HEX_OUT); local $/; my $hex_string = <HEX_IN>; if ($hex_string =~ /\hello/) { print HEX_OUT "$file"; print HEX_OUT " match"; } else { print HEX_OUT "$file"; print HEX_OUT " not match"; } } close(HEX_IN); close(HEX_OUT);
It has to be in hex string format. Thanks!

Replies are listed 'Best First'.
Re: Hex string search all files in directory
by LanX (Bishop) on Dec 03, 2012 at 21:03 UTC
    I think you are overwriting the results for each file, "test.txt" is just the last one in the alphabet.

    What happens if you change

    open (HEX_OUT, ">>", $hex_out") or die "$!";

    to append instead of overwriting? Or even better you move it before the loop?

    Why don't you use warnings?

    BTW: proper indentation would facilitate reading your code =)

    Cheers Rolf

    PS:why do you escape the h in /\hello/?

Re: Hex string search all files in directory
by bdalzell (Sexton) on Dec 03, 2012 at 21:22 UTC
    if you want to match all instances of "hello" wether it is "HELLO","Hello" or "hello" you should also change the line:
    if ($hex_string =~ /\hello/)
    if (lc($hex_string)) =~ /\hello/)
      The more cannonical would be, in my opinion,
      if ($hex_string =~ /hello/i)
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006948]
Approved by LanX
[1nickt]: LanX You flatter yourself. I downvote only posts that I believe do not support and promote Perl. These would include threads berating P5P volunteers and others.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-04-26 10:35 GMT
Find Nodes?
    Voting Booth?