用于从文本文件中过滤行的批处理脚本

时间:2013-11-22 09:10:33

标签: powershell batch-file powershell-v2.0

首先:我在Windows 7机器上;)。

我有一个包含几十个文件的文件夹。每个文件包含大约240.000行。但是只需要一半的行。

我想要做的是:拥有一个运行在这些文件上的脚本,过滤掉包含字符串“abcd”的每一行,并将其保存在新目录中或者只保存在同一个文件中。

5 个答案:

答案 0 :(得分:1)

我会尝试使用Powershell,如下所示:

$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
}

答案 1 :(得分: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

答案 2 :(得分:0)

您可以使用sed for Windows

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

查找abcd个文件中包含的所有.txt行,制作备份文件.bak并将检测到的行存储在原始文件中。

答案 3 :(得分: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"

命令Find如果找到某些内容则返回错误级别= 0

答案 4 :(得分: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)"
 }