user1197252
user1197252

Reputation:

Picking values from a variable

I have a variable in my shell script that looks like this:

DIR="HOME_X_Y_Z";

It represents a directory name. Imagine I have multiple directories all named something like this:

HOME_1_2_Z/
HOME_9_A_Z/
HOME_3_5_Z/
etc...

The only values that change are those located at position X and Y. Is there a regular expression I could use (maybe with sed or awk) to peel those values out? I want to be able to used them independently elsewhere in my script.

So I would be able have 2 more variables:

X_VALUE="";
Y_VALUE=""; 

Thanks

Upvotes: 0

Views: 114

Answers (3)

Aleks-Daniel Jakimenko-A.
Aleks-Daniel Jakimenko-A.

Reputation: 10653

This is very easy with pure bash:

IFS='_' read X_VALUE Y_VALUE Z_VALUE <<< "${DIR#*_}"

Explanation:
So, ${DIR#*_} is going to remove the shortest match from the beggining of the string. See parameter expansion cheat sheet for more stuff like that.

echo ${DIR#*_} # returns X_Y_Z

After that all we have to do is change the default IFS to _ and read our values.

Upvotes: 1

kojiro
kojiro

Reputation: 77107

Set IFS and use read:

IFS=_ read home x y z rest <<< 'HOME_X_Y_Z'
echo "$y" # "Y"
echo "$z" # "Z"
echo "$x" # "X"

Upvotes: 4

Jonathan Wakely
Jonathan Wakely

Reputation: 171303

If you don't want to fiddle with IFS you can still do it in pure Bash by stripping off the parts you don't want, but it's not a one-liner:

DIR2=${DIR#HOME_}
DIR2=${DIR2%_Z}      # $DIR now contains just X_Y
X_VALUE=${DIR2%_*}
Y_VALUE=${DIR2#*_}

Upvotes: 1

Related Questions