0

I am trying to run a command which requires two files from a pair to be separately specified (Left and Right). Each pair of files has a different basename followed by .1 for left or .2 for right (e.g. A.1.txt, A.2.txt, B.1.txt, B.2.txt). There are over 100 such pairs, so I'd like to call them in a loop, rather than writing out the command >100x, and the basenames are all unique and long, so I'd rather not write them all out, just iterate over every pair in the directory.

Here's what I've been trying, which doesn't work:

*for file in /*.1.txt

echo $(basename "$file") | sed 's/.1.txt/.2.txt/' > file2

do command -left "$file" -right "$file2"

done*

Any suggestions very much appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alison
  • 1
  • 2
  • Possible duplicate of [Looping over pairs of values in bash](https://stackoverflow.com/questions/28725333/looping-over-pairs-of-values-in-bash) – tripleee Jan 17 '18 at 13:21
  • Similar to the above, but the given answer there writes out the basenames for each file, which isn't practical here. – Alison Jan 17 '18 at 13:43
  • There are several answers there which explain how to loop over pairs of values. If there is some detail of that which you cannot figure out how to adapt to your needs, maybe update your question to explain precisely where you are stuck. Your concrete problem appears to be that you are writing the new file name into a file named `file2` rather than capturing it in a variable with that name, but the duplicate should show you how to do that, too. – tripleee Jan 17 '18 at 13:46

1 Answers1

0

How about this?

for file in *.1.txt; do                                                                   
   file2=$(echo ${file} | sed 's/\.1/\.2/')                                               
   command -left "${file}" -right "${file2}"                                                                                                                                                            
done
Misantorp
  • 2,606
  • 1
  • 10
  • 18