2016-10-08 2 views
-1

현재 프로덕션 환경에서 실행 중 인시던트로 4000 개의 CSV 파일을 보관할 수있는 폴더가 있습니다.주어진 위치의 문자열을 PowerShell로 바꿉니다.

PowerShell로 내가 대체하려는 :

In all files => OK 
In all lines => OK 

위치에서 모든 문자열이 현재 문자열의 콘텐츠에 대한 고려없이 위치 끝 (18)에 9를 시작합니다. 나는 올바른 정규 표현식을 사용할 수는 있지만 할 수 없다는 의문을 품는다.

-replace 'WhatToUse','MyNewString' 

예 : (모든 파일이 2016년 8월 10일가 고정 된 문자열이 다른 문자열을 포함)

3162498;08.10.2016;30.10.2016;CHN; 

3162498;MyNewString;30.10.2016;CHN; 
+1

'+ (\ d +?); (. +?);', '$ 1; newstring;' – wOxxOm

답변

0

이 시도 될 것이라고는 (확인의 확인되는 경우)

$pathwithfiles= "C:\temp" 
    $stringtosearh="toto" 
    $stringtoreplace="titi" 
    $delimitercsv=";" 


    $listfil=gci -Path $pathwithfiles -Recurse -File -Filter "*.csv" 


    foreach ($currentfile in $listfil) 
    { 
     $contentfile=get-content $currentfile.FullName 
     [string[]][email protected]() 
     $numline=0 

     foreach ($row in $contentfile) 
     { 

     $numline++ 

      if ($numline -eq 1) 
      { 
       $contentfileres+=$row 
       continue 
      } 
      else 
      { 
      [string[]] $linearray=$row -split $delimitercsv 
      [string[]] [email protected]() 


       for ($i = 0; $i -lt $linearray.Count; $i++) 
       { 

         if ($i -ge 9 -and $i -le 18) 
         { 
         $linearrayres+=$linearray[$i].Replace($stringtosearh, $stringtoreplace) 
         } 
         else 
         { 
         $linearrayres+=$linearray[$i] 
         } 
       } 

       $contentfileres+=($linearrayres -join $delimitercsv) 

      } 

     } 

     $contentfileres | out-file ($currentfile.FullName) 

     Write-Host "File $currentfile.FullName replaced" 
    } 
+0

이 거대한 코드의 블롭은 질문과 무슨 관련이 있습니까? –

+0

이 "거대한 주석이 달린 거대한 블록"은 지목 된 모든 csv에서 9 ~ 18의 위치에 스 트린을 대신합니다. 비판을 제안하는 것이 더 좋습니까? – Esperento57

1

이온은이 같은 그것을 할 수 :

$old = "3162498;08.10.2016;30.10.2016;CHN;" 
$new = $old.remove(8,10).insert(8,"MyNewString") 

이 될 것입니다 : 3,162,498; MyNewString, 2016년 10월 30일, CHN를;

관련 문제