views:

142

answers:

2

Suppose I have a large number of strings formatted something like:

<tag>blah blahXXXXXblah blah</tag>

I want to transform these strings into something like:

blah blahZZZZZblah blah

on a powershell command line. All instances of XXXXX get replaced by ZZZZZ in the transformation and the outer tags are stripped out. It isn't well-formed XML.

I can write a script that would evaluate this easily enough, I believe, but when dealing with this particular bit of software I find myself performing tasks like this more often than I'd like. I'm interested in learning how to do this straight from the powershell command line without the additional step of writing a .ps1 script to run.

It seems like something powershell would be good at, I just don't know how. :)

A: 

if you can write the script just create a cmdlet

MSDN docs on Cmdlet creation

Alex
I'm explicitly asking how to do it on the commandline without writing something separate.
Greg D
+2  A: 

Well the simplest way that I can think of (assumes your list is held in $foo):

$foo | %{$_.Replace("XXXXX", "ZZZZZ")}
EBGreen
Oops...didn't see that you wanted to remove the tags too, but that would be similar.
EBGreen
In this case, a replace with empty string would be enough to remove the tags. I think the %{} construct is what I was looking for, thanks! Is that essentially a foreach object in $foo structure?
Greg D
yup. It is actually an alias for the ForEach-Object commandlet which isn't exactly the same as a raw foreach loop, but close enough. The Get-Alias commandlet is handy for figuring out what things are when people are posting sample command lines.
EBGreen
@EBGreen nice one again :)
Steven Murawski