1

I have files under a common folder which contain numbers as a part of them. I am trying to access these files serially through perl command executed via bash script. When I try to obtain filenames based on serial numbers in filenames it does not work. How can I resolve this.

Example filenames:

test_S1_L001.sorted.bam
abc_S2_L001.sorted.bam
cdf_S3_L001.sorted.bam

I am trying to extract individual filenames using following command in bash

script.pl $mapFolder/*S{$id}_L001.sorted.bam 

Where, $id is numbers after S e.g 1, 2 ,3 provided from the bash script.

mainScript.sh 1
mainScript.sh 2
mainScript.sh 3

However this command is giving me error below

failed to open "mapFolder/*S1_L001.sorted.bam" for reading: No such file or directory

It should ideally take full file name for the perl script as

script.pl mapFolder/test_S1_L001.sorted.bam
Callie
  • 333
  • 1
  • 9
  • Are you sure `mapFolder` is a subdirectory of your current directory? Try setting `$mapFolder` to an absolute path. – Barmar May 18 '23 at 20:59
  • Yes I have set mapFolder to an absolute path. – Callie May 18 '23 at 21:08
  • Then it should work. The wildcard should be expanded if there are any matching files. – Barmar May 18 '23 at 21:11
  • 3
    If `id` = 1, then `{$id}` becomes `{1}` not `1` – jhnc May 18 '23 at 21:34
  • 1
    maybe you meant `${id}` ? Good luck. – shellter May 18 '23 at 23:20
  • Regarding "expand on a regex..." - shells don't use regexps to find files, they use globbing patterns. Those look and function a bit like regexps, but they have their own syntax and semantics and definitely are not regexps. – Ed Morton May 18 '23 at 23:36
  • I find the title of your question confusing, since there is no regex anywhere in your posting. Aside from this, the error message comes from your Perl program, and if you claim that the file indeed is there, you better provide a minimal Perl example program, which shows how you open the file. – user1934428 May 19 '23 at 07:03
  • Not also that `{$id}` does not make sense. bash does not perform brace expansion, unless there is at least one comma inside the brace (such as in `{$id1,$id2}`. In your case, if `id` is, say, `1`, `{$id}` would expand to `{1}`. You can catch this type of error if you, during debugging of your script, turn on tracing with `set -x`. – user1934428 May 19 '23 at 07:06

1 Answers1

0

I used script.pl $mapFolder/*S${id}*_L001.sorted.bam which seems to work.

Callie
  • 333
  • 1
  • 9
  • This just a typo, then, putting the `$` in the wrong place? Or was there anything else to it? (Questions caused by typographic errors are categorically off-topic). – Charles Duffy May 18 '23 at 22:24
  • (btw, there's no regex in this code at all; what you have here is a glob expression, not a regex) – Charles Duffy May 18 '23 at 22:27
  • There is a wild card * before and after from *S${id} to *S${id}* – Callie May 18 '23 at 22:27
  • 1
    Having a wild card does not make it a regexp. It's a globbing pattern. See [what-are-the-differences-between-glob-style-patterns-and-regular-expressions](https://stackoverflow.com/questions/23702202/what-are-the-differences-between-glob-style-patterns-and-regular-expressions) and google for other definitions/examples. – Ed Morton May 18 '23 at 23:38
  • In a regexp, `*` is not a wildcard but instead a modifier that says "zero or more of the immediately prior token". So if this _were_ regex syntax, it wouldn't do what you intend; it's only glob syntax where `*` is a wildcard. (If you've seen people write `.*`, that's the regex equivalent to the glob `*`: the `.` means "one character that can be anything", and the `*` modifies it to "zero or more characters that can be anything") – Charles Duffy May 19 '23 at 11:49