[PowerShell(pwsh)] 특정 문구가 있는 파일을 모두 찾아내기 & 해당 문구 일괄 수정하기

식빵·2023년 9월 25일
0

PowerShell

목록 보기
6/6
post-thumbnail

특정문구 있는 파일 모두 조회

Get-ChildItem | Where-Object {(get-content $_) -match 'insert into'} |
foreach {
  write-host "match! -> $($_)";
}
  • (get-content $_) 부분은 (get-content $_ -Raw) 로 작성해도 괜찮습니다.
  • -Raw 를 쓰면 file 내의 문자열을 모두 쌩(?)으로 읽는 거고,
    안 쓰면 String 배열로 읽습니다.



특정문구 있는 파일 모두 조회 + 문구수정

Get-ChildItem | Where-Object {(get-content $_) -match '조회 문구'} |
foreach {
  write-host "match! -> $($_)";
    $content = [System.IO.File]::ReadAllText($_).Replace('조회 문구','대체할 문구');
    [System.IO.File]::WriteAllText($_, $content);
}



참고링크

profile
백엔드를 계속 배우고 있는 개발자입니다 😊

0개의 댓글