0

I need to remove double quote in a text file, only if lines start with "https". File contents is like this:

...
    "bla, bla, bla"
    "https://example.com"
    "bar, bar, bar"
...

I've to match "https://example.com", remove both double quotes, leaves the double quotes in other lines, than set-content in place...

I've tried many methods but I'm stuck bacause I don't know how to handle double quotes in a regex, or declaring a filter in "if" or "where" statement and than replace text in place...

Latest try:

$TextFile = Get-Content "e:\file.txt"
foreach ($Line in $TextFile) {if ($Line.StartsWith('"https')) { $line.trim('"')} | Set-Content $TextFile

But not works...

I've read this post and this but I don't understand how to fit these solutions to my needs..

Can Someone help me please?

ilRobby
  • 69
  • 2
  • 10

2 Answers2

2

Read the textfile in as single string using the -Raw switch and then do a regex replace:

(Get-Content "e:\file.txt" -Raw) -replace '(\s*)"(https:[^"]+)"','$1$2'

If you need to overwrite the text file with the new content, append

| Set-Content -Path "e:\file.txt" -Force

to the above.

Output:

...
    "bla, bla, bla"
    https://example.com
    "bar, bar, bar"
...

Regex details:

(             Match the regular expression below and capture its match into backreference number 1
   \s         Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
      *       Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
"             Match the character “"” literally
(             Match the regular expression below and capture its match into backreference number 2
   https:     Match the characters “https:” literally
   [^"]       Match any character that is NOT a “"”
      +       Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"             Match the character “"” literally
Theo
  • 57,719
  • 8
  • 24
  • 41
  • What can I say... really awesome Theo! Just to be sure that I've understand correctly, the expression `'$1$2'` represent the number of times that `-replace` has to act? – ilRobby May 15 '20 at 19:48
  • @ilRobby Thanks and no. `$1$2` means to replace the string with what is captured in backreference 1 and 2.(the stuff in between the brackets). The first in this case captures the whitespace to the left; the second captures the url between the quotes. – Theo May 15 '20 at 19:53
0

To GET the 'https://' string without double quotes:

$content = Get-Content PATH TO YOUR FILE

foreach( $line in $content ) {
    if( $line -match "(`"https:\/\/)" ) {
        $line -replace '"',''
    }
}
Vish
  • 466
  • 2
  • 12