/data}/{bla/${INSTANCE} or /data}/{bla/$INSTANCE
the idea is to match a env varialbe name, so it could be between ${ } or a single $
Perl can do it with: perl -ne '/\$\{?(\w+)\}?/;print $1'
What shell can do?
/data}/{bla/${INSTANCE} or /data}/{bla/$INSTANCE
the idea is to match a env varialbe name, so it could be between ${ } or a single $
Perl can do it with: perl -ne '/\$\{?(\w+)\}?/;print $1'
What shell can do?
Where is the string you wnat to match? Is it in a file or is it already in a variable?
If it's in a variable:
case $var in
*\${INSTANCE}*| *\$INSTANCE* )
echo MATCH;;
*) echo NO MATCH ;;
esac
If it's in a file or the output of a command, use grep:
grep -e '${INSTANCE}' -e '$INSTANCE'
I want to return the word between ${} or after $, but I don't know the word name.
example of Perl result
$ echo '/data}/{bla/${INSTANCE}' | perl -ne '/\$\{?(\w+)\}?/;print $1'
INSTANCE
$ echo '/data}/{bla/$INSTANCE' | perl -ne '/\$\{?(\w+)\}?/;print $1'
INSTANCE
You haven't answered the question:
Where is the string you want to match?
[indent]Is it in a file?
Is it already in a variable?
Is it the output of a command?
Are you looking for it in a single line? Or in a file containing more than one line?
[/indent]
It's better if you provide more details but hope this helps:
echo '/data}/{bla/${INSTANCE}' | sed 's/.*${*\([A-Za-z0-9]\{1,\}\)}*.*/\1/'
supposing it is a file has 1 line only
/data}/{bla/${INSTANCE} or /data}/{bla/$INSTANCE
![]()
Thanks the expression worked for both. Can you expain how it worked?
i don't understand usage of "\1"
You can do it entirely in the shell, using no external commands:
read -r line < "$FILE"
var=${line#*$}
var=${var#*{}
var=${var%%[!a-zA-Z_0-9]*}
echo "$var"
Or you can use sed.
sed 's/[^$]*${*\([a-zA-Z_0-9]*\)}*.*/\1/' "$FILE"
Or you can use awk:
awk -F '$' '{
gsub( /[{}]/,"", $2 )
gsub( /[^a-zA-Z_0-9].*/,"", $2)
print $2 }
' "$FILE"
These scripts all find the first variable in the line if there is more than one.
Hmmm...I'm not great at explaining but here goes...
Think of \1, \2, \3, ..., \n as "place-holders". This is where results from your searching are placed.
In sed, you can assign a result to a "place holder" by using \(<regexp>\). If you have 3 \(<regexp>\) then you'll have 3 "place-holders" that is if they have a result...
So the \1 means:
\1 - result of this \([A-Za-z0-9]\{1,\}\).
Aaaarrrrggghhhh!!! I just can't explain it enough, man :D. Check the ff. and I hope they give a more detailed explanation:
http://www.grymoire.com/Unix/Sed.html\#uh-4
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns