LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2008, 01:11 AM   #1
Fredstar
Member
 
Registered: Jul 2004
Location: Rochester, NY
Distribution: Fedora9::FreeBSD7.1
Posts: 296

Rep: Reputation: 30
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
 
Old 01-19-2008, 02:32 AM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
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
 
Old 01-19-2008, 02:33 AM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
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
 
Old 01-19-2008, 11:55 AM   #4
Fredstar
Member
 
Registered: Jul 2004
Location: Rochester, NY
Distribution: Fedora9::FreeBSD7.1
Posts: 296

Original Poster
Rep: Reputation: 30
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.
 
Old 01-20-2008, 04:08 PM   #5
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 15
Posts: 546

Rep: Reputation: 43
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration