views:

274

answers:

4

I have a file containing a large number of occurrences of the string Guid="GUID HERE" (where GUID HERE is a unique GUID at each occurrence) and I want to replace every existing GUID with a new unique GUID.

This is on a Windows development machine, so I can generate unique GUIDs with uuidgen.exe (which produces a GUID on stdout every time it is run). I have sed and such available (but no awk oddly enough).

I am basically trying to figure out if it is possible (and if so, how) to use the output of a command-line program as the replacement text in a sed substitution expression so that I can make this replacement with a minimum of effort on my part. I don't need to use sed -- if there's another way to do it, such as some crazy vim-fu or some other program, that would work as well -- but I'd prefer solutions that utilize a minimal set of *nix programs since I'm not really on *nix machines.

To be clear, if I have a file like this:

etc etc Guid="A" etc etc Guid="B"

I would like it to become this:

etc etc Guid="C" etc etc Guid="D"

where A, B, C, D are actual GUIDs, of course.

(for example, I have seen xargs used for things similar to this, but it's not available on the machines I need this to run on, either. I could install it if it's really the only way, although I'd rather not)

A: 

you can just capture the uid into a variable first, then do the sed?

@echo off
setlocal enabledelayedexpansion
for /f %%x in ('uuidgen.exe') do (
        set uid=%%x
)
sed -e "s/Guid=\"\(.*\)\"/Guid=\"!uid!\"/g" file
ghostdog74
Be careful that using .* in your regular expressions may cause it to grab more than you expect, depending on the mode that it's run in. You might consider instead doing something like "[^"]+" (Note how it explicitly will never swallow a literal " )
Steven Schlansker
That appears to replace every GUID in the file with the same new GUID (when I try it). I need to replace every GUID with a new, unique GUID.
Josh Petrie
A: 

Would you be open to compiling a C# console app to do this? I whipped this up real quick. It takes a filename as a command line argument, finds anything that looks like a GUID, replaces it with a new GUID, and writes the new contents of the file.

Take a look:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace GUIDSwap
{
    class Program
    {
        static int Main(string[] args)
        {
            try
            {
                if (args.Length == 0) throw new ApplicationException("No filename specified");

                string filename = args[0];
                filename = filename.TrimStart(new char[] { '"' }).TrimEnd(new char[] { '"' });

                if (!File.Exists(filename)) throw new ApplicationException("File not found");

                StreamReader sr = new StreamReader(filename);
                string text = sr.ReadToEnd();
                sr.Close();

                StringBuilder sbNew = new StringBuilder();

                string pattern = "[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}";

                int lastStart = 0;
                foreach (Match m in Regex.Matches(text, pattern))
                {
                    sbNew.Append(text.Substring(lastStart, m.Index - lastStart));
                    sbNew.Append(Guid.NewGuid().ToString());
                    lastStart = m.Index + m.Length;
                }

                sbNew.Append(text.Substring(lastStart));

                StreamWriter sw = new StreamWriter(filename, false);
                sw.Write(sbNew.ToString());
                sw.Flush();
                sw.Close();

                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 1;
            }
        }
    }
}
BigJoe714
A: 

I rewrote the C# solution in PowerShell. I figured it would be easier for you to run a powershell script then compile a C# exe.

Steps for using this:

  1. Download/install powershell
  2. Save the code below somewhere, named GuidSwap.ps1
  3. Modify the $filename and $outputFilename variables to suit your needs
  4. Run powershell -noexit c:\location\to\guidswap.ps1

## GuidSwap.ps1
##
## Reads a file, finds any GUIDs in the file, and swaps them for a NewGUID
##

$filename = "d:\test.txt"
$outputFilename = "d:\test_new.txt"

$text = [string]::join([environment]::newline, (get-content -path $filename))

$sbNew = new-object system.text.stringBuilder

$pattern = "[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}"

$lastStart = 0
$null = ([regex]::matches($contents, $pattern) | %{
    $sbNew.Append($text.Substring($lastStart, $_.Index - $lastStart))
    $guid = [system.guid]::newguid()
    $sbNew.Append($guid)
    $lastStart = $_.Index + $_.Length
})
$null = $sbNew.Append($text.Substring($lastStart))

$sbNew.ToString() | out-file -encoding ASCII $outputFilename

Write-Output "Done"
BigJoe714
I am a developer, I'm not sure where you saw otherwise. This, however, will be more useful than writing a C# app to do it.
Josh Petrie
Sorry about that. That was a different question where I saw that.
BigJoe714
A: 

how to replace all instance of a GUID with a new certain one, not variants? thanks.