LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help With bash script (https://www.linuxquestions.org/questions/programming-9/help-with-bash-script-614684/)

Fredstar 01-19-2008 01:11 AM

Help With bash script
 
Hi all,

I'm trying to find a tool or simple bash expression that will allow me to subtract a string from another string, or an expression from a string.

for example.

Say i have a string,

Code:

"/home/user/code/src
And another string

Code:

"/home/user/code/src/com/domain/pkg/blah.java"
What could i use to subtract /home/user/code/src from the second string to return com/domain/blah.java ?

thanks

angrybanana 01-19-2008 02:32 AM

Code:

string="/home/user/code/src/com/domain/pkg/blah.java"
substring="/home/user/code/src/"
newstring=${string#$substring}

Here's more info:
http://tldp.org/LDP/abs/html/string-manipulation.html

twantrd 01-19-2008 02:33 AM

I like using sed to do things like this. Here's my way:

Code:

[twantrd@twantrd /]$ echo "/home/user/code/src/com/domain/pkg/blah.java" | sed 's#\(.*src/\)\(.*\)#\2#'
com/domain/pkg/blah.java


-twantrd

Fredstar 01-19-2008 11:55 AM

Thank you!!

Both options worked fine. I ended up just going with bash. Also thanks for the link had a lot of good stuff i was looking for.

urka58 01-20-2008 04:08 PM

Why not using this..?
elio@darkstar:~$ echo /home/user/code/src/com/domain/pkg/blah.java | cut -d / -f 6,7,8,9
com/domain/pkg/blah.java
Ciao


All times are GMT -5. The time now is 04:59 AM.