Reputation: 89
I am trying to use one or two lines of Bash (that can be run in a command line) to read a folder-name and return the version inside of the name.
So if I have myfolder_v1.0.13
I know that I can use echo "myfolder_v1.0.13" | awk -F"v" '{ print $2 }'
and it will return with 1.0.13
.
But how do I get the shell to read the folder name and pipe with the awk
command to give me the same result without using echo
? I suppose I could always navigate to the directory and translate the output of pwd
into a variable somehow?
Thanks in advance.
Edit: As soon as I asked I figured it out. I can use
result=${PWD##*/}; echo $result | awk -F"v" '{ print $2 }'
and it gives me what I want. I will leave this question up for others to reference unless someone wants me to take it down.
Upvotes: 1
Views: 207
Reputation: 85560
But you don't need an Awk
at all, here just use bash
parameter expansion.
string="myfolder_v1.0.13"
printf "%s\n" "${string##*v}"
1.0.13
Upvotes: 1
Reputation: 121
You can use
basename "$(cd "foldername" ; pwd )" | awk -Fv '{print $2}'
to get the shell to give you the directory name, but if you really want to use the shell, you could also avoid the use of awk completetly: Assuming you have the path to the folder with the version number in the parameter "FOLDERNAME":
echo "${FOLDERNAME##*v}"
This removes the longest prefix matching the glob expression "*v" in the value of the parameter FOLDERNAME.
Upvotes: 1