Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign part of a file name to bash variable?

Tags:

bash

shell

I have a file and its name looks like:

12U12345._L001_R1_001.fastq.gz

I want to assign to a variable just the 12U12345 part.

So far I have:

variable=`basename $fastq | sed {s'/_S[0-9]*_L001_R1_001.fastq.gz//'}`

Note: $fastq is a variable with the full path to the file in it.

This solution currently returns the full file name, any ideas how to get this right?

like image 258
SaltedPork Avatar asked Jan 02 '23 15:01

SaltedPork


2 Answers

Just use the built-in parameter expansion provided by the shell, instead of spawning a separate process

fastq="12U12345._L001_R1_001.fastq.gz"
printf '%s\n' "${fastq%%.*}"
12U12345

or use printf() itself to store to a new variable in one-shot

printf -v numericPart '%s' "${fastq%%.*}"
printf '%s\n' "${numericPart}"

Also bash has a built-in regular expression comparison operator, represented by =~ using which you could do

fastq="12U12345._L001_R1_001.fastq.gz"
regex='^([[:alnum:]]+)\.(.*)'

if [[ $fastq =~ $regex ]]; then
    numericPart="${BASH_REMATCH[1]}"
    printf '%s\n' "${numericPart}"
fi
like image 76
Inian Avatar answered Jan 05 '23 06:01

Inian


You could use cut:

$> fastq="/path/to/12U12345._L001_R1_001.fastq.gz"
$> variable=$(basename "$fastq" | cut -d '.' -f 1)
$> echo "$variable"
12U12345

Also, please note that:

  • It's better to wrap your variable inside quotes. Otherwise you command won't work with filenames that contain space(s).

  • You should use $() instead of the backticks.

like image 29
Ronan Boiteau Avatar answered Jan 05 '23 04:01

Ronan Boiteau