0

I am running my program on Unix computer. It has 300 loop. At each loop, it will generate 4 files, which call 1.txt, 2.txt, 3.txt, 4.txt. And in the next loop, the new files will overwrite the old ones. Now I want these files at the exactly loop1, loop 50, loop 100, and so on. But I could not seat all day to wait and see the running program. Is there any script can do a work as when these .txt files appear in running directory, rename it as a loop counter (first loop's 1.txt file will be 1_1.txt, second loop's 1.txt file will be 2_1.txt and so on...)

  • Is your 'program' already a shell script? If so, it should be easy enough to implement a few additional conditions for the 1st, 50th and 100th invocation. Can you add the script here? – Haxiel Feb 20 at 10:13
  • You have a script that creates the files? Then why not give them the correct name from the script itself? Or, you have some outside process that creates the files and you want to rename them as they appear? Can you edit your question to clarify a bit? – ilkkachu Feb 20 at 10:28
  • Hi everyone, thanks for your comments. My program is written in Fortran but I do not have permission to change the code. – newbie1 Feb 21 at 0:14
0

There are a couple of ways to do this. Which is most efficient will depend on how your script works. If you can edit your question to contain your script or a snippet at least, I'll edit this to reflect a more accurate answer for your situation.

Here's an example for loop that runs 300 times:

for i in {1..300}; do
  echo "test$i" > 1.txt
  echo "test$i" > 2.txt
  echo "test$i" > 3.txt
  echo "test$i" > 4.txt
done

That, as it stands, will overwrite 1-4.txt each loop as you mention your script does.

here's a simple solution:

count=1
for i in {1..300}; do
  echo "test$i" > ${count}_1.txt
  echo "test$i" > ${count}_2.txt
  echo "test$i" > ${count}_3.txt
  echo "test$i" > ${count}_4.txt
  ((count++))
done

This will mean that you will have files named 1_1.txt, to 300_1.txt

  • Thank you for your answer. My program is written in Fortran and I do not have permission to edit the code. So I wonder that can I use the script to do this job. Best regards. – newbie1 Feb 21 at 0:13
  • I'm not sure how you'd expect to achieve this without editing the code to be honest. you could have a script that watches the directory those files are created in constantly, then move them immediately, but that would be a high i/o and very inefficient script for this purpose.. – RobotJohnny Feb 21 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.