-1

I have a file and i can get 2 lines in the file.I have a file with the following content.

public class Websites
{
    //private const string URL = "https://au.yahoo.com/?p=us";
    private const string URL = "https://www.google.com/";
    //private const string URL = "https://stackoverflow.com/";
          ....

I can get the 2 lines individually like so:

$linegoogle = Get-Content "C:\myfile.cs" | Select-String "https://www.google.com/" | Select-Object - 
ExpandProperty Line
$google = $linegoogle.Trim()

$linestackoverflow = Get-Content "C:\myfile.cs" | Select-String "https://stackoverflow.com/" | 
Select-Object -ExpandProperty Line
$stackoverflow = $linestackoverflow.Trim()

But what i wabn to do is the following:

if (condition1)
{
    //replace the 2 lines  with
      
      // private const string URL = "https://www.google.com/";  
     private const string URL = "https://stackoverflow.com/";
}
else if (condition 2)
{
         //replace the 2 lines with

          private const string URL = "https://www.google.com/"; 
         // private const string URL = "https://stackoverflow.com/";
}
3
  • Are you sure you want to edit source files like so? Why not put urls in a config file instead? Commented Nov 23, 2020 at 8:10
  • Yes i want to edit the source file. not configs for this . Commented Nov 23, 2020 at 8:19
  • Does this answer your question: use of the switch statement: stackoverflow.com/a/58182017/1701026 Commented Nov 23, 2020 at 10:33

1 Answer 1

0

Certainly not an elegant solution, but this might do it for you:

# set the condition here. 
# can be either 'stackoverflow', 'google' or 'yahoo' as per your example file.
$condition = 'stackoverflow'

# read the file as single multiline string
$content = Get-Content "C:\myfile.cs" -Raw
# do the replacements depending on what is in $condition
$result  = switch ($condition) {
    'stackoverflow' {
        $content -replace '(?m)^(\s+)(private const string URL = "https://www\.google\.com/.*";)', '$1//$2' -replace
                          '(?m)^(\s+)(private const string URL = "https://au\.yahoo\.com/.*";)', '$1//$2' -replace
                          '(?m)^(\s+)//(private const string URL = "https://stackoverflow\.com/.*";)', '$1$2'
    }
    'google' {
        $content -replace '(?m)^(\s+)(private const string URL = "https://stackoverflow\.com/.*";)', '$1//$2' -replace
                          '(?m)^(\s+)(private const string URL = "https://au\.yahoo\.com/.*";)', '$1//$2' -replace
                          '(?m)^(\s+)//(private const string URL = "https://www\.google\.com/.*";)', '$1$2'
    }
    'yahoo' {
        $content -replace '(?m)^(\s+)(private const string URL = "https://stackoverflow\.com/.*";)', '$1//$2' -replace
                          '(?m)^(\s+)(private const string URL = "https://www\.google\.com/*";)', '$1//$2' -replace
                          '(?m)^(\s+)//(private const string URL = "https://au\.yahoo\.com/.*";)', '$1$2'
    }
}

# show the result on screen
$result

# write back to file
$result | Set-Content -Path "C:\myfile.cs"

Remember that -replace uses regex, so you need to escape the special characters in the urls (in this case, only the dots) with a backslash. Also append .* after the last slash in the urls to account for any possible querystrings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.