나는 개인적으로 더 멀리 갈 것입니다. 나는 연속적인 연속 된 줄의 수를 계산하고 싶다. 그런 다음 파일 이름, 행 수 및 행 자체를 인쇄하십시오. 줄 수 (삭제 대상?)로 결과를 정렬 할 수 있습니다.
// int a = 10;
// int b = 20;
// DoSomething()
// SomethingAgain()
여기 내 코드입니다 : 내 코드 주석 처리 된 줄 사이에 빈 줄에 포함되지 않습니다, 그래서이 부분은 주석 코드의 두 블록으로 간주됩니다.
$Location = "c:\codeishere"
$occurences = get-ChildItem $Location *cs -recurse | select-string '//.*;'
$grouped = $occurences | group FileName
function Compute([Microsoft.PowerShell.Commands.MatchInfo[]]$lines) {
$local:lastLineNum = $null
$local:lastLine = $null
$local:blocks = @()
$local:newBlock = $null
$lines |
% {
if (!$lastLineNum) { # first line
$lastLineNum = -2 # some number so that the following if is $true (-2 and lower)
}
if ($_.LineNumber - $lastLineNum -gt 1) { #new block of commented code
if ($newBlock) { $blocks += $newBlock }
$newBlock = $null
}
else { # two consecutive lines of commented code
if (!$newBlock) {
$newBlock = '' | select File,StartLine,CountOfLines,Lines
$newBlock.File, $newBlock.StartLine, $newBlock.CountOfLines, $newBlock.Lines = $_.Filename,($_.LineNumber-1),2, @($lastLine,$_.Line)
}
else {
$newBlock.CountOfLines += 1
$newBlock.Lines += $_.Line
}
}
$lastLineNum=$_.LineNumber
$lastLine = $_.Line
}
if ($newBlock) { $blocks += $newBlock }
$blocks
}
# foreach GroupInfo objects from group cmdlet
# get Group collection and compute
$result = $grouped | % { Compute $_.Group }
#how to print
$result | % {
write-host "`nFile $($_.File), line $($_.StartLine), count of lines: $($_.CountOfLines)" -foreground Green
$_.Lines | % { write-host $_ }
}
# you may sort it by count of lines:
$result2 = $result | sort CountOfLines -desc
$result2 | % {
write-host "`nFile $($_.File), line $($_.StartLine), count of lines: $($_.CountOfLines)" -foreground Green
$_.Lines | % { write-host $_ }
}
코드를 개선하는 방법을 알고 계시면 게시하십시오! 몇 가지 표준 cmdlet을 사용하여 코드를 작성할 수 있다는 느낌이 들지만 코드가 짧아 질 수 있습니다.
'LineNumber'의 실제 정수를'LineNumber ________ etc'라는 문자가없는 변수에 저장하는 방법이 있습니까 –