I have a file with the format

<string> <string> <string>

I am trying to replace the third column string with random data using sed. The third column string length is fixed and is 48 digits

I was able to fix up a solution using read

while read a b c; do
    echo $a $b $(cat /dev/urandom | tr -dc '0-1' | fold -w 48 | head -n 1)
done < input > output

However the loop takes too long too process. How can I do this with sed.


cat /dev/urandom | tr -dc '0-1' is a horribly slow way to get random numbers - you're discarding a lot of data just because it doesn't parse to an ASCII number.

You could use od or hexdump to convert the data to numbers, instead, maybe awk to replace the third field:

$ awk -v cmd="od -t u2 -A n /dev/urandom | tr -d ' \n' | fold -w 48" '{cmd | getline a; $3=a}1' foo
a b 544404963198532458179603539139474299576034345258
a b 288743886427692659230387417952036728563243415446
a b 862132599821016414678116482868514114597223157825
a b 033164829235119519053106724136729063192651002644
a b 344594724128550250448115260033862902855932105599
a b 312488113115218631189450762993316672549312384472
a b 536218132619476215470461793189974621744028599402
a b 312943765306550325469516346331872556359574159859
a b 530561655804530239462031928466172515137971221356
a b 295271911741933346066441964722122556255551414840

od -t u2 -A n /dev/urandom | tr -d ' \n' | fold -w 48 converts the random data into two-byte unsigned ints, then we use tr to remove the spaces from od's output.

awk is very nice for column-based text processing. The <command> | getline method allows you to run the <command> once and then read its output for the duration of the awk command.

A simpler way than coercing od output to the required format is to use hexdump:

hexdump -e '"%u"' /dev/urandom | fold -w 48

Using the perl Bytes::Random::Secure module:

$ perl -MBytes::Random::Secure=random_bytes_hex -alne '
  print join " ", @F[1..2], random_bytes_hex(24)
' file
<string> <string> 56fe375ade090e9838c13b5bbb799c2fac26d42da3f01691
<string> <string> 8bf748fb4d372e701af5ae0075a9de58c8ce3b05210c24bd
<string> <string> 345c58a5983e7bade8fed0311267daeea9ed314b1efe33d1

The module is available in Ubuntu via package libbytes-random-secure-perl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy