How to remove certain character strings with awk?

Hi all,

I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine).

Input:

 DBPATH=/some/path/database/db

Desired output:

/some/path/database

Thank you!

Hello Ejianu,

Following may help you in same.

echo "DBPATH=/some/path/database/db" | awk '{sub(/.*=/,X,$0);print}'

Thanks,
R. Singh

1 Like

RavinderSingh13,

Thank you for your prompt response. Unfortunately I get the following:

>echo "DBPATH=/some/path/database/db" | awk '{sub(/.*=/,X,$0);print}'
awk: syntax error near line 1
awk: illegal statement near line 1

Hello ejianu,

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

Thanks,
R. Singh

1 Like

RavinderSingh13,

Thank you! You are correct, I am using a Solaris OS.

Using nawk and /usr/xpg4/bin/awk I get the following, which is closer to what I need, but I still get /db at the end. How can I remove the /db also?

>echo "DBPATH=/some/path/database/db" | nawk '{sub(/.*=/,X,$0);print}'
/some/path/database/db

>echo "DBPATH=/some/path/database/db" | /usr/xpg4/bin/awk '{sub(/.*=/,X,$0);print}'
/some/path/database/db

>echo "DBPATH=/some/path/database/db" | /usr/xpg6/bin/awk '{sub(/.*=/,X,$0);print}'
ksh: /usr/xpg6/bin/awk:  not found

Hello ejianu,

Please try following(sorry I didn't see you don't need db also).

echo "DBPATH=/some/path/database/db" | nawk '{sub(/.*=/,X,$0);match($0,/\/.*\//);print substr($0,RSTART,RLENGTH-1)}'

Thanks,
R. Singh

1 Like

It works. Thank you!

With sed

echo "DBPATH=/some/path/database/db" | sed 's#.*=##; s#/[^/]*$##'

---------- Post updated at 11:52 AM ---------- Previous update was at 11:31 AM ----------

With expr

expr "DBPATH=/some/path/database/db" : ".*=\(.*\)/.*" 
1 Like

Thank you MadeInGermany! I confirm both of the above work on my Solaris box.