Hi,
I am new to Unix. I am trying to extract a substring from an input string: Ex -
input string: deploy_v11_9_1
i want to extract and store the value v11_9_1 from the input string in a new variable.
I am using following command in my shell script file:
echo "Enter the folder name u want to access ... ";
read FOLDER;
version = $(echo $FOLDER | awk '{split($0,a,"_v");print a[2]}');
echo $version;
However, I am getting error message that
version: command not found
Could you please help?
Thanks,
Pranav
version = $(echo $FOLDER | awk '{split($0,a,"_v");print a[2]}');
Remove the spaces around the "=" sign:
version=$(echo $FOLDER | awk '{split($0,a,"_v");print a[2]}');
1 Like
We also need to remove '$' symbol and replace it with `.
Following code finally worked:
echo "Enter the folder name u want to access ... ";
read FOLDER;
version=`echo $FOLDER | awk '{split($0,a,"_v");print a[2]}'`
echo "Version is "$version
output:
Enter the folder name u want to access ...
deploy_v11_9_1
Version is 11_9_1
Jotne
February 7, 2013, 7:47am
4
version=`echo $FOLDER | awk '{split($0,a,"_v");print a[2]}'`
Here you also have removed the ;
at the end compare to post above.
Try this and see if it works
version=$(echo $FOLDER | awk '{split($0,a,"_v");print a[2]}')
Its more easy to read a code using $(code)
compare `code`
It may be shorten some to:
version=$(awk '{split($0,a,"_v");print a[2]}' <<< $FOLDER)
I do also not see why you split $0
This may also work
version=$(awk -F"_v" '{print $2}' <<< $FOLDER)
1 Like
pamu
February 7, 2013, 7:48am
5
As per your given input your final output is highlighted above.
If you want v in the final output then use sth like this..
And instead of using split just us FS for the same
$echo "deploy_v11_9_1" | awk -F "y_" '{ print $2}'
v11_9_1
or if you want only numbers then..
$echo "deploy_v11_9_1" | awk -F "_v" '{ print $2}'
11_9_1
and please look this..
$ version=`echo "deploy_v11_9_1" | awk -F "_v" '{ print $2}'`
$ echo $version
11_9_1
$ version=$(echo "deploy_v11_9_1" | awk -F "_v" '{ print $2}')
$ echo $version
11_9_1
Both $()
and ``
are used for the same but $()
is preferred over backticks.
1 Like
Thanks all for ur valuable suggestions