Answer the question
In order to leave comments, you need to log in
How does SED work with variables?
I execute the command
sed -i "s/\(\([^,]\+,\)\{9\}\)/\1$((RANDOM%1000000+8243678))/" file.txt
Answer the question
In order to leave comments, you need to log in
The RANDOM function is executed once when it is called, sed calls this function once when opening a file, so you need to loop it, like this:
cat file.txt | while read line; do echo $line | sed "s/\(\([^,]\+,\)\{9\}\)/\1$((RANDOM%1000000+8243678))/"; done
SED does not work with this variable in any way. When processing a command, the shell replaces the variable with a value that has already been calculated before even running sed itself.
As a result, sed does something like:
Konkase is right in advising you to use a loop.
you have one thread running and it contains one variable $RANDOM
try to make a loop for each line with the assignment of this variable. in fact, for each cycle it will be generated anew
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question