안녕하세요. 저는 powershell을 막 시작했으며 "ABCD #######"와 같은 큰 파일을 반복하는 powershell 스크립트를 사용하고 있습니다. "#### ";"@@ "; ##;"@@ "; ####; ####;"@ "; PowerShell에서해야 할 일은이 파일을 반복적으로 처리하는 것으로이 파일은 20,000 개가 넘는 회선을 포함 할 수 있으며 각 회선의 정보를 가져 와서 다른 파일로 출력 할 수 있습니다. 나는 그 일을하고있다. 문제는 정말로 느리고 누군가가 내 코드를 도울 수 있는지 궁금해하고 있었다.큰 파일을 반복하는 powershell
foreach ($fileName in (ls i.gft1* | %{$_.name})){
$fileNo=1
$STUFFCount=0
cd work
new-item flttemp$fileNo -type file -force
cat $fileName | %{$_.replace('"','')} > temp
foreach ($line in (cat temp)){
echo $containerCount
if ($STUFFCount -eq 999)
{
$fileNo=$fileNo+1
$STUFFCount=0
break;
new-item flttemp$fileNo -type file
}
add-content flttemp$fileNo "STUFF_START" -encoding utf8
add-content flttemp$fileNo "STUFF"-encoding utf8
$no=$line.split(";")[0]
if ($line.substring("3","1") -eq "U")
{
add-content flttemp$fileNo "STUFF_TYPE:STUFF" -encoding utf8
}
else
{
add-content flttemp$fileNo "STUFF_TYPE:STUFF" -encoding utf8
}
add-content flttemp$fileNo "STUFF_NO:$no" -encoding utf8
add-content flttemp$fileNo "STUFF_NOTO:$no" -encoding utf8
$ISO=$line.split(";")[1]
add-content flttemp$fileNo "STUFF_ISO:$ISO" -encoding utf8
$weight=$line.split(";")[5]
if ($weight -gt 0)
{
$weight=2.20462 * $weight
$weight=$weight.tostring("#.##")
add-content flttemp$fileNo "STUFF_WGT:$weight" -encoding utf8
}
else
{
add-content flttemp$fileNo "STUFF_WGT:" -encoding utf8
}
$weight=$line.split(";")[6]
if ($weight -gt 0)
{
$weight=2.20462 * $weight
$weight=$weight.tostring("#.##")
add-content flttemp$fileNo "STUFF_MWGT:$weight" -encoding utf8
}
else
{
add-content flttemp$fileNo "STUFF_MWGT:" -encoding utf8
}
add-content flttemp$fileNo "}STUFF_END" -encoding utf8
$STUFFCount=$STUFFCount+1
}
}
코드 작업 (편집 아무것도 놓치지 않은 경우)이 PowerShell을 4-5 분 정도 걸립니다 동안의이의 콘 셸 버전 1 분에 flttemp의 $의 fileNo 파일을 완료 단지 이 스크립트가 얼마나 많은 파일을 통과해야하는지에 대해서는 너무 느립니다. 내 질문은 다시, 파일을 빨리 읽을 수있는 PowerShell을 최적화하는 데 사용하지 않는 방법이 있습니까?
저는 아주 신기해서 powershell에서 $ lines을 어떻게 반복 할 것인지 모르겠습니다. – Adlis
업데이트 된 코드 확인 – Asnivor
감사합니다. 30 초 향상되었지만 필요한만큼 느려졌습니다. Powershell 나는 큰 파일들을 반복하기에는 너무 느리다. ReadAllText를 살펴보고 그걸로 뭔가 할 수 있는지 알아 보겠습니다. – Adlis