5

I'm using sed to substitute a random 10 digit string of numbers for a certain field in a file, which I can successfully do. However, the same random 10 digit string of numbers are used for each substitution sed performs which is unacceptable in this case. I need a new random 10 digit string of numbers for every substitution sed performs. Here's what I have so far:

#!/bin/bash
#
#
random_number()
    {
    for i in {1}; do tr -c -d 0-9 < /dev/urandom | head -c 10; done
    }

while read line
    do
        sed -E "s/[<]FITID[>][[:digit:]]+/<FITID>$(random_number)/g"
    done<~/Desktop/FITIDTEST.QFX

Here's a sample of what the original FITIDTEST.QFX file looks like:

                <FITID>1266821191

                <FITID>1267832241

                <FITID>1268070393

                <FITID>1268565193

                <FITID>1268882385

                <FITID>1268882384

And here is the output after executing the script:

                <FITID>4270240286

                <FITID>4270240286

                <FITID>4270240286

                <FITID>4270240286

                <FITID>4270240286

                <FITID>4270240286

I need those 10 digit numbers to be different for each field. I thought the "while loop" would force sed to call the random_number() function each time but apparently it's called once and the value is stored and used repeatedly. Is there anyway to avoid that? Any help is greatly appreciated!

2 Answers 2

2

Just use awk:

$ cat tst.awk
BEGIN { srand() }
{
    sub(/[0-9]+/,sprintf("%010d",rand()*10000000000))
    print
}

$ awk -f tst.awk file
<FITID>3730584119
<FITID>1473036092
<FITID>8390375691
<FITID>6700634479
<FITID>8379256766
<FITID>6583696062

$ awk -f tst.awk file
<FITID>7844627153
<FITID>0141034890
<FITID>9714288799
<FITID>0911892354
<FITID>8916456168
<FITID>4187598430
0
1

Your sed is replacing all the lines with matching pattern not just one line hence at the end of loop you are seeing same number in replacement.

You can use:

while read line; do
    sed -E "/<FITID>/s/<FITID>[[:digit:]]+/<FITID>$(random_number)/" <<< "$line"
done < ~/Desktop/FITIDTEST.QFX > _tmp_

Output:

cat _tmp_

<FITID>9974823224
<FITID>1524680591
<FITID>7433495381
<FITID>6642730759
<FITID>9653629434
<FITID>1325816974
3
  • I still need to execute substitution for EVERY match but I just need every match to be substituted with a different random 10 digit number. Your solution appears to only substitute the first match with a random 10 digit number. May 25, 2016 at 2:28
  • 1
    Your updated command worked, thank you! So if I understand this correctly, the "Here String" (<<<) is feeding sed one line at time and by adding: <FITID> before /s/<FITID> tells sed to execute on each line which in turn calls the $(random_number) function each time and generates a new random number? May 25, 2016 at 14:28
  • Yes you summed it very well, that is exactly how it is working.
    – anubhava
    May 25, 2016 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.