Gen random char then sed replacements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gen random char then sed replacements
# 1  
Old 11-09-2011
Gen random char then sed replacements

Hey guys,

I need to first generate some random characters, which I am already doing perfectly as follows:
Code:
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:
Code:
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!

Last edited by Franklin52; 11-10-2011 at 03:15 AM.. Reason: Please use code tags, thank you
# 2  
Old 11-10-2011
If you aren't dead set on using sed, you could try something like this:

Code:
#!/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[i] = rword[i] 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:
Code:
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
Code:
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.


Last edited by agama; 11-10-2011 at 02:05 AM.. Reason: typo
# 3  
Old 11-10-2011
Hi Agama,

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

This is working atm...

Code:
# 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

Last edited by holyearth; 11-10-2011 at 10:48 AM..
# 4  
Old 11-10-2011
Quote:
Originally Posted by holyearth
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.
# 5  
Old 11-10-2011
Thanks Agama!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

2. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

3. Shell Programming and Scripting

How to get count of replacements done by sed?

Hi , How can i get count of replacements done by sed in a file. I know grep -c is a method. But say if sed had made 10 replacement in a file, can i get number 10 some how? (8 Replies)
Discussion started by: abhitanshu
8 Replies

4. UNIX for Dummies Questions & Answers

How to use sed to do multiple replacements all at once?

I have a text file where I want to use sed to do multiple replacements all at once (i.e. with a single command) . I want to convert all AA's to 0, all AG's to 1 and all GG's to 2. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Using SED to replace a random number?

Hi all, I need to run a number of scripts which have a certain phrase on them so I have identified these by; grep -l 100 *script | sort -u Normally I could just run something along the lines of; for i in `grep -l 100 *script | sort -u`; do ./${i}; done However before I run each of... (0 Replies)
Discussion started by: JayC89
0 Replies

6. Shell Programming and Scripting

Removing special char's with sed

Hi, I've a txt file which contains the following kind of listed data 18971 ./aosrp18.r 15340 ./aosrp12.r 22996 ./aosrp08.r 17125 ./aosrp06.r I'm trying to get rid of the ./ in the file and have tried the following with sed but I'm not getting the correct result... I'm not sure what way... (7 Replies)
Discussion started by: Jazmania
7 Replies

7. Shell Programming and Scripting

Sed and random

Hi everyone, this is my first post. Some time ago I did a script to convert .csv files to oracle inserts. Now I need to add something and I can't make it work. This is the code: #!/bin/bash #Rev. 2 paso_uno() { local tmp=`mktemp temp.uno` cat $1 > $2 cat $2 > $tmp mv $tmp $2 }... (7 Replies)
Discussion started by: Tr0cken
7 Replies

8. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies

9. Shell Programming and Scripting

sed - delete until char x

Hi, I got a Textfile contains hundreds of lines like this: 3 02 8293820 0 22 22 All I need is this: 293820 0 22 22 So i decided to delete until the first '8' comes up. But how I can realize that? (9 Replies)
Discussion started by: mcW
9 Replies

10. Shell Programming and Scripting

sed escape char

Hi, For the following complex code , <!-- ... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies
Login or Register to Ask a Question