In awk
如果我给出多个文件作为参数awk
,有两个特殊变量:
NR
= 对应于所有文件中所有行的行号。
FNR
=当前文件的行号。
我知道在 Perl 中,$.
对应于NR
(所有文件中的行中的当前行)。
有什么可以媲美的FNR
Perl 中的 AWK 也有吗?
假设我有一些命令行:
perl -pe 'print filename,<something special which hold the current file's line number>' *.txt
这应该给我类似的输出:
file1.txt 1
file1.txt 2
file2.txt 1
事实上,eof http://perldoc.perl.org/perlfunc.html#eof文档显示了一种执行此操作的方法:
# reset line numbering on each input file
while (<>) {
next if /^\s*#/; # skip comments
print "$.\t$_";
} continue {
close ARGV if eof; # Not eof()!
}
打印所有文件第一行的单行示例:
$ perl -ne 'print "$ARGV : $_" if $. == 1; } continue { close ARGV if eof;' *txt
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)