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. bash-4.1$ echo "THISPREFIX_blahblah_yyymmdd.txt" THISPREFIX_blahblah_yyymmdd.txt bash-4.1$ var123="THISPREFIX_blahblah_yyymmdd.txt"; file_prefix=" ${var123%%_*} " ; echo $file_prefix THISPREFIX bash-4.1$ 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 _. 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_bla
Comments
Post a Comment