Skip to content
Advertisement

Looping over pairs of files

Hello I need to iterate over pairs of files and do something with them.

For example I have 4 files which are named AA2234_1.fastq.gz AA2234_2.fastq.gz AA3945_1.fastq.gz AA3945_2.fastq.gz

As you can propably tell the pairs are AA2234_1.fastq.gz <-> AA2234_2.fastq.gz and AA3945_1.fastq.gz <-> AA3945_2.fastq.gz (they share the name before _ sign)

I have a command with syntax looking like this:

initialize_of_command file1 file2 output_a output_b output_c output_d parameteres

I want this script to find the number of files with fastq.gz extension in a directory, divide them by 2 to find number of pairs then match the pairs together using probably regex (maybe to two variables) and execute this command for each pair once.

I have no idea how to pair up those files using regex and how to iterate over the pairs so the scripts knows through which pairs it have already iterated.

Here is my unfinished script:

JavaScript

Also I would like for the output names to be named after the shared name of input files which in this case is AA2234 and AA3945

The desired output of this script should be 8 files named accordingly to pairs:

JavaScript

and

JavaScript

Advertisement

Answer

Assuming the filenames do not contain whitespace, would you please try:

JavaScript

The script outputs the java command lines as a dry run. If the output looks good, drop echo and run.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement