胡海飞 胡海飞 - 4 months ago 9
Linux Question

How to loop over two different pattern files in linux?

I got two pattern of files one is like XXX_01.fastq and another is XXX_01_001.fastq

I am going to write a for loop to loop over all these files with different pattern. Any help about it?

Answer

Contents of list.txt:

$ cat list.txt
AAA_01.fastq
AA_01_001.fastq
BBB_01_002.fastq
BBB_02.fastq

Example using bash pattern matching:

for file in `cat list.txt`; do
  if [[ $file =~ [A-Z]{3}_[0-9]{2}\.fastq || $file =~ [A-Z]{3}_[0-9]{2}_[0-9]{3}\.fastq ]]; then
    echo "MATCH $file";
  fi;
done

Output:

MATCH: AAA_01.fastq
MATCH: BBB_01_002.fastq
MATCH: BBB_02.fastq