I have a srcipt that I pass a path into that will consist of 5 or 6 elements (/1/2/3/4/5/6). I want to always pull the third element out of the path name. How can i do this?
Thanks
I have a srcipt that I pass a path into that will consist of 5 or 6 elements (/1/2/3/4/5/6). I want to always pull the third element out of the path name. How can i do this?
Thanks
OLDIFS=$IFS; IFS=/; set -- $path; IFS=$OLDIFS
echo "$4" # because the first element $1 will be empty
# echo "$1" | sed 's/\// /g' | awk '{print $3}'
Actually, if by "pull out" you mean "remove" rather than "extract" (as per the topic title), do as above, then glue back together all except the one you don't want. Something like
p=
n=
s=
while true
do
case $# in 0) break;; esac
i=$1
shift
# skip $4
n=x$n
case $n in xxxx) continue;; esac
p="$p$s$i"
s=/
done
echo "$p"
That got messier than I thought, but it uses no external programs.