Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey PowerShell Guy !, How Can I Replace Text That Includes Double Quote Marks and a Tab Character?

Some time ago but a new Hey Scripting Guy ! translation to PowerShell

How can I replace text that includes double quote marks and a tab character ?

 the example given in this article is :

Speaking of which, let’s talk scripting for awhile. As we understand it, FC, you have a file (or maybe a bunch of files) that include this text, where the underscore represents the tab character:

rowsep="1">_

What you’d like to do is replace each of those values with this:

rowsep="1"><para>

For the complete example see the original article. 

The PowerShell version looks like this :

$new = (Get-Content C:\Scripts\test.txt) -replace "(rowsep=`"1`">)`t", '$1<para>'
Set-Content C:\Scripts\test.txt $new

Some notes :

  • to escape in PowerShell we use the Backtick character ( ` ), when we use sigle quotes we do not need to escape the Double Quotes but inside a Single quotes string we can not use escaped charaters like `t (meaning Tab) hence the use of Double quotes and the escaping of the double quotes.
  • Next interesting point in this example is the use of Sub captures in the replacement, when we use the -Replace operator we can capture part of the original string by putting this part into parentheses (), the whole replacementstring is captured in the $0 variable and ever () block will get assigned to its own variable and will be numbered in this case $1 will contain the part of the original string we want to preserve (without the TAB )
  • Same as in the original VbScript example we need to first save the new text into memory, we do that by assigning it to the $new variable and after that on the next line write this text back to the test.txt file.
  • Also note that as the variable replacement of $1 is handled by the -Replace operator we do not need to use Double Quotes to resolve this variable.

Enjoy,

Greetings /\/\o\/\/ 

 

Published Tuesday, December 11, 2007 10:34 AM by MoW
Filed under: , ,

Comments

# Interesting Finds: December 12, 2007

Wednesday, December 12, 2007 10:12 AM by Jason Haley

# Hey PowerShell Guy !, How Can I Remove Duplicate Values From a Pair of Text Files?

Again a Hey Scripting Guy ! translation about replacements in Text files : How Can I Remove Duplicate

Thursday, December 13, 2007 6:48 PM by The PowerShell Guy

# re: Hey PowerShell Guy !, How Can I Replace Text That Includes Double Quote Marks and a Tab Character?

So I am still a bit confused.  And I am using Powershell 1.0, so bear with me and point out if I am needint PS v2 or just not scripting this right.  I have a situation where I need to locate a text string in a file, and add some text below it.  in this config file there is a section "<listeners>" which I can easily pick out with:

get-content filename | select-string "<listeners>"

works awesome!

now I need to add right below that header, and the best I have come up with is using a double-double quote with backtick method I found on-line - which doesn't produce erors, but also does not appear to add the text.  so do I need add-content, then set content?

Help!  here's the second line - including double double quotes with backtick.  for some reason if I add the first < character in front of "add name" it errors..

<add name="`"EventLogListener`"" type="`"XYZSoftware.Tracing.Listeners.EventLogTraceListener, XYZ_Trace, Version=1.2.3.4, Culture=neutral, PublicKeyToken=1a2b3c4d5e6f`"" initializeData="`"App_name`"" />

SO...  should I only be typing in one backtick in fronnt of a double quote?  like "<add name=`"blah blah`"

Thank you in advance and I will wait patiently for an answer!

mr_tim

Wednesday, January 27, 2010 12:26 PM by mr_tim

# re: Hey PowerShell Guy !, How Can I Replace Text That Includes Double Quote Marks and a Tab Character?

Please pardon the typo. The second line should start with:

add-content "<add name=

Wednesday, January 27, 2010 12:31 PM by mr_tim
Anonymous comments are disabled