Need to split a string

Hi,

We have a SunOS 5.10 Generic_142900-13 sun4v sparc SUNW,T5240.

I'm trying to find a way to split a string into 2 variables.

Ex:
parm1="192.168.1.101/parent/child"

What I need to do is split the string above into:

host="192.168.1.101"
location="parent/child"

I saw the solution provided here http://www.unix.com/shell-programming-scripting/26190-finding-character-position-file.html but somehow we don't have the -o option for grep as I keep getting illegal option --o.

The purpose of this is for a shell script already created to do an FTP function. Originally it was intended for accepting HOST, USER, PASS and FILE parameters only. Basically file goes to whatever the main directory on FTP connect. Now users are passing HOST/folder as value for HOST parameter which gives out an error now since 192.168.1.101/parent/child is not a valid HOST. And it would be a lot of work to add another parameter for LOCATION because a lot of other programs are already using this script so I'm trying to avoid this solution for now.

I'd appreciate any suggestions.

Thanks.

host=${parm1%%\/*}
location=${parm1#*\/}
1 Like

Thanks.

I'm not quite sure what you did there but correct me if I'm wrong.

The syntax below will show strings before "/". The char "\" is used for escaping. I'm guessing the chars "%%" and "*" at the end are responsible for showing all the characters before "/".

${parm1%%\/*}

Same thing for the "#*".

Thanks.

%% tells the shell to start looking for the last occurrence of '/' from the end of string and strip off everything () from there till the end (this is done by \/). The last occurrence of '/' from the end of string is the '/' right after 192.168.1.101. So, it deletes this '/' and all that follows, leaving behind just the IP address.

Similarly, #*\/ starts looking from the beginning of string until the first occurrence of '/'. The first occurrence of '/' from the beginning of string is the '/' right after 192.168.1.101. So it deletes the IP address and the first occurrence of '/', leaving behind "parent/child".

1 Like