Home > Software design >  How to remove everything from path after a word using sed in bash/ shell script?
How to remove everything from path after a word using sed in bash/ shell script?

Time:01-12

Where the word is repeated and only wanted to be removed from specific word location

Lets say my path is - /opt/xyz/config/config.xml 
Solution I want after using sed is: /opt/xyz/config/

how can this be obtained?

I am sick of using {sed 's/config.*//'} >> This actually removes both config words such as it looks

/opt/xyz/

I have tried using this in multiple ways

>  sed 's/config.*//'

CodePudding user response:

Maybe with something like this?

sed 's/[^/]*$//'

But if the filepath is in a shell variable then you might as well use:

mydir=${myfilepath%/*}/

CodePudding user response:

Another potential solution, depending on your use-case, is the dirname bash function, e.g.

dirname /opt/xyz/config/config.xml
/opt/xyz/config
  • Related