Given
DIRNAME_MAIN="/home/vMX-ENV/vMX-21.1R1/"
I would like to ONLY display 21.1R1
from the directory above, Is there any way I can only display value after home/vMX-ENV/vMX-
?? and remove the /
at the end so instead of being 21.1R1/
it will end up being: 21.1R1
?
Thanks!
CodePudding user response:
Bash has a fairly wide variety of built-in mechanisms for manipulating variables' values. Of particular interest for the present problem are parameter expansion forms that remove prefixes or suffixes that match specified shell patterns. For example:
# Remove any trailing slash and store the result in DIRNAME_NORM
DIRNAME_NORM=${DIRNAME_MAIN%/}
# Emit the value of $DIRNAME_NORM, less the longest prefix matching shell
# pattern *vMX-
echo "${DIRNAME_NORM##*vMX-}"
There is no need to rely on an external program for this case.
CodePudding user response:
Using sed
$ sed 's/[^0-9]*\([^/]*\).*/\1/' input_file
21.1R1