none
How to add parenthesis around a string or a text RRS feed

  • Question

  • Hi,

    I have a string -  'switch $choice' . I need to have parenthesis around $choice so that string looks like switch ($choice)' . So how to add parenthesis around $choice?

    Friday, August 26, 2016 12:35 PM

Answers

  •  'switch $choice' -replace '\$Choice','($Choice)'

    Friday, August 26, 2016 12:41 PM
  • Hi Charud,

    welcome to Technet. This should do the trick:

    (Get-Content file.txt) | ForEach-Object {
    	$_ -replace 'switch \$Choice in', 'switch ($Choice) in'
    } | Set-Content file.txt

    Please give a little thought to your questions before you post them and consider presenting a little context. While it isn't necessary (and in fact detrimental) to post your entire life's story here, sufficient context to answer "what" and "why" saves a lot of time, both yours and that of those answering you.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, August 26, 2016 1:54 PM

All replies

  •  'switch $choice' -replace '\$Choice','($Choice)'

    Friday, August 26, 2016 12:41 PM
  • This is ok, but the catch is, in my powershell script, I am reading this string from a text file (which contain other irrelevant data). So a line in the text file contains - "switch $choice in". This needs need to be replaces as "switch ($choice) in". How to achieve this?
    Friday, August 26, 2016 1:45 PM
  • (Get-Content D:\Test.txt ) -replace '\$Choice','($Choice)'

    $File = Get-Content D:\Tests\file.txt
    $File -replace '\$Choice','($Choice)'


    Friday, August 26, 2016 1:48 PM
  • Hi Charud,

    welcome to Technet. This should do the trick:

    (Get-Content file.txt) | ForEach-Object {
    	$_ -replace 'switch \$Choice in', 'switch ($Choice) in'
    } | Set-Content file.txt

    Please give a little thought to your questions before you post them and consider presenting a little context. While it isn't necessary (and in fact detrimental) to post your entire life's story here, sufficient context to answer "what" and "why" saves a lot of time, both yours and that of those answering you.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, August 26, 2016 1:54 PM
  • Why Foreach.. It worked without Foreach

    PS C:\> Get-Content D:\Tests\File.txt
    This is line One switch $choice in
    This is line Two
    switch $choice in This is line Three
    This is switch $choice in line Four
    
    PS C:\> (Get-Content D:\Tests\File.txt) -replace '\$choice','($choice)'
    This is line One switch ($choice) in
    This is line Two
    switch ($choice) in This is line Three
    This is switch ($choice) in line Four

    Friday, August 26, 2016 2:00 PM
  • Why Foreach.. It worked without Foreach

    Weellll ... mostly, because of this retro feeling I have for PowerShell 2. We still have quite a few customers who still refuse to bury or at least update their old servers to PS4+. While this code will actually work in v2, many of the simplified array-content access shortcuts will not. Example:

    (Get-Content test.txt).SubString(0, 5)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, August 26, 2016 2:07 PM
  • Started using PowerShell from v5

    So don't know much about v2 or v3. Thanks for replying, i am new to powershell so wanted to know if there was some other reason to use foreach.


    Sunday, August 28, 2016 4:24 PM
  • In that case: Welcome to PowerShell :)

    Generally, I often use it for compatibility reasons. It's also helpful for beginners, as it shows them a tool they can use for other, similar tasks (as you could put in pretty much anything in that scriptblock).


    There's no place like 127.0.0.1

    Monday, August 29, 2016 7:32 AM