none
Delete somes lines for each files in directory RRS feed

  • Question

  • Hi,

    I am looking for a way to remove some lines in each files in a directory.

    I have 10 xml files and want to delete lines 1, 2, 5, 8, 10, 22, 23, 24
    I found GnuWin32 awk.exe which can delete lines number but I don't know how to integrate it in a script :

    Get-ChildItem "$PathReports" -Filter *.xml | 
    Foreach-Object{
    "C:\Program Files (x86)\GnuWin32\bin\awk.exe" "'NR!~/^(1|2|5||8|10|22|23|24)$/'"
    }

    Thank you in advance for your help


    • Edited by Fixit34 Thursday, November 20, 2014 3:07 PM
    Thursday, November 20, 2014 3:04 PM

Answers

  • You don't really need the 3rd party utility for that:

    $Remove = 1,2,3,5,8.10,22,23,24
    
    $Remove = $Remove | sort -Descending
    
    Get-ChildItem "$PathReports" -Filter *.xml | 
    Foreach-Object {
      $FileData = {Get-Content $_.Fullname}.invoke()
       Foreach ($LineNumber in $Remove) 
        { $Filedata.RemoveAt($LineNumber) }
      $FileData | Set-Content $_.FullName
     }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinorModerator Thursday, November 20, 2014 3:31 PM
    • Marked as answer by Fixit34 Thursday, November 20, 2014 3:44 PM
    Thursday, November 20, 2014 3:27 PM
    Moderator
  • You can do this by reading the file into an ArrayList object:


    $lines = new-object System.Collections.ArrayList
    $lines.AddRange((get-content "whatever.txt"))
    

    You can use the RemoveAt method to drop the lines you don't want:


    $lines.RemoveAt(24)  # Removes line 25; index starts at 0
    

    After removing the desired lines, write the file again:


    $lines | out-file "whatever.txt"
    


    -- Bill Stewart [Bill_Stewart]


    Thursday, November 20, 2014 3:43 PM
    Moderator

All replies

  • You don't really need the 3rd party utility for that:

    $Remove = 1,2,3,5,8.10,22,23,24
    
    $Remove = $Remove | sort -Descending
    
    Get-ChildItem "$PathReports" -Filter *.xml | 
    Foreach-Object {
      $FileData = {Get-Content $_.Fullname}.invoke()
       Foreach ($LineNumber in $Remove) 
        { $Filedata.RemoveAt($LineNumber) }
      $FileData | Set-Content $_.FullName
     }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinorModerator Thursday, November 20, 2014 3:31 PM
    • Marked as answer by Fixit34 Thursday, November 20, 2014 3:44 PM
    Thursday, November 20, 2014 3:27 PM
    Moderator
  • You can do this by reading the file into an ArrayList object:


    $lines = new-object System.Collections.ArrayList
    $lines.AddRange((get-content "whatever.txt"))
    

    You can use the RemoveAt method to drop the lines you don't want:


    $lines.RemoveAt(24)  # Removes line 25; index starts at 0
    

    After removing the desired lines, write the file again:


    $lines | out-file "whatever.txt"
    


    -- Bill Stewart [Bill_Stewart]


    Thursday, November 20, 2014 3:43 PM
    Moderator
  • It is perfect !!

    Thank you

    Thursday, November 20, 2014 3:44 PM
  • You're welcome!

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, November 20, 2014 4:07 PM
    Moderator
  • # want to delete lines 1, 2, 5, 8, 10, 22, 23, 24
    
    foreach ($File in (Get-ChildItem -Path "d:\sandbox\test" -Filter *.xml)) {
        $Lines = Get-Content -Path $File.FullName 
        $Lines = $Lines[2 , 3 + 5 + 6 + 8 + 10..20 + 24..($Lines.length - 1)]
        $Lines | Out-File -FilePath $File -Encoding ascii -Force -Confirm:$false
    }


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable) _________________________________________________________________________________ Powershell: Learn it before it's an emergency http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    Thursday, November 20, 2014 4:38 PM