Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl shell一行程序:拾取stdin最后一行的最后一个字段_Perl - Fatal编程技术网

Perl shell一行程序:拾取stdin最后一行的最后一个字段

Perl shell一行程序:拾取stdin最后一行的最后一个字段,perl,Perl,给出如下输出: -rw-r--r-- 1 tinosino staff 4.0K 21 Mar 2012 EtcHosts_Backup_2012-03-21_21-19-10.txt -rw-r--r-- 1 tinosino staff 3.8K 1 Apr 2012 EtcHosts_Backup_2012-04-01_01-06-12.txt -rw-r--r-- 1 tinosino staff 3.9K 18 Jun 2012 EtcHosts_

给出如下输出:

-rw-r--r--   1 tinosino  staff   4.0K 21 Mar  2012 EtcHosts_Backup_2012-03-21_21-19-10.txt
-rw-r--r--   1 tinosino  staff   3.8K  1 Apr  2012 EtcHosts_Backup_2012-04-01_01-06-12.txt
-rw-r--r--   1 tinosino  staff   3.9K 18 Jun  2012 EtcHosts_Backup_2012-06-18_18-33-40.txt
-rw-r--r--   1 tinosino  staff   3.9K 27 Aug  2012 EtcHosts_Backup_2012-08-27_09-37-44.txt
我想得到最后一行(8月27日)的最后一个字段(文件名),基本上是:

EtcHosts_Backup_2012-08-27_09-37-44.txt
我感兴趣的是如何在
perl
中最好地做到这一点

如果有更好的方法只使用
ls
,这不是我想要的(我知道我可以按日期、创建、修改等进行排序,我可以
-1t
只显示文件名)

这主要是关于学习如何在
perl
中执行我在
awk
中通常执行的操作:

ll | tail -n 1 | awk '{print $NF}'
给予:

EtcHosts_Backup_2012-08-27_09-37-44.txt
这就是“想要的答案”

以下是我的尝试:

ll | perl -lane 'eof() && print $F[$#F]'
我能改进一下这件难看的。。F<代码>$F[$#F]

答案“似乎是正确的”,但获得$NF的方法似乎很原始。

改进:

perl -lane 'eof() && print $F[-1]'  # or ...
perl -lane 'eof() && print pop @F'
进一步:

perl -lane 'END { print $F[-1] }'   # or...
perl -lane 'END { print pop @F }'
当然,为什么不:

ls | tail -1

为了回答您关于改进
$F[$#F]
的具体问题,Perl支持使用负数作为数组索引。例如,-1表示列表的最后一个元素,因此您可以将一行写为:

ll | perl -lane 'eof() && print $F[-1]'
谢谢我喜欢
打印pop@F
!当然不是
ls | tail-1
,因为我说过这只是学习
perl
$NF
方法的一个例子。回答得好!