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

How to loop over a list containing two different pattern files in linux (bash)?

I got a list containing filenames that match the following two patterns:


  • one is like XXX_01.fastq

  • another is XXX_01_001.fastq



I am going to write a
for
loop (in bash) to loop over all the filenames with different patterns and I need to determine which ones match the patterns above. 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