I have this simple script that I'm working on. I must admit, I'm totally new to PERL and kinda stuck with this stupid problem.
open(IN, "<def/t.html") or die();
while(<IN>) {
chomp;
if($_ =~ m/FF0000/) {
print "\n" . $_ . "\n";
}
}
So... I opened the t.html file and found the given string in the file. Output was ok, but I need also filename of a file in which string was found, to be printed. I really don't know how to return this, and I need it right after the $_
. Thanks for the help in advance.
That is a strange idea, but you can if you want:
# Somewhere in the code
open(F, "f.txt");
my $f = fileno(F);
# Here you want to find the filename
open(FILENAME, "ls -l /proc/$$/fd/$f|");
my @fn = split(/\s+/, <FILENAME>);
print $fn[$#fn], "\n";
perl 1.pl
Output:
/home/ic/f.txt
Here you know only the file descriptor and find the filename using it.
You can also write it much shorter with readlink
:
open(F, "f.txt");
my $f = fileno(F);
# Here you want to find the filename
print readlink("/proc/$$/fd/$f"), "\n";
I must note that the file can be already deleted (but it exists still if it is open).