Issue
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!
Solution
Using sed
grouping and back referencing
$ sed 's/[^0-9]*\([^/]*\).*/\1/' input_file
21.1R1
/[^0-9]*
- Exclude anything up to the next occurance of a digit character. As this part is not within the parenthesis ()
to be grouped, it will be excluded.
\([^/]*\)
- This will group everything from the first digit up to the next occurance of /
slash.
.*/
- Exclude everything else
\1
- Return the group with backreference \1
.
awk
can also be used.
$ awk -F"[/-]" '{print $6}' input_file
21.1R1
-F"[/-]"
- Set delimiter to /
and -
then print column 6 which will contain the string.
Answered By - HatLess Answer Checked By - Pedro (WPSolving Volunteer)