1

I want to change in several txt-files a variable. But this variable shouldn't be equal in every txt-file. I tried this, but it doesn't work.

for file in ./*.txt ;do
    for value in $(seq 1 5); do
        sed -i 's/x_max=.*/x_max='$value'/ ' $file
    done
done

So every x_max has got the value:5

2
  • 1
    Of course. You run each of the five replacements on each file. So they all end up with the last edit. Commented Mar 19, 2014 at 15:24
  • You are creating a loop before sed runs incrementing at every iteration. since the file is being over-written, every file will have x_max=5. Commented Mar 19, 2014 at 15:28

2 Answers 2

1

This should do the trick. Replace each file only once, with a different value each time.

value=1
for file in *.txt; do
  sed -i 's/x_max=.*/x_max='$value'/' $file
  value=$((value + 1))
done
Sign up to request clarification or add additional context in comments.

Comments

0

That should do it - only iterate once and raise the counter by 1 after each run:

counter=1

for file in ./*.txt ;do
    sed -i 's/x_max=.*/x_max='$counter'/ ' $file
    (( counter++ ))
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.