2

I am attempting to use a bash script to create multiple output files from a file I have already created. As well as creating 10 output files I want to alter one piece of text in each of the files i'm creating from the original.

In the original document the starting lines read:

# For a single processor calculation
variable T equal 300 # Simulation temperature
variable salt equal 100.0 # Salt concentration [mM]

# Random number seed for Langevin integrator
variable random equal CPRAND 

I want to change text that reads CPRAND to a random number in each file that I am creating.

The current bash script which I am using right now to try and do this is listed below:

for i in {0..10}
do
   cat me.sh | sed ’s/CPRAND/$((1 + RANDOM % 1000))/g‘ > “RunFile$(printf “%03d” “$i”).in”
done

I have not had any luck in getting it to work so far for multiple file creation. I appreciate any advice on this.

Thanks!

3
  • Please use four spaces in front of your file contents and code instead of the > (which is meant for quoting). You can also highlight the block and hit the {} button to properly format it. It's difficult to understand how your file is structured with the way your question is currently formatted. Commented Oct 23, 2018 at 13:11
  • I tried to correct your formatting. Please verify that the input and script as shown are are correct. Are your quotes in the script really instead of "? Also, what does I have not had any luck mean? Did you get errors? If so, show them. Commented Oct 23, 2018 at 13:13
  • And don't use cat here. sed can read directly from the source file. Commented Oct 23, 2018 at 15:22

3 Answers 3

1

You need to interpret the content of the sed program, so the calculus you are doing gets processed by bash. Also, you can avoid the extra cat and the printf:

for i in {001..010}; do sed "s/CPRAND/$((1 + RANDOM % 1000))/" me.sh > RunFile${i}.in; done
Sign up to request clarification or add additional context in comments.

Comments

1

This might work for you (GNU sed & parallel):

parallel 'sed 's/CPRAND/'$((1+RANDOM%1000))'/g' >RunFile{}.in' me.sh ::: {000..010}

Comments

0

Because you are using instructions GNU/Bash must interpret, you must use double quotes, instead of single quote, at end, the working instruction can be:

for i in {0..10}
do
   cat me.sh | sed "s/CPRAND/$((1 + RANDOM % 1000))/g" > "RunFile$(printf "%03d" "$i").in"
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.