Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the filename of the current file in a Perl one-liner

Tags:

perl

Is it possible to get the current filename (from a list of files) in a Perl one-liner like this below?

perl -lane 'if ($_=~/my_value/) { print $_; }' *.txt
like image 870
Toni Hermoso Pulido Avatar asked Feb 22 '15 22:02

Toni Hermoso Pulido


People also ask

How do I get the filename from the path in Perl?

use File::Basename; ... $file = basename($path); Why did $file=$path=~s/.

How do I read the first line of a file in Perl?

Note: since ->lines is returning a list, calling it without the brackets around $firstline it will be assigned the number of lines which have been read from filename. txt : 1 (or 0 if it's empty). You might want to chomp $firstline; or the value could have a newline at the end and that might not be something you want.

What is a Filehandle in Perl?

A filehandle is an internal Perl structure that associates with a file name. Perl File handling is important as it is helpful in accessing file such as text files, log files or configuration files. Perl filehandles are capable of creating, reading, opening and closing a file.

How do I print the last line of a file in Perl?

open(FILE,'file. txt'); while (<FILE>) print if eof; } close(FILE);


1 Answers

The name of the variable is $ARGV. You can find out about it in perlvar: $ARGV.

like image 138
rouzier Avatar answered Oct 21 '22 05:10

rouzier