Gen random char then sed replacements

Hey guys,

I need to first generate some random characters, which I am already doing perfectly as follows:

randomize=`cat /dev/urandom | tr -dc "a-z0-9" | fold -w 6 | head -n 1`

This is where I am stuck...I need to sed replace some static values with those random characters, but I need each replacement to have *different* random characters...for example:

sed -i 's/blah/blah?$randomize/g' /home/test.html

^ Above does not work ... but it would replace all occurrences of "blah" with blah?au2bu and blah?d2g3h and blah?3juh4 ... I don't want them to all be blah?au2bu

Thanks for the help!

If you aren't dead set on using sed, you could try something like this:

#!/usr/bin/env ksh

awk  -v pattern="${1:-boo}" '
    function gen(       j, i, c )       # gen random replacement words
    {
        ridx = 0;
        for( i = 0; i < 500; i++ )
        {
            for( j = 0; j < word_len; j++ )
                rword = rword substr( soup, ((int(rand( ) *1000)) % length( soup )) + 1, 1 );
        }
    }
    BEGIN {
        soup = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";   # xlation
        word_len = 5;       # random word size
        gen();
     }

    {
        while( sub( pattern, "<goop>" rword[ridx++], $0 ) );    # assume <goop> is not in any input data nor the pattern
            if( ridx >= 500 )
                gen();
        gsub( "<goop>", pattern "?" );
        print;
    }
' "$@"

Reads from standard input or filename(s) supplied on the command line, writes to stdout. The word size can be changed.

Test input:

Now is the time to test
Forever is the time to try the programme
now the final sentance is here the end.

Test output with the command test.ksh the <data

Now is the?LC00s time to test
Forever is the?ON2nl time to try the?5berE programme
now the?jn1BE final sentance is here the?cJod1 end.

Hi Agama,

Thank you for that, but it seems a bit complicated...

This is working atm...

# Random character generator
randomizer()
{
randomize=`cat /dev/urandom | tr -dc "a-z0-9" | fold -w 6 | head -n 1`
result=$randomize
}

randomizer
echo $result

randomizer
echo $result

But I have to keep calling randomizer in order to make it get a fresh result and also when I call sed with $result, it makes all replacements the same rather than unique $result

Yes, you'll have to 'get a bit more complicated' if you want each replacement to be different.

Thanks Agama!