CAFE

자유 게시판

윈도우 문자 제외 파워쉘 주석 제거 & 빈줄 제거

작성자동우|작성시간22.11.11|조회수571 목록 댓글 2

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://hooeon.tistory.com/entry/Powershell%EC%97%90%EC%84%9C%EC%9D%98-%EB%AC%B8%EC%9E%90%EC%97%B4-%EB%8B%A4%EB%A3%A8%EA%B8%B0-%EA%B8%B0%EC%B4%88   

 

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의 정규식을 설명합니다.

https://learn.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7.3    

 

새로로운걸 찾아 버렸다 

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  

마찬가지로 맨 아래 줄은 그대로임 ㅠ 

 

 

 


https://gkstamin.tistory.com/entry/Windows-%ED%85%8D%EC%8A%A4%ED%8A%B8%ED%8C%8C%EC%9D%BC-%EB%B9%88%EC%A4%84-%EC%A0%9C%EA%B1%B0-CMD   

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  

다음검색
현재 게시글 추가 기능 열기

댓글

댓글 리스트
  • 작성자동우 작성자 본인 여부 작성자 | 작성시간 22.11.11 PowerShell -Command "& {Get-Content """"%temp%\threat\threat-100.txt"""" | %{$_ -replace '#','REM'} > """"%temp%\out444.txt""""}"
    맨위에만 있으면 이렇게 해도 되긴 하는대 문제는 ㅋㅋ 중간에 있어서
  • 작성자동우 작성자 본인 여부 작성자 | 작성시간 22.11.11 PowerShell 명령어 문자열 모두 제거
댓글 전체보기
맨위로

카페 검색

카페 검색어 입력폼