Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CMD script to search/replace & in file

Status
Not open for further replies.

Exie

Programmer
Sep 3, 2003
156
AU
Hi Folks,

I know this isnt the right forum, I searched, but couldnt find the right spot, so hopefully powershell is close enough.

My need is, to have a basic CMD script run a command, that pops out a flat XML file, but within some of the elements is the & symbol.

This needs to be changed to &

On my workstation, I just used sed 's/&/&/g' but it seems the designers arent happy about installing sed so I'm looking for an alternative.

I can use findstr to locate the offending line, but cant wrap my mind around changing it.

Best I can think of is, use a for loop to iterate over each line in the file, test it, when found, re-write the line with corrected output. Seems like the hard way though.

Any ideas/thoughts would be welcome.
 
This script is bare-bones but will do it
Code:
param(
   $infile = $(throw "Please specify an input filename"),
   $outfile
   )

if ($outfile -eq $null)
    {
        $outfile = $infile
    }
$content = Get-Content $infile
$content = $content -replace '&', '&'
$content | Set-Content $outfile
Save that to something like Fix-OurXML.ps1, then they can run it as
.\Fix-OurXML.ps1 file1.xml
which will overwrite file1.xml with the correction, or
.\Fix-OurXML.ps1 file1.xml file1-fixed.xml
which will create a new file and leave the original.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top