如何在 Perl 语句中获取输入文件的名称?
文件 monday.csv
223.22;1256.4
227.08;1244.8
228.08;1244.7
229.13;1255.0
227.89;1243.2
224.77;1277.8
文件 tuesday.csv
227.02;1266.3
227.09;1234.9
225.18;1244.7
224.13;1255.3
228.59;1263.2
224.70;1247.6
这个 Perl 单行代码为我提供了第一列中前三行中第二列中具有最高值的行文件“monday.csv”中的数字为 227 或 226:
perl -F\; -ane '$hash{$_} = $F[1] if /22[78]/; END{ print and exit for sort{ $hash{$b} <=> $hash{$a} } keys %hash }' monday.csv
此 Perl 单行代码为我提供了第一列中前 3 位数字为 227 或 226 的行中第二列中具有最高值的行 * day.csv 文件:
perl -F\; -ane '$hash{$_} = $F[1] if /22[78]/; END{ print and exit for sort{ $hash{$b} <=> $hash{$a} } keys %hash }' *day.csv
我如何重写这个单行代码以获得类似的输出
filename:“文件‘filename.csv’中第一列中前 3 位数字为 227 或 226 的行中第二列中具有最高值的行”
filename :对于每个 *day.csv
, 文件?
File monday.csv
223.22;1256.4
227.08;1244.8
228.08;1244.7
229.13;1255.0
227.89;1243.2
224.77;1277.8
File tuesday.csv
227.02;1266.3
227.09;1234.9
225.18;1244.7
224.13;1255.3
228.59;1263.2
224.70;1247.6
This Perl one-liner gives me the row with the highest value in the second column from the rows where in the first column the first three digits are 227 or 226 from the file "monday.csv":
perl -F\; -ane '$hash{$_} = $F[1] if /22[78]/; END{ print and exit for sort{ $hash{$b} <=> $hash{$a} } keys %hash }' monday.csv
This Perl one-liner gives me the row with the highest value in the second column from the rows where in the first column the first 3 digits are 227 or 226 from all *day.csv files:
perl -F\; -ane '$hash{$_} = $F[1] if /22[78]/; END{ print and exit for sort{ $hash{$b} <=> $hash{$a} } keys %hash }' *day.csv
How could I rewrite this one-liner to get an output like
filename : "row with the highest value in the second column from the rows where in the first column the first 3 digits are 227 or 226 from the file 'filename.csv'"
for each *day.csv
file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
$ARGV
作为当前文件名。如果您只对最大值感兴趣,则无需存储所有值然后对它们进行排序;相反,只需存储每个文件的最大值。另外,您的正则表达式可能应该锚定到行的开头。或者,如果您想存储最大值出现的整行:
You can use
$ARGV
for the current file name. If you're only interested in the max, no need to store all the values and then sort them; instead, just store the max for each file. Also, your regex probably should be anchored to the start of the line.Or, if you want to store the entire line where the max occurs:
文件名包含在
$ARGV
变量中:然而,所提出的俏皮话有一个问题:如果第一列有重复值怎么办?
更好的一句台词是:
根据评论:
The filename is contained in the
$ARGV
variable:However, the one-liners presented have an issue; what if you have repeated values of your first column?
A better one-liner would be:
Based on the comment:
看来你可以使用$ARGV。
It seems that you can use $ARGV.