How to delete specific contents of a file?

ancient 21 Reputation points
2021-01-28T13:46:56.227+00:00

Hi All

Background info I need to remove a specific line within a file based on if the line below it contains x text
eg: if the below exists the delete the line above it

https://www.cardea.nhs.uk/cardea/

Line number
1 {
2 “checksum”: “d144cb6f1255ff4041579855b40de2ab”,
3 “roots”: {
4 “bookmark_bar”: {
5 “children”: [ {
6 “children”: [ {
7 “date_added”: “13249054777883609”,
8 “guid”: “95f3f891-5fbd-4dae-8cba-368c1472d894”,
9 “id”: “3”,
10 “name”: “Cardea”,
11 “show_icon”: false,
12 “source”: “import_fre”,
13 “type”: “url”,
14 “url”: “https://www.cardea.nhs.uk/cardea/”
15 }, {
16 “date_added”: “13249054777916773”,
17 “guid”: “b6d814ad-0ffe-43fc-b402-f59df0eeab12”,
18 “id”: “4”,
19 “name”: “Current Health”,
20 “show_icon”: false,
21 “source”: “import_fre”,
22 “type”: “url”,
23 “url”: “https://web.currenthealth.com/”
24 }, {

Help is much appreciated.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,576 questions
{count} votes

Accepted answer
  1. MotoX80 34,761 Reputation points
    2021-01-28T18:02:45.327+00:00
    $File = 'c:\temp\x'                                 # The file to process
    $SearchString = 'Carde'                             # The string to look for
    
    if (Test-path  ($File + ".old")) {                  # Does an old file already exist?
        "A .old file already exists. Do something with it."
        return
    }    
    
    $data = Get-Content $File                             # Read in the data before we...
    Rename-Item   $File -NewName ($File + ".old")         # ...rename it in case we need the original file.    
    
    $count = $data.count - 1                              # there is no next entry when processing the last entry    
    for($i = 0; $i -lt $count  ; $i++) {                  # loop thru each line except the last one 
        if ( $data[$i + 1] -match $SearchString) {        # does the next line contain the text we're looking for?
            "Removing: {0}" -f $data[$i]     
        } else { 
            $data[$i] | out-file $File -Append            # write this line out 
        }
    } 
    $data[$i] | out-file $File -Append                    # write out the last entry      
     
    

2 additional answers

Sort by: Most helpful
  1. Titan 206 Reputation points
    2021-01-29T10:35:23+00:00

    Hello @ancient !

    Does this work for you?

    Delete the line above those }, { lines

      $srcPath = "path\to\your\file"  
      $dstPath = "path\to\your\new_file"  
      
      (Get-Content -Path $srcPath -Raw) -replace '\n.*(\n\d*\s*\}(,\s*\{\n|\s*$))', '$1' |  
      Set-Content -Path $dstPath  
    

    Deletes e.g. line 13:

      (Get-Content -Path $srcPath -Raw) -replace '\n.*(\n.*https://www\.cardea\.nhs\.uk/cardea/.*\n)', '$1' |  
      Set-Content -Path $dstPath  
    

    Deletes line 13 and 14:

      (Get-Content -Path $srcPath -Raw) -replace '\n.*(\n.*https://www\.cardea\.nhs\.uk/cardea/.*\n)', '' |  
      Set-Content -Path $dstPath  
    

    Best whishes

    PS: I have updated my answer, hope it helps.

    ---
    If this Answer was helpful, accept and upvote it please.


  2. Ian Xue 38,551 Reputation points Microsoft Vendor
    2021-02-01T08:13:11.303+00:00

    Hi,

    MotoX80's script works well on my end. Or you can try this

    $file = 'D:\file.txt'  
    $result = 'D:\result.txt'  
    $url = 'https://www.cardea.nhs.uk/cardea/'  
    $temp = $null  
    Get-Content -Path $file | ForEach-Object {  
        if($_ -notmatch $url){$temp |Out-File -FilePath $result -Append}   
        $temp = $_  
    }  
    $temp |Out-File -FilePath $result -Append  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.