Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Search for word and change complete line in file

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Search for word and change complete line in file

    Iīm trying to make a java program that takes an argument as a variable. Then opens a file searches for a keyword and replaces the whole line with another.

    for example as I would do it with the command sed -i "/OBSERVER/c OBSERVER 'argument' /DAY" hdu.tpl3

    so I would look for the word OBSERVER in the file hdu.tpl3 and substitute the line with "OBSERVER 'argument' /DAY

    This is the code a have and dosenīt change anything in the file

    import java.io.*;
    import java.util.regex.*;

    public class FileTest {

    public static void main(String[] args){

    String name= "";
    name = name + args[0];
    String line= "sed -i";
    line = line.concat(" \"/OBSERVER/c OBSERVER '");
    line = line.concat(name);
    line = line.concat("' /DAY \" hdu.tpl3");
    String command = line;
    System.out.println(command);

    try{
    Process process = Runtime.getRuntime().exec(command);
    } catch (Exception e) {
    System.out.println("Something went wrong.");
    }
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Search for word and change complete line in file

    Are there any error messages returned from the process? Add code to open the streams written by the process, read them and print the results on the screen.
    The try{} catch will not get error messages from the process object.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: September 22nd, 2014, 07:51 AM
  2. Replies: 1
    Last Post: November 8th, 2013, 08:31 AM
  3. Word Search
    By jamie1234 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 9th, 2013, 06:03 PM
  4. search for specific line in the file
    By Rajitha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 16th, 2012, 09:18 AM
  5. Word Search Help
    By Tanner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2011, 01:39 AM

Tags for this Thread