0

I want to iterate over different file names. The file names are XY1.txt, XY2.txt...XY500.txt. Each file that is taken as input generates 3 output files which I want to rename according to the current index. For XY1.txt, output1.txt becomes 1_1.txt, output2.txt becomes 1_2.txt and so on. But I do not know how to change the filename as per index. I looked it up but I was not finding what I needed on this website or elsewhere. I am new to bash. The below pseudo code is what I was able to write until now. Please help me out

    for i in {1..500} ; 
         filename = "XY"+str(i) + ".txt"  ;
         do ./CompiledCodeFile  -filename -use_option 4; 
         mv output1.txt str(i)+"_1.txt"   ;
         mv output2.txt str(i)+"_2.txt"   ;
         mv output3.txt str(i)+"_3.txt"   ;
    done
2
  • Wow, there's a lot of things that are wrong, let me correct it. Commented Nov 26, 2021 at 9:12
  • Edit: Removed the typos Commented Nov 26, 2021 at 9:14

2 Answers 2

2
#!/bin/bash

for i in {1..500}
do
    filename="XY$i.txt"
    ./CompiledCodeFile  "$filename" -use_option 4
     mv output1.txt "${i}_1.txt"
     mv output2.txt "${i}_2.txt"
     mv output3.txt "${i}_3.txt"
done

notes:

  • $i and ${i}, what's the difference?

They're the same thing, but sometimes you need to use the ${} for differentiating the variable name from the following letters (here the _)

  • Why use double quotes for expanding variables, for example "$filename"?

Because that's almost mandatory. Not using double quotes is the cause of a lot of errors, while using double quotes can only be wrong for a very few cases (the only ones that I can think of is [[ xyz == $regexp ]] and the likes).
edited following @MikaelKjær comment.

Sign up to request clarification or add additional context in comments.

6 Comments

I saw some Bash scripts for other uses where they use a semi colon. Why is that not necessary here ?
You can use a semi-colon OR a newline for separating commands
@MikaelKjær : ShauryaGoyal is a new bash user, so I can't possibly not use double quotes in my answer, even if it is not mandatory in his specific case...
To be clear, I'm not criticizing you using quotes - I encourage it. It's the note I'm criticizing since it's just plain wrong. Your answer would be better without the second note.
@MikaelKjær : Well, my initial note was like a conclusion without prior explanations. Is it better after the edit?
|
1
for i in `seq 500`; do
mv output${i}.txt ${i}_${i}.txt
done
  1. use seq function to generate integer from 1 to 500
  2. In bash, the type of variable is string in default.

2 Comments

seq is not a function. It is an external application, though not specified by POSIX, a common utility. It is superfluous to spawn an external application to do some counting and then pass that application's output to bash for word splitting. In bash, C-style for loops are the best method for implementing a counter: for ((i=1; i<=500; i++))
And the script doesn't do what the OP asked for.

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.