How to delete specific contents of a file ?

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 then delete the line above it

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

https://web.currenthealth.com/

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 }, {

In a previous post I was just deleting certain line numbers but have found they differ for some users…this now makes it more complex hence a new topic.

Help is much appreciated.

This looks like json to me. If so, there is a built in conversion command in PowerShell that will greatly simplify your life. Just import the file, do the conversion, modify using normal objects (not parsing text), dump it back out to a new file.

$myfile = "C:\yourfile.txt"

$data = Get-Content -Raw -Path $myfile | ConvertFrom-Json

#modify data as needed here

$data | ConvertTo-Json | Set-Content -Path "C:\Yournewfile.txt"

 

Hi Mike

Thanks for the info…only problem is that I only want to delete the favourites that have been deployed.

The personal ones the staff have saved need to remain and each person may have random url’s saved which I won’t know.

It seems deleting the line eg line 13 ““type”: “url”,” then deletes the actual url below from the favourites bar

Where as if I delete the actual line that contains the url, (line 14) it recreates when the browser is restarted.

This is why you need to work with a collection of objects not parsing strings with regex. Can you post an entire file contents (if you need to redact personal info just ‘xxx’ through it? If so, I’ll show you what I mean.

Hi Mike

I finally understood what you mean…had help from someone else and did indeed convert it.

Thank you.