如何使用powershell获取每个文件的文件名和行数
我使用以下powershell脚本来计算给定目录中每个文件的行数:如何使用powershell获取每个文件的文件名和行数,powershell,Powershell,我使用以下powershell脚本来计算给定目录中每个文件的行数: dir -Include *.csv -Recurse | foreach{get-content $_ | measure-object -line} 这给了我以下输出: Lines Words Characters Property ----- ----- ---------- -------- 27 90 11 95 449 ... 每个文件的计数很好
dir -Include *.csv -Recurse | foreach{get-content $_ | measure-object -line}
这给了我以下输出:
Lines Words Characters Property
----- ----- ---------- --------
27
90
11
95
449
...
每个文件的计数很好(我不需要单词、字符或属性),但我不知道计数的文件名是什么
理想的输出是:
Filename Lines
-------- -----
Filename1.txt 27
Filename1.txt 90
Filename1.txt 11
Filename1.txt 95
Filename1.txt 449
...
如何将文件名添加到输出?尝试以下操作:
dir -Include *.csv -Recurse |
% { $_ | select name, @{n="lines";e={
get-content $_ |
measure-object -line |
select -expa lines }
}
} | ft -AutoSize
我可以提供另一种解决方案:
Get-ChildItem $testPath | % {
$_ | Select-Object -Property 'Name', @{
label = 'Lines'; expression = {
($_ | Get-Content).Length
}
}
}
我在手术室做手术。TXT文件,返回值如下↓
Name Lines
---- ----
1.txt 1
2.txt 2
3.txt 3
4.txt 4
5.txt 5
6.txt 6
7.txt 7
8.txt 8
9.txt 9
我之所以要这样排序,是因为我正在重写一个unixshell命令(摘自第145页的《实用程序员:精通之旅》)
此命令的目的是查找行数最多的五个文件
目前,我的进步就是上述内容,我已接近成功
但是,这个命令比unixshell命令复杂得多
我相信应该有一个更简单的方法,我正在努力找到它
find . -type f | xargs wc -l | sort -n | tail -5