Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash – regex trim untill symbol

trying to trim untill and including symbol " – " .
for some reason, when i add ^(.*?) bash will not modify the varieble.
GNU bash, version 5.1.16(0)-release (amd64-portbld-freebsd12.2)

fname="text1 - text2.jpg"
outp="${fname//^(.*?)\s-\s/}"
echo $outp

expected result: text2.jpg
any suggestions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You appear to be trying to use a regexp (specifically a PCRE or a BRE or ERE with undefined behavior) instead of a globbing pattern in your command. See https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html.

Is this what you’re trying to do?

$ echo "${fname/* - }"
text2.jpg

$ echo "${fname#* - }"
text2.jpg

$ echo "${fname/*[[:space:]]-[[:space:]]}"
text2.jpg
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading