header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Select-String and delete the string
Last Post 09 Mar 2010 09:52 AM by seanels. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
seanelsUser is Offline
New Member
New Member
Posts:14
Avatar

--
24 Feb 2010 01:04 PM
    Hi, I am using Select-String to find the line in a file that matches a string. I know the file will always be in the first few lines, so I only want to get the first match then stop the search, because the file is huge. I have retrieved the line, but what is the best way to delete the line from the file?

    See attachment for input file and code.









    testxml.xml
    test.ps1

    SureshUser is Offline
    New Member
    New Member
    Posts:31
    Avatar

    --
    25 Feb 2010 12:02 AM
    This is what I can think of now:

    $testXML = [System.Collections.ArrayList] (gc .\testxml.xml)
    $testXML.Remove("String to be removed") # This will remove the first instance of the string in the list array.
    Out-File -FilePath updatedtestxml.xml -InputObject $testXML $Encoding default


    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    25 Feb 2010 01:20 AM
    Unfortunately, if you want to replace a specific line of text in a text file, you have to re-write the entire file once you've found the line you want to replace.

    I don't know how long your XML file is because the attachment got messed up, however, I recently had a scenario where I had to see if a string existed in a 250,000 line XML file, and add it if it wasn't there, and I had to compare each line with 10 different strings. However, doing that literally took maybe half a second, so I wouldn't worry about performance too much if you aren't going to be doing this frequently.

    If you're still curious as to what the best way is, the best way would be to create a new file to write to as you are reading from the original file. That way, you aren't putting the entire file in memory all at once. Another thing to consider is that when re-writing the original text file, you are in danger of data loss if the re-writing operation fails half-way through, and that's why using a proxy text file is an advantage in this case.


    seanelsUser is Offline
    New Member
    New Member
    Posts:14
    Avatar

    --
    25 Feb 2010 04:54 AM
    Thank you sureshbabutadisetty, your suggestion worked perfectly and effeciently. Thanks GWHowarth88, good advice to write to a new file to avoid errors.

    I had a bit of trouble converting my Select-String output to a string, but got there in the end:

    $myLine = Select-String -Path $file -Pattern "string to be matched" -List | Select-Object Line
    $myString = $myLine.Line


    seanelsUser is Offline
    New Member
    New Member
    Posts:14
    Avatar

    --
    09 Mar 2010 09:52 AM
    There is a more efficient way to do this for anyone interested. See this post:

    http://stackoverflow.com/questions/...wer/submit

    Essentially, cast to [xml] created 2 items: string (probably from the DTD part) and xml. In order to parse the XML, you need to access it like so:

    [xml]$xml = (get-content "file.xml")
    # to access the root node of the xml:
    $xml.root[1]


    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer