Monday, May 18, 2015

List files that match a pattern and do not match another pattern

 If you want to list files that match certain pattern use ls and grep in a pipe:
  
ls | grep '.vcf$'

1410UNHS-0007_2108_1_SNP_INDEL.vcf
1410UNHS-0007_2305_3_SNP_INDEL.vcf
1411KHX-0032_1522-2_SNP_INDEL.vcf
1411KHX-0032_2356-3_SNP_INDEL.vcf
1503UNHX-0001_0020805_4_SNP_INDEL.vcf
1503UNHX-0001_200212_2_SNP_INDEL.vcf
1503UNHX-0001_2510_4_SNP_INDEL.vcf
1503UNHX-0001_2989_3_SNP_INDEL.vcf
1503UNHX-0001_CA1346_SNP_INDEL.vcf
1503UNHX-0001_CA1508_SNP_INDEL.vcf
new_1410UNHS-0007_2108_1_SNP_INDEL.vcf
new_1410UNHS-0007_2305_3_SNP_INDEL.vcf
new_1411KHX-0032_1522-2_SNP_INDEL.vcf
new_1411KHX-0032_2356-3_SNP_INDEL.vcf
new_1503UNHX-0001_0020805_4_SNP_INDEL.vcf
new_1503UNHX-0001_200212_2_SNP_INDEL.vcf
new_1503UNHX-0001_2510_4_SNP_INDEL.vcf
new_1503UNHX-0001_2989_3_SNP_INDEL.vcf
new_1503UNHX-0001_CA1346_SNP_INDEL.vcf
new_1503UNHX-0001_CA1508_SNP_INDEL.vcf


But now if you want to exclude from this list those files that contain "new" at the beginning use grep -v

ls | grep '.vcf$' | grep -v '^new'

1410UNHS-0007_2108_1_SNP_INDEL.vcf
1410UNHS-0007_2305_3_SNP_INDEL.vcf
1411KHX-0032_1522-2_SNP_INDEL.vcf
1411KHX-0032_2356-3_SNP_INDEL.vcf
1503UNHX-0001_0020805_4_SNP_INDEL.vcf
1503UNHX-0001_200212_2_SNP_INDEL.vcf
1503UNHX-0001_2510_4_SNP_INDEL.vcf
1503UNHX-0001_2989_3_SNP_INDEL.vcf
1503UNHX-0001_CA1346_SNP_INDEL.vcf
1503UNHX-0001_CA1508_SNP_INDEL.vcf

No comments:

Post a Comment