Extract a substring using SED/AWK

Hi All,

I have a log file in which name and version of applications are coming in the following format
name[version]

It may look like following, based on the name of the application and version:

XYZ[1.2.3] OR xyz[1.2.3]  OR XyZ[1.2.3] OR xyz[1.2]

I want to separate out the name and version and store them into variables. Like,

name=xyz
version=1.2.3

I am using SED but no luck. I am not that well versed in AWK. Could you please help me out here?

Thanks in advance,
Bhaskar

Hi

$ x="XYZ[1.2.3]"
$ name=`echo $x | awk -F "[][]" '{print $1}'`
$ version=`echo $x | awk -F "[][]" '{print $2}'`

$ echo $name
XYZ
$ echo $version
1.2.3

Guru.

Try

echo "XyZ[1.2.3]"|sed 's|\(.*\)\[\(.*\)\]|\1 \2|g'|read name version
1 Like
echo "XYZ[1.2.3]" | perl -ne '(/(.+?)\[(.+?)\]/) && print "name=$1\nversion=$2\n"'
1 Like

Hi Guru and Elixir,

Thanks a lot to both of you. It worked.

Cheers,
Bhaskar