Split a string using a string as delimiter in awk Split a string using a string as delimiter in awk shell shell

Split a string using a string as delimiter in awk


No need to use awk , you can do this with your POSIX shell like so:

$ var="/as/asdasd/asdasd/root/asdqwe/asd/asssdd/"$ echo ${var%/root/*}/as/asdasd/asdasd$ echo ${var#*/root/}asdqwe/asd/asssdd/

Update

If your Solaris version of awk isn't working (probably because FS must be chars not strings), then try this method using split()

awk '{split($0,a,"/root/");$1=a[1] "/"; $2=a[2]; print $1,$2}'


It works here, aside from the extra / on the front which you don't handle. Maybe you want "root/" as your delimiter? It may also be necessary to use a newer awk; Solaris still ships the ancient V7 Unix awk as /usr/bin/awk, POSIX-compliant awk is /usr/bin/nawk.