Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, everyone. I'm somewhat new to linux and I'm having a problem trying to automatically update a text file. Maybe someone here can help me out. I have a text ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3

    Updating a single line in a text file

    Hello, everyone. I'm somewhat new to linux and I'm having a problem trying to automatically update a text file. Maybe someone here can help me out.

    I have a text file called namelist.wps. In this file there is a line that reads:

    Code:
    start_date = '2010-12-26_12:00:00', '2010-12-26_12:00:00',
    I have to automatically update the year, month, and day of month. I set values for the year, month, and day of month using the following code in a c-shell script:

    Code:
    set y1 = `date +%Y`
    set m1 = `date +%m`
    set d1 = `date +%d`
    After I do this, how do I update year, month, and day of month, without changing any of the other lines in the namelist.wps file?

  2. #2
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3
    I think sed could be used to do this but I don't know how.

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3
    Ok. This is the code for the script I am trying to use:

    Code:
    #!/bin/csh
    
    set y1 = `date +%Y`
    set m1 = `date +%m`
    set d1 = `date +%d`
    
    set date1 = ${y1}-${m1}-${d1}_12:00:00
    
    cat namelist.wps | sed 's/^.*start_date.*$/ start_date = '$date1', '$date1',/' >newnamelist.wps
    
    exit 0
    The out put in the newnamelist.wps file is :

    Code:
     start_date = 2010-12-31_12:00:00, 2010-12-31_12:00:00,
    The Problem is that I need single quotes around the dates so it reads as follows:

    Code:
     start_date = '2010-12-31_12:00:00', '2010-12-31_12:00:00',
    Someone please help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...