Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script regex matching

In my bash script, I have an array of filenames like

files=( "site_hello.xml" "site_test.xml" "site_live.xml" )

I need to extract the characters between the underscore and the .xml extension so that I can loop through them for use in a function.

If this were python, I might use something like

re.match("site_(.*)\.xml")

and then extract the first matched group.

Unfortunately this project needs to be in bash, so -- How can I do this kind of thing in a bash script? I'm not very good with grep or sed or awk.

like image 261
Barry Rosenberg Avatar asked Jun 04 '26 02:06

Barry Rosenberg


2 Answers

Something like the following should work

files2=(${files[@]#site_})   #Strip the leading site_ from each element
files3=(${files2[@]%.xml})    #Strip the trailing .xml

EDIT: After correcting those two typos, it does seem to work :)

like image 55
jkerian Avatar answered Jun 05 '26 17:06

jkerian


xbraer@NO01601 ~
$ VAR=`echo "site_hello.xml" | sed -e 's/.*_\(.*\)\.xml/\1/g'`

xbraer@NO01601 ~
$ echo $VAR
hello

xbraer@NO01601 ~
$

Does this answer your question?

Just run the variables through sed in backticks (``)

I don't remember the array syntax in bash, but I guess you know that well enough yourself, if you're programming bash ;)

If it's unclear, dont hesitate to ask again. :)

like image 34
Erik A. Brandstadmoen Avatar answered Jun 05 '26 15:06

Erik A. Brandstadmoen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!