Replacing a block of text in a file with sed

Posted on 2005-04-15
Last Modified: 2013-12-26
I need to locate and replace a block of text in a file daily.

for example,

....locate following block

 start_year                      =  2004
 start_month                   =     12
 start_day                       =     31
 start_hour                      =    00
 end_year                        =  2005
 end_month                     =     01
 end_day                         =     02
 end_hour                        =     12

.... and replace it with this:

 start_year                      =  2005
 start_month                   =     01
 start_day                       =     01
 start_hour                      =    00
 end_year                       =  2005
 end_month                    =     01
 end_day                        =     03
 end_hour                       =     12

Surely I don't have to do a line-by-line replacement. It seems like I should be able to pick a beginning and ending element, and choose to replace everything in between... I don't know how to deal with a block in sed, any ideas....?

Thanks in advance
Question by:Jerryleo
    1 Comment
    LVL 7

    Accepted Solution

    How about something like this in awk:

    awk '{
      if($1=="start_year") {
        print "start_year                      =  2005\n start_month                   =     01\n start_day                       =     01\n start_hour                      =    00\n end_year                       =  2005\n end_month                    =     01\n end_day                        =     03\n end_hour                       =     12"
      if(p==0) { print }
      if($1=="end_hour") { p=0 }
    }' filename


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now