Home > OS >  Unix/bash. Two for loops in parallel for file names with numbers
Unix/bash. Two for loops in parallel for file names with numbers

Time:08-26

I have a directory with many files with similar names like these:

file_0.5_1.5_xxxx.txt
file_1.5_2.5_xxxx.txt
file_2.5_3.5_xxxx.txt
file_3.5_4.5_xxxx.txt
...
file_1000.5_1001.5_xxxx.txt

Using unix/bash, I want to perform some actions in each of them and then output files with similar names like:

file_0.5_1.5_xxxx2.txt
file_1.5_2.5_xxxx2.txt
file_2.5_3.5_xxxx2.txt
file_3.5_4.5_xxxx2.txt
...
file_1000.5_1001.5_xxxx2.txt

I am thinking in doing a for loop like this (below), but I am having trouble with the expression "{i 1}". There must be some simple way of doing it, but I am not able to find how:

for i in {0..1000}; do
grep house file_${i}.5_${i 1}.5_xxxx.txt > file_${i}.5_${i 1}.5_xxxx2.txt;
done

Thank you !

CodePudding user response:

You may use it like this:

for i in {0..1000}; do
   fn="file_${i}.5_$((i 1)).5_xxxx.txt"
   grep -F 'house' "$fn" > "${fn/.txt/2.txt}"
done
  • Related