Home > Software design >  making reference to file two by two in a bash command
making reference to file two by two in a bash command

Time:02-10

I have this list of file that I have to analyse by pair (the a_1 with a_2, b_1 with b_2 and so on)

a_1.fq
a_2.fq
b_1.fq
b_2.fq
c_1.fq
...

I want to set a for loop to make reference to these pairs of file in a command, bellow this is just an example of what I want to do (with a false syntax) :

$ for File1 File2 in *1.fq *2.fq; do STAR --readFilein File1 File2 ; done

Thank you a lot for your help

CodePudding user response:

Use a function to process pairs and iterate the glob expansion:

process_pair()
{
  while [ $# -gt 0 ]
  do
    f1=$1 # get 1st argument
    shift # shift to next argument
    f2=$1 # get 2nd argument
    shift # shift to next for the next round

    # Do stuffs with file1 and file2
    printf 'f1=%s\tf2=%s\n' "$f1" "$f2"
  done
}

# Submit pattern expansion as process_pair arguments
process_pair ./*_[12].fq

CodePudding user response:

You can iterate over just one type of the files and use parameter expansion to device the second one:

for file1 in *1.fq; do
    file2=${file1%1.fq}2.fq
    ...

The %pattern removes the pattern from the end of the variable's value.

You might want to check for the file2's existence before running the command.

Or, if you can get the files listed in the way the pairs are adjacent, you can populate $@ with them and shift the parameters by two:

set -- [a-z]_[12].fq
while (( $# )) ; do
    file1=$1
    file2=$2
    shift 2
    ...
done

CodePudding user response:

You can try something like that:

for letter in {a..z}
do
 # Your logic
  echo "Working on $letter"
  cat $letter\_1.fq
  cat $letter\_2.fq
done
  • Related