Skip to content

Instantly share code, notes, and snippets.

@haoel
Last active March 28, 2022 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haoel/27dd6274426ecfb4467b7a3a2a0cdb33 to your computer and use it in GitHub Desktop.
Save haoel/27dd6274426ecfb4467b7a3a2a0cdb33 to your computer and use it in GitHub Desktop.
Replace delimited block of text in file with the contents of another file

Replace Text Block

We have megaease.conf, and want to replace the contents in user.conf between the ######### MEGAEASE-BEGIN ########## and ######### MEGAEASE-END ########## lines.

First time, insert the block

echo "######### MEGAEASE-BEGIN ##########" >> user.conf
cat megaease.conf >> user.conf
echo "#########  MEGAEASE-END  ##########" >> user.conf

Then, update the block with new configuration

awk 'FNR==NR{ _[++d]=$0;next}                                                                                                                                                                                          
/######### MEGAEASE-BEGIN ##########/{
  print
  for(i=1;i<=d;i++){ print _[i] }
  f=1;next
}
/#########  MEGAEASE-END  ##########/{f=0}!f' megaease.conf  userr.conf > user.new.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment