bash - sed replace with eval -


what want do

replace: '1000' with: (a random number) shuf -i 500-1500 -n 1 

shuf .. shall exectued every occurence of 1000

what i've tried

sed -i 's/1000/$(shuf -i 500-1500 -n 1)/g' 

but takes eval string

it happens because single quotes escape meaning of $ , command substitution not occurring.

solution use double quotes

sed -i "s/1000/$(shuf -i 500-1500 -n 1)/g" 

to replace each occurrence different values shuf, can loop on file using basic while , replace string as

while read line     echo "$line" | sed -i "s/1000/$(shuf -i 500-1500 -n 1)/g" >> output_temp done     mv output_temp original_file 
  • the while loop reads each line file , replaces them.
  • mvreplaces original file temp file.

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -