2013-11-22 2 views
0

우선 : Windows 7 시스템을 사용하고 있습니다.).텍스트 파일에서 행을 필터링하는 일괄 처리 스크립트

수십 개의 파일이있는 폴더가 있습니다. 각 파일에는 약 240.000 개의 행이 들어 있습니다. 그러나 그 행의 절반 만 필요합니다.

나는 무엇을하고 싶습니다 : 이러한 파일을 통해 실행되는 스크립트를 "abcd"문자열이 들어있는 모든 행을 필터링하고 새 디렉터리에 저장하거나 그냥 같은 파일에 저장하도록합니다. 백업 파일 .bak을,

$currentPath = "the path these files currently in" 
$newPath  = "the path you want to put the new files" 

$files = Get-ChildItem $currentPath 

foreach ($item in $files) { 
    Get-Content $item | Where-Object {$_ -notmatch 'abcd'} |Set-Content $newPath\$item 
} 
+0

당신이 배치 파일을 원하십니까 또는 powershell 솔루션? – foxidrive

+0

글쎄, 나는 정직하게 생각하지 않는다. :) 작동하는 한은 – user2428207

+0

모든 파일을 하나의 파일로 수집하거나 별도의 파일로 모든 'abcd'라인을 원합니 까? –

답변

0
@echo off 
    setlocal enableextensions 

    set "_where=c:\some\where\*.txt" 
    set "_filter=abcd" 

rem find files which needs filter 
for /f "tokens=*" %%f in ('findstr /m "%_filter%" "%_where%"') do (

    rem generate a temporary file with the valid content 
    findstr /v /c:"%_filter%" "%%~ff" > "%%~ff.tmp" 

    rem rename original file to .old 
    ren "%%~ff" *.old > nul 

    rem rename temporary file as original file 
    ren "%%~ff.tmp" "%%~nxf" > nul 

) 
rem if needed, delete *.old files 
1

나는 다음과 같이 PowerShell을 사용하려고 할 것 원본 파일의 감지 된 행 그 뭔가를

0

당신이 .txt 파일의 모든 abcd 들어있는 행을 찾기 sed for Windows

sed -i.bak "/abcd/!d" *.txt 

사용할 수 있습니다 및 저장 :

0
@echo on 
For %%a in (*.txt) do (CALL:FILTER "%%a") 
echo/Done.&pause>nul&exit/b 
:FILTER 
type "%~1"|find "abcd" 1>nul 2>nul 
if %errorlevel% EQU 0 find /n "abcd" "%~1">"Found abcd in %~1.txt" 

명령은 반환 오류 수준 = 0 찾기 파일이 큰, 나는 그래서이 같은 거라고 경우 :

$Folder = 'C:\MyOldFiles' 
$NewFolder = 'C:\MyNewFiles' 
New-Item -ItemType Directory -Path $NewFolder -Force 

foreach ($file in Get-ChildItem $Folder) 
{ 
    Get-Content $file -ReadCount 1500 | 
    foreach { $_ -notmatch 'abcd' } | 
    Add-Content "$NewFolder\$($file.name)" 
} 
관련 문제