2

I am new to coding and I have been looking for a way to write a loop that generates multiple files after changing one aspect of a master file.

I've used some examples to get started and can get the code working for changing and creating 1 file, but not the loop. Here's the code:

for i in 'seq 1 6'; do sed 's/ref.txt/ref${i}.txt/g' CPMIR.as > CPMIR${i}.as

In the end, I would like to change ref.txt in the CPMIR.as file to ref1.txt and output a new CPMIR1.as file, ref2.txt and output the CPMIR2.as, ref3.txt and output the CPMIR3.as,.... all the way to 6.

Thanks for your help!

2

2 Answers 2

1

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

seq 6 | parallel "sed 's/ref\.txt/ref'{}'.txt/g' CPMIR.as >CPMIR{}.as"
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you haven't used backticks or better you should use $(..) for $(seq 1 6) ,also, you need to use double-quotes while using variables inside sed.

This should work:

#!/bin/bash

for i in $(seq 1 6)
do
sed "s/ref.txt/ref${i}.txt/" CPMIR.as > CPMIR${i}.as
done

1 Comment

Thanks @user123 I gave it a try, but the code isn't working on my end to create new file that only include the change of ref.txt to the corresponding ref$i.txt any thoughts what else I could try? Open to suggestions for other commands!

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.