[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Replacing a block of text in a file with sed

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
0
Jerryleo
Asked:
Jerryleo
1 Solution
 
glassdCommented:
How about something like this in awk:

awk '{
  if($1=="start_year") {
    p=1
    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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now