shell - replace each occurrence of "[i]" with some random number in text file using bash -
I want to change "[i]" sub-substring with some random number using bass. Like
abc. [I] .xyz [i] .lmn [i] should be output
abc.3.xyz.5.lmn.32 Here are 3, 5, 32 randomized numbers.
#! / Bin / bash # $ result replace_random ($ search, $ replaced), replace all the places in # $ # instead of $ # in # random number # using Bash's built-in PRNG and return on Return_Rendam () (local S = $ 1 local replace = $ 2 # while $ $ ($ search)) is replaced by $, replaces first place # $ with random number varies with "$ Replaced "forces this as a simple string To be believed in # and not to avoid example. '[I]' is defined as a # square class (the same is true for '?' And '*') while [[$ $ s} == * "$ {replace}" *]]; Echo $ $ s s = $ {s / "$ {replace}" / $ RANDOM} "$ {s}"} foo = 'abc [I] .xyz [i] .lmn [i] .aab 'bar = $ echo "$ {foo}" "[i]") "foo = [$ foo]" echo "bar = [$ bar] " .
$ ./t.sh foo = [abc. [I] .xyz [i] .lmn [i]. AAB] bar = [abc.10103.xyz4641.lmn21264.aab] You can use the example to force a random number into a small range < Pre s = $ {s / "$ {replace}" / $ ((RANDOM% 64))} which will result in a number of 0 - 63.
Comments
Post a Comment