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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:


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
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

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
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Good catch CEHJ!

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?
MarkLoveExExAuthor Commented:
Would you trust this site to get the unix4j jars?

I would assume I would just add those to my class path...
MarkLoveExExAuthor Commented:
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?
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
MarkLoveExExAuthor Commented:
CEHJ -- yes, I am very lucky that sed works on these particular binary files that I apply it on.
I'm not sure you can rely on the continuance of that
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

From novice to tech pro — start learning today.