Sometime you are processing a lot files and you have to construct a dynamic prefix and suffix based file names. So you can search for multiple files based on prefix pattern and suffix pattern.
Here's how you extract the PREFIX of a string given you have some sort of delimiter in the string itself.
So for the SUFFIX extraction the patter is similar only we use ## instead of %%.
see below :
Here's how you extract the PREFIX of a string given you have some sort of delimiter in the string itself.
In the above example string what we want to extract are the charcters after the _ (underscore) thus in our variable manipulation we specified _ after the %% which denotes that we get all the characters before _.bash-4.1$ echo "THISPREFIX_blahblah_yyymmdd.txt"THISPREFIX_blahblah_yyymmdd.txtbash-4.1$ var123="THISPREFIX_blahblah_yyymmdd.txt"; file_prefix="${var123%%_*}" ; echo $file_prefixTHISPREFIXbash-4.1$
So for the SUFFIX extraction the patter is similar only we use ## instead of %%.
see below :
bash-4.1$ echo "THISPREFIX_blahblah_yyymmdd.txt"
THISPREFIX_blahblah_yyymmdd.txt
bash-4.1$ var123="THISPREFIX_blahblah_yyymmdd.txt"; file_suffix="${var123##*.}" ; echo $file_suffix
txt
bash-4.1$
Comments
Post a Comment