Need help on parsing string

String example:

/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx

What I would like to do is create a variable and save gistst in it.

I thought if I could create an array and split it by '/'

then I could use the 4th array element or if they was a way to do a substring porocess.

Thing is I do not know any unix shell scripting. I've been able to get most of my script to work but I'm having trouble with this.

Can anyone help me out.

Thanks

var=$( echo "/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx" | cut -d"/" -f5 )
$ y="/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx"
$ x=${y%/*}
$ mm=${x##*/}
$ echo $mm
gistst
x="/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx"
a=$( echo $x | sed 's/\(.*\)\/\(.*\)\/\(.*$\)/\2/' )
echo $a

I got this message back when I tried your example,

cut: you must specify a list of bytes, characters, or fields
Try `cut --help' for more information.
test.sh: line 2: -d/: No such file or directory

Thanks

Your example worked like a charm. Thanks a million! :slight_smile:

Can you show test.sh?

x=` echo '/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx'|awk -F"/" '{print $5}'`
echo $x

I suppose,

you had included the command as below

echo "/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx" | cut -d/

If so, following is the expected output

cut: you must specify a list of bytes, characters, or fields
Try `cut --help' for more information.

#!/bin/sh

var=$( echo "/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx" | cut
-d"/" -f5 )

Fixed it and it does work. I made my console screen wider and it had extra return charcters.

#!/bin/sh

var=$( echo "/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx" | cut -d"/" -f5 )
echo $var

[root@esxtst1 scripts]# sh test.sh
gistst

Thanks a million... :slight_smile:

Thanks for your help.

if you have Python,here's an alternative

# path="/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx"
# result=`echo $var | python -c "print raw_input().split('/')[-2]"`
# echo $result
gistst
$ echo "/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx"\
> |ruby -e 'puts gets.split("/").values_at(4)'
gistst

Yes, you can, by setting IFS to / and using the positional parameters:

var=/vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx
IFS=/
set -f  ## turn off wildcard expansion
set -- $var
newvar=$5 ## The initial slash represents a field
printf "%s\n" "$newvar"