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!

Cae.rich
  • 171
  • 7
  • 1
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Oct 17 '18 at 02:57
  • Please check: [expand unix variable inside sed command](https://stackoverflow.com/questions/52550812/expand-unix-variable-inside-sed-command/52551728#52551728) – User123 Oct 17 '18 at 04:04

2 Answers2

1

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

seq 6 | parallel "sed 's/ref\.txt/ref'{}'.txt/g' CPMIR.as >CPMIR{}.as"
potong
  • 55,640
  • 6
  • 51
  • 83
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
User123
  • 1,498
  • 2
  • 12
  • 26
  • 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! – Cae.rich Oct 17 '18 at 04:39