Perl File Line Number

In awk , if I give more than one file as an awk argument, there are two special variables:

NR = line number corresponding to all lines in all files.

FNR = line number of the current file.

I know that in Perl $. matches NR (current line among lines in all files).

Is there anything comparable to Perl's FNR AWK?

Let's say I have some command line:

 perl -pe 'print filename,<something special which hold the current file line number>' *.txt 

This should give me a conclusion, for example:

 file1.txt 1 file1.txt 2 file2.txt 1 
+7
source share
2 answers

There is no such variable in Perl. But you have to learn eof to write something like

 perl -ne 'print join ":", $. + $sum, $., "\n"; $sum += $., $.=0 if eof;' *txt 
+2
source

Actually, the eof documentation shows a way to do this:

 # reset line numbering on each input file while (<>) { next if /^\s*#/; # skip comments print "$.\t$_"; } continue { close ARGV if eof; # Not eof()! } 

An example of a single-line font that prints the first line of all files:

 $ perl -ne 'print "$ARGV : $_" if $. == 1; } continue { close ARGV if eof;' *txt 
+6
source

All Articles