https://zondigy.tistory.com/349
Get-Content 1.txt | %{$_ -replace "#"} > 2.txt
findstr 도 있지만 애는 행만 제거 할수 있다고 하네요. ㅠㅠ. 즉 행 자체를 통채로 날려 버림. 중간 부분을 제거 못함.
123.123.123.123 # asdasdasd
123.123.123.123 # asdasdasd
123.123.123.123 # asdasdasd
123.123.123.123 # asdasdasd
123.123.123.123 # asdasdasd
hello there # sdfsdfdfs
샘플
Get-Content 1.txt | %{$_ -replace " # [a-z]*"} > 2.txt
이렇게 하면 성공적으로 제거 가능 하다.
PowerShell -Command "& {Get-Content """"%temp%\threat\threat-100.txt"""" | %{$_ -replace ' # ',''} > """"%temp%\out444.txt""""}"
문제는 떨어져 있는 공간이 있으면 또 잘 안된다
https://kin.naver.com/qna/detail.naver?d1id=1&dirId=104&docId=432385293 전승환님이 알려 주심 ㅎㅎ 천재
PowerShell -Command "& {$FILE = Get-Content """"%temp%\threat\threat-100.txt"""";$SaveFileDir = """"%temp%\out444.txt"""";foreach ($LINE in $FILE) {foreach($Line in $LINE.Split(' # ')) {Write-Output $Line >> $SaveFileDir;break;}}}"
간단한 설명PowerShell의 정규식을 설명합니다.
새로로운걸 찾아 버렸다
Get-Content 1.txt | %{$_ -replace " # [ -~]*"} > 2.txt
Get-Content 1.txt | %{$_ -replace ' # [ -~]*'} > 2.txt
이렇게 하면 뒤에 모든 것이 삭제 된다 ㅎㅎ;; 대단.
1. 샘플 입력
2 출력 값 # 이후 모두 삭제 된걸 볼수 있다.
파워쉘로 이렇게 할수 있군요
PowerShell -Command "& {Get-Content """"%temp%\threat\threat-100.txt"""" | %{$_ -replace ' # [ -~]*',''} > """"%temp%\out444.txt""""}"
올래~! ㅋㅋ
배치파일에서는
PowerShell -Command "& {Get-Content 1.txt | %%{$_ -replace ' # [ -~]*'} | %%{$_ -replace '# Format[ -~]*'}> 2.txt}"
그다음 cmd 에서는
PowerShell -Command "& {Get-Content 1.txt | %{$_ -replace ' # [ -~]*'} | %{$_ -replace '# Format[ -~]*'}> 2.txt}"
오류가 날때 배치 파일 넣을때 이해 하기.
그리고 경로는 파워쉘 $env:temp%이 아니라 %temp%\ 경로를 써야 한다. 이유를 모른 나 같은 하수는 ㅠㅠ
https://kin.naver.com/qna/detail.naver?d1id=1&dirId=104&docId=432420058
전승환님이 추가로 알려 주심
Get-Content 1.txt | %{$_ -replace ' # [ -~]*'} | %{$_ -replace ' # [ -~]*'}> 2.txt
하지만 cmd에서는 오류가 나서 다시 수정 함
https://kin.naver.com/qna/detail.naver?d1id=1&dirId=104&docId=432467134
PowerShell -Command "& {Get-Content 1.txt | %{$_ -replace ' # [ -~]*'} | %{$_ -replace '# Format[ -~]*'}> 2.txt}"
어렵네요 결론
그냥 두번 해서 하기로 정했다 포기 ㅎㅎ;;
https://stackoverflow.com/questions/9223460/remove-empty-lines-from-text-file-with-powershell
(get-content '2.txt') -notmatch '^\s*$' > 444.txt
맨 윗줄 제거 된다
https://www.stevefenton.co.uk/blog/2020/09/remove-blank-lines-from-a-file-with-powershell/
https://www.quora.com/How-do-I-remove-blank-lines-from-a-CSV-file-using-PowerShell
Get-Content 2.txt | Where { $_.Replace(",","") -ne "" } | Out-File 3.txt
마찬가지로 맨 아래 줄은 그대로임 ㅠ
type 2.txt | findstr /rc:"[^ <tab>]" > 3.txt
빈줄 제거
하지만 맨 마지막 줄은 제거가 안된다 원래 그런가 봄 ㅎㅎ;;
https://www.dostips.com/forum/viewtopic.php?t=8263
type 2.txt | findstr /v "^$" > 4.txt
더 깔끔한 버전
https://lindevs.com/methods-to-remove-empty-lines-from-file-on-windows