1

I want to replace a line in a file called link.j and run a program after it, when the program is done I want to run it again but with a different replacement for the link.j file as in a loop for all the values specified by me.

So for replacing the one line I have done

sed -i '' -e '3 s/0.5/0.1/' link.j

then run

copy.sh

So I replace the value of the third line from 0.5 to 0.1. Now I want to keep doing it but for values ranging from 0.1 to 1.0 spaced out 0.1 apart, so 0.1,0.2...1.0, and always running the code after.

The code, that I run after, copies the file which I just modified into a new subfolder, so I would like to save these folders before I would run the substitution again.

The copy.sh script looks like this

for dir in `ls -d ../files/files-12/??`
do

id=`echo $dir | awk -F/ '{print $4}'`
mkdir $id

cp ../stack/*/${id}/rf* .
done
2
  • What does copy.sh look like? Maybe there are simplifications possible across the script and the sed command. Commented Oct 16, 2020 at 14:17
  • Sorry for the late response. Answering your question, the copy.sh is just a small script in which I copy files from folders above into this folder with several subfolders. I will edit it in Commented Oct 21, 2020 at 11:14

1 Answer 1

1

You could loop over the possible replacement strings like this:

for n in 0.{1..9} 1.0; do
    sed -i '' -e "3 s/0\.5/$n/" link.j
    ./copy.sh
done

Notice that I've escaped the period in 0.5; otherwise, it would match any character.

Brace expansion is a Bashism; if you don't use Bash, you might have seq (also non-POSIX):

for n in $(seq 0.1 0.1 1.0); do

and for POSIX compliance, you could use awk:

for n in $(awk 'BEGIN { for (i = 0.1; i <= 1.0; i += 0.1) print i }'); do
Sign up to request clarification or add additional context in comments.

2 Comments

I have a problem with this script which you provided which is my fault since I did not show the copy.sh first. But This way the link.j file only stays with the 0.1 value and furthermore it does not save the file afterwards. My thought would be that I would run the script, it would generate a link.j file which would be used by the copy.sh script, afterwards I would run this again but save it on another folder.
Otherwise I would just manually substitute the 0.5 individually and store the folders elsewhere

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.