Regex - excerpt before the match and not counting this match

I am trying to capture file names by deleting both the file extension and the suffix, for example:

TEST_EXAMPLE_SUFFIX.file
Output = TEST_EXAMPLE

I want to do this based on matching the _SUFFIX part and extracting all the characters before that (not counting _SUFFIX). Normally I would use something like:

FILE_EXT=_SUFFIX
/.+?(?=$FILE_EXT)/

However, when pipelines that together are part of the for loop:

for t in $(ls *.fastq | sed -e /.+?(?=$READ1_EXT)/)

I get an error message:

command substitution: line 14: syntax error near unexpected token `('

What did I do wrong?

+4
source share
2 answers

Do not parse the output ls, you can use the bash parameter extension to achieve what you need

for t in *_SUFFIX.fastq 
do
  echo "${t%_SUFFIX.fastq}" #stips _SUFFIX.fastq part
done

References


Edit:

- :

, Test_R1.file Test_R2.file, , Test , -

declare -A arry # declaring an associative array
for t in Test_R*.file
do
  arry["${t%_R*.file}"]=1 
  # stips _R(number).file part and makes it a key to arry
  # Remember arry keys are unique.
  # The assignment ie '=1' is not relevant here, you can assign any value
done
# We are all set to print the unique filenames
echo "${!arry[@]}"
# "${!arry[@]}" expands to the list of array indices (keys) for arry
+5

, bash, :

for file in *_SUFFIX.fastq; do echo "${file%_*}"; done

for .fastq.

:

$ file=TEST_EXAMPLE_SUFFIX.fastq

$ echo "${file%_*}"
TEST_EXAMPLE
+1

Source: https://habr.com/ru/post/1652863/


All Articles