Batch Script for filtering rows from textfile

first of all: I'm on a Windows 7 machine ;).

I got a folder with several dozens files. Each file contains about 240.000 rows. But only half of that rows are needed.

What I would like to do is: have a script that runs over these files, filters out every row that contains the string "abcd" and have it either saved in a new directory or just saved in the same file.

Answers


I would try using Powershell as below:

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

@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

you might use sed for Windows

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

Find all abcd containing rows in .txt files, make a backup file .bak and store the the detected lines in the original file.


@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"

The command Find returns error level = 0 if him find something


If the files are that big, I'd so something like this:

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

Need Your Help

Must Webkit always be used on the main thread?

multithreading cocoa webkit

I'm trying to render webpages in the background and I ran into the following trouble

In Veloeclipse How to change color of JavaScript content?

java eclipse eclipse-plugin velocity eclipse-juno

Am using veloeclipse, but javascript content colors are paled Green,want to change little dark,Is there anyway to achieve this.?