mardi 23 juillet 2019

replace a string of n chars with a random string of n chars in bash

I have a file called tmp.txt. It has chars 'a-z' separated by spaces on one side of a tab delimited file and numbers '0-9.' on the other side. for each line I want to do some random replaces of 1-3 characters. my tmp.txt looks like this:

s h e h a d y o u r 0.9472 0.2074 0.4878 0.2227 0.4998 0.2841 0.5323 0.4254 0.539 0.4981  
d o n t a s k m e t o c a r r y 0.9741 0.0999 0.338 0.0572 0.4514 0.223 0.5036 0.3835 0.4844 0.6306 
e v e n t h e n 0.8549 0.1265 0.5248 0.2713 0.622 0.2011 0.4334 0.4137 0.4788 0.5435

I have written this much of a script so far:

cat tmp.txt | while IFS= read -r line; 
    do 
        for i in {1..3}; 
        do 
            a=$(tr -dc 'a-z0-9' | head -c $i);
            b=$(head /dev/urandom | tr -dc 'a-z0-9' | head -c $i);
            sed -i 's/$a/$b/g';
        done; 
    done

The sed doesn't seem to be finding $line since I get:

sed: no input files
sed: no input files
sed: no input files

I thought that I was still within read -r line in this loop but it seems I am wrong. Does anyone know what I am doing wrong?

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire