INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

CMD script to search/replace & in file

CMD script to search/replace & in file

(OP)
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.

RE: CMD script to search/replace & in file

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close