3

Assume that I have two sets of files foo_A_uid.bar and foo_B_uid.bar. Each set contains (a known) number of files. Now, I want to process corresponding pairs using some tool (ImageMagick in my case), that is I want to run some_command foo_A_i.bar foo_B_i.bar out_i.bar, where out_i.bar is the result/output when running the tool on the pair foo_A_i.bar and foo_B_i.bar for some fixed i.

My question is how can I batch this process? Some python, bash script?

Clarification: By set of files I mean a list of files, all start with the same initial, then some UID and having the same extension. This hold for both input sets and the output.

5
  • 1
    When you say "sets of files" do you mean text files containing lists of files? So does "foo_A_i.bar" then mean "the ith line in file foo_A_i.bar"? If that's the case you can use the unix "paste" command to merge the two files side-by-side. And if that's not what you mean, I don't understand it. Jan 19, 2012 at 15:22
  • By "set of files" I mean that I have two sets, each contains the same number of files. Each element of either of the sets is a file - .png in my case.
    – Dror
    Jan 19, 2012 at 15:35
  • How long are those files? Will both lists of names easily fit into RAM? Are there filenames containing blanks and other, unusual characters like tab, line feed, page feed and so on? Jan 19, 2012 at 16:25
  • The files in my case are binaries...
    – Dror
    Jan 19, 2012 at 19:03
  • I was confused because it's not very clear from 'foo bar' style names you chose whether 'foo_A_uid.bar' was a text file containing file names (which I assume it is) or, for example, a directory containing the files. It's much clearer to use a name like 'file_list_A.txt' for a "set" and 'image_A_01.png" for a target file. Jan 20, 2012 at 1:21

2 Answers 2

1

Here's a possible solution, which works, although I don't know if it's a good one.

#!/bin/bash

EXPECTED_ARGS=3
E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]
then
    echo Not the right number of arguments
    exit $E_BADARGS
fi

for i in $( ls $1* ); do
    TMP=$(echo $2$(echo $i | cut -d'_' -f2))
    TMP2=$(echo $3$(echo $i | cut -d'_' -f2))
    convert $i $TMP +append $TMP2
done

Running it should be something like:

script foo_A_ foo_B_ out_
0
#!/bin/bash
count=$(wc -l foo_A_uid.bar)
for line in $(seq 1 ${count/ */})
do
    aline=$(sed -n $line"p" foo_A_uid.bar)
    bline=$(sed -n $line"p" foo_A_uid.bar)
    yourcommand "$aline" "$bline"
done

Assuming both files are sorted, the count-line get's the number of lines in file a. Then a loop runs over all values from 1..count. The count-variable contains the name of the file, which is cut off with ${count/ */}. With sed, you get the same line from both files. -n means 'no printing by default', $line"p" will then just print line $line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .