System : Solaris I am trying to split a string using the delimiter as another string
For example:
The main string is : /as/asdasd/asdasd/root/asdqwe/asd/asssdd/
I wanna split this into two part from the "root" substring such that
$1 = /as/asdasd/asdasd/
and
$2 = asdqwe/asd/asssdd/
This is the code I implemented using FS, but it doesn't work:
echo /as/asdasd/asdasd/root/asdqwe/asd/asssdd/ | awk '
BEGIN { FS = "root" } { print $2 }'
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/
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With