Die ersten 20 Zeilen von .txt Datein automatisch löschen lassen?

Hallo ich habe einen Ordner mit 1000en Unterordnern und überall befinden sich txt Dateien drinnen wo immer die ersten 20 Zeilen Text gelöscht werden sollen bei allen … das mit der Hand zu machen ist unmöglich oder dauert Wochen …

(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
5 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jo135
1 year ago

With the common Unix tools, this is a small thing. For a single file:

sed -i '' '1,20d' datei.txt

For all *.txt files in all folders under the current:

find . -type f -name "*.txt" -exec sed -i '' '1,20d' "{}" \;

This should be tested carefully (sed without i-flag!). Or at least secure the data beforehand.

I’m sure it’s going with Powershell if you’re on Windows. But there’s the syntax even worse.

jo135
1 year ago

These are commands that are directly available on Linux, MacOS and similar operating systems. On Windows, it’s probably the easiest thing to do with WSL2. And as I said, you can probably get the same thing with Powershell, but that’s where I get out.

Erzesel
1 year ago
Get-ChildItem -Path '.' -Filter '*.txt' -Recurse -ea sil|
 %{
   $NewText = Get-Content $_.FullName|
    Select-Object -Skip 20
   $NewText|
    Set-Content $_.FullName
 }

parallel to your masses of files:

function Get-PartSizes ( [Long]$Size , [Long]$Parts ) {
    $Quotient=[Math]::Floor($Size/$parts)
    $Remainder=$Size%$parts
    $(for ($i=0;$i -lt $parts; ++$i){
        if ($Remainder){
            $Quotient+1
            --$Remainder
        }
        else{$Quotient}
    })|? {$_}
}


 #was die Jobs  tun sollen (das  gleiche  wie   im oberen  Script)
$Job_Worker = {
    param(
        [Array]$FileList=@()
    )
    $FileList |
        %{ 
            "verarbeite Datei $_" #nur mal ein Lebenszeichen des Jobs aus  dem Hintergrund
            $NewText = Get-Content $_ |
                Select-Object -Skip 20
            $NewText|
                Set-Content $_
        }
}


 #Anzahl der paralelen  Jobs festlegen  (Hier Automatisch) 
$JobCount=$env:NUMBER_OF_PROCESSORS


  #Liste  aller .txt -Dateien
[Array]$SearchFiles =(Get-ChildItem -Path 'C:\basepath' -Filter *.txt -Recurse -ea Silent).FullName


 #Dateiliste in einigermaßen gleich große Teile zerlegen
$ListSlices=Get-PartSizes $SearchFiles.Count $JobCount |
    ?{$_}|
    %{$base=0}{
        ,$SearchFiles[$base..($base+$_-1)]  #aufpassen, Comma-Operator ...als neues Array übergeben
        $base+=$_
    }


  #Jobs  starten
$Jobs = $ListSlices|
    %{ Start-Job  -ScriptBlock $Job_Worker  -ArgumentList (,$_) }
 #auf vollzug  warten
$Jobs| Receive-Job -wait