Java "sed" substitute command

Taking the following linux sed command:

sed 's/Q3A01RAD/MPM01   /' name > temp.file

What would be an equivalent way to accomplish this using Java code?
MarkLoveExExAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim CakalicSenior Developer/ArchitectCommented:
I could post a custom solution but I think the unix4j project might be a better option:
https://github.com/tools4j/unix4j

Regards,
Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dpearsonCommented:
Something like this will get you started:

String line;
// Read the file
try (BufferedReader input = new BufferedReader(new FileReader(file))) {
    while ((line = input.readLine()) != null) {
         // Do the string replacement
         line = line.replace("Q3A01RAD","MPM01   ");

         // Print out the result
         System.our.println(line) ;
    }
}

Open in new window


Doug
0
CEHJCommented:
Something like this will get you started:
In point of fact, you posted something implementing

 's/Q3A01RAD/MPM01   /g'

Open in new window


To implement the given pattern, you'd need String.replaceFirst
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

dpearsonCommented:
Good catch CEHJ!

Doug
0
MarkLoveExExAuthor Commented:
Jim Cakalic -- I like the idea of using unix4j. However, I'm trying to figure out how to get it in my RedHat Eclipse IDE. Is there a compiled jar out there somewhere that I can add to my build path to get access to the functionality and have intellesense to help me formulate the syntax?
0
MarkLoveExExAuthor Commented:
Would you trust this site to get the unix4j jars?
http://www.java2s.com/Code/Jar/u/unix4j.htm

I would assume I would just add those to my class path...
0
MarkLoveExExAuthor Commented:
0
MarkLoveExExAuthor Commented:
Ok. I figured out how to use unix4j in my java code. Unfortunately, the file that I am attempting to use as input is a BINARY file.  Command-line sed worked on my binary file, but the unix4j routine seems to have trouble with it (the binary file got larger/corrupted. Any ideas?
0
CEHJCommented:
sed is normally used on text files (it works line-wise) so the Java implementation might not expect to have to be able to work with binary files
0
MarkLoveExExAuthor Commented:
CEHJ -- yes, I am very lucky that sed works on these particular binary files that I apply it on.
0
CEHJCommented:
I'm not sure you can rely on the continuance of that
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.