File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes How to replace first occurence of a string in another string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to replace first occurence of a string in another string" Watch "How to replace first occurence of a string in another string" New topic
Author

How to replace first occurence of a string in another string

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi All,

I am trying to read the value from a property file and capture the value into a variable. My property file is a key value pair separted by '=' sign. So in script I replace the values before '=' with empty space and I get the value. Now I have a problem where my value apart also contains '=' sign and when I execute the script my data becomes null. Below is the code i use.



My Property file entry is



Could anyone let me know how to replace only the first occurence of the string?


Ashwin Sridhar
SCJP | SCWCD | OCA
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 960
    
  10

Your expression looks more complex than I would expect but to deal with the immediate problem I think you need to make the second sed expression use a reluctant .* . i.e. 's/^.*?=//' . Does 'sed' allow reluctant qualifiers ? Doesn't seem to !

Edit : Since 'sed' does not seem to support reluctant qualifiers I created a perl version -
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15632
    
  15

If I got that correctly:



Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to replace first occurence of a string in another string
 
Similar Threads
assertions compilation error
String replace
How to use Javascript variable value in JSP
Parse text between angle brackets
Inserting newline character