Insert new text in the middle of a file replacing old text without re-inserting the same things over and over?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a file that I want to update relatively frequently.

What needs to be done is that a specific part of the file would be changed to include some more lines. I am doing it with sed and a option:

So this works:



sed -i "/import:/a$IMPORTED" file 


This works as expected the first time. On the next re-runs this commands appends over and over the same lines.

How would I approach this so that I somehow remove everything I imported before I run it again?

E.g. use some following lines to anchor in the beginning that I know were not added automatically or something like that?










share|improve this question























  • How do you know when the old $IMPORTED ends?
    – Jeff Schaller
    Nov 27 at 20:42










  • @JeffSchaller:I know the format of the lines I add. And also I could consider the following line as an anchor but that would be error prone in case it changed
    – Jim
    Nov 27 at 21:32










  • @don_crissti: I don't know that except that I kind of know the format of what I insert
    – Jim
    Nov 27 at 21:33










  • @don_crissti: The added lines have a specific prefix: e.g. foo/bar/etc. But I am not sure if there won't be any other addition that wasn't done by sed.
    – Jim
    Nov 27 at 21:50






  • 1




    sed can delete lines, no problem. So if you know those comments are always only on inserted lines, delete those lines e.g. |// auto-lines|d
    – don_crissti
    Nov 27 at 22:27















up vote
0
down vote

favorite












I have a file that I want to update relatively frequently.

What needs to be done is that a specific part of the file would be changed to include some more lines. I am doing it with sed and a option:

So this works:



sed -i "/import:/a$IMPORTED" file 


This works as expected the first time. On the next re-runs this commands appends over and over the same lines.

How would I approach this so that I somehow remove everything I imported before I run it again?

E.g. use some following lines to anchor in the beginning that I know were not added automatically or something like that?










share|improve this question























  • How do you know when the old $IMPORTED ends?
    – Jeff Schaller
    Nov 27 at 20:42










  • @JeffSchaller:I know the format of the lines I add. And also I could consider the following line as an anchor but that would be error prone in case it changed
    – Jim
    Nov 27 at 21:32










  • @don_crissti: I don't know that except that I kind of know the format of what I insert
    – Jim
    Nov 27 at 21:33










  • @don_crissti: The added lines have a specific prefix: e.g. foo/bar/etc. But I am not sure if there won't be any other addition that wasn't done by sed.
    – Jim
    Nov 27 at 21:50






  • 1




    sed can delete lines, no problem. So if you know those comments are always only on inserted lines, delete those lines e.g. |// auto-lines|d
    – don_crissti
    Nov 27 at 22:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file that I want to update relatively frequently.

What needs to be done is that a specific part of the file would be changed to include some more lines. I am doing it with sed and a option:

So this works:



sed -i "/import:/a$IMPORTED" file 


This works as expected the first time. On the next re-runs this commands appends over and over the same lines.

How would I approach this so that I somehow remove everything I imported before I run it again?

E.g. use some following lines to anchor in the beginning that I know were not added automatically or something like that?










share|improve this question















I have a file that I want to update relatively frequently.

What needs to be done is that a specific part of the file would be changed to include some more lines. I am doing it with sed and a option:

So this works:



sed -i "/import:/a$IMPORTED" file 


This works as expected the first time. On the next re-runs this commands appends over and over the same lines.

How would I approach this so that I somehow remove everything I imported before I run it again?

E.g. use some following lines to anchor in the beginning that I know were not added automatically or something like that?







text-processing sed regular-expression perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 at 1:31









Isaac

10.3k11446




10.3k11446










asked Nov 27 at 20:39









Jim

390213




390213











  • How do you know when the old $IMPORTED ends?
    – Jeff Schaller
    Nov 27 at 20:42










  • @JeffSchaller:I know the format of the lines I add. And also I could consider the following line as an anchor but that would be error prone in case it changed
    – Jim
    Nov 27 at 21:32










  • @don_crissti: I don't know that except that I kind of know the format of what I insert
    – Jim
    Nov 27 at 21:33










  • @don_crissti: The added lines have a specific prefix: e.g. foo/bar/etc. But I am not sure if there won't be any other addition that wasn't done by sed.
    – Jim
    Nov 27 at 21:50






  • 1




    sed can delete lines, no problem. So if you know those comments are always only on inserted lines, delete those lines e.g. |// auto-lines|d
    – don_crissti
    Nov 27 at 22:27

















  • How do you know when the old $IMPORTED ends?
    – Jeff Schaller
    Nov 27 at 20:42










  • @JeffSchaller:I know the format of the lines I add. And also I could consider the following line as an anchor but that would be error prone in case it changed
    – Jim
    Nov 27 at 21:32










  • @don_crissti: I don't know that except that I kind of know the format of what I insert
    – Jim
    Nov 27 at 21:33










  • @don_crissti: The added lines have a specific prefix: e.g. foo/bar/etc. But I am not sure if there won't be any other addition that wasn't done by sed.
    – Jim
    Nov 27 at 21:50






  • 1




    sed can delete lines, no problem. So if you know those comments are always only on inserted lines, delete those lines e.g. |// auto-lines|d
    – don_crissti
    Nov 27 at 22:27
















How do you know when the old $IMPORTED ends?
– Jeff Schaller
Nov 27 at 20:42




How do you know when the old $IMPORTED ends?
– Jeff Schaller
Nov 27 at 20:42












@JeffSchaller:I know the format of the lines I add. And also I could consider the following line as an anchor but that would be error prone in case it changed
– Jim
Nov 27 at 21:32




@JeffSchaller:I know the format of the lines I add. And also I could consider the following line as an anchor but that would be error prone in case it changed
– Jim
Nov 27 at 21:32












@don_crissti: I don't know that except that I kind of know the format of what I insert
– Jim
Nov 27 at 21:33




@don_crissti: I don't know that except that I kind of know the format of what I insert
– Jim
Nov 27 at 21:33












@don_crissti: The added lines have a specific prefix: e.g. foo/bar/etc. But I am not sure if there won't be any other addition that wasn't done by sed.
– Jim
Nov 27 at 21:50




@don_crissti: The added lines have a specific prefix: e.g. foo/bar/etc. But I am not sure if there won't be any other addition that wasn't done by sed.
– Jim
Nov 27 at 21:50




1




1




sed can delete lines, no problem. So if you know those comments are always only on inserted lines, delete those lines e.g. |// auto-lines|d
– don_crissti
Nov 27 at 22:27





sed can delete lines, no problem. So if you know those comments are always only on inserted lines, delete those lines e.g. |// auto-lines|d
– don_crissti
Nov 27 at 22:27











1 Answer
1






active

oldest

votes

















up vote
0
down vote













Your command:



sed "/import:/a$IMPORTED" infile


Is inserting the literal $IMPORTED text on linux and GNU sed. The problem is that the shell is interpreting the backslash before the $. You can either quote correctly:



sed '/import:/a'"$IMPORTED" infile


Or, split the value in its own script part:



sed -e '/import:/a' -e "$IMPORTED" infile


It is always better to single quote sed scripts.



To replace an old value with a new one that may contain an undefined number of lines, you need to




  • First: capture all lines in the pattern space:



    sed 'H;1h;$!d;x' infile




  • Set values to change:



    old='original'
    new='newvalue'




  • Remove old value (which must exist):



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' infile




  • Add the new input:



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' -e 's/(import:)/1n'"$new"'/g' infile



There is an corner issue in that if the variable $old contains a null value (empty) a newline will gets removed. To solve that we need:



sed -E -e 'H;1h;$!d;x' -e 's/(import:)'"$old:+"(\n)$old""'/1/g' -e 's/(import:)/1n'"$new"'/g' infile





share|improve this answer






















  • 1) I need to add remove the existing importing and add/refresh the new ones 2) Why is single quoting better? 3) I don't really understand the snippet. Could you please elaborate what's happening there?
    – Jim
    Nov 28 at 19:03










  • (1)and(3) I'll try. (2) Because it avoids the interpretation of backslash (and other characters like $, !, etc.) by the shell which change what sed receives as commands.
    – Isaac
    Nov 28 at 23:03










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484515%2finsert-new-text-in-the-middle-of-a-file-replacing-old-text-without-re-inserting%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Your command:



sed "/import:/a$IMPORTED" infile


Is inserting the literal $IMPORTED text on linux and GNU sed. The problem is that the shell is interpreting the backslash before the $. You can either quote correctly:



sed '/import:/a'"$IMPORTED" infile


Or, split the value in its own script part:



sed -e '/import:/a' -e "$IMPORTED" infile


It is always better to single quote sed scripts.



To replace an old value with a new one that may contain an undefined number of lines, you need to




  • First: capture all lines in the pattern space:



    sed 'H;1h;$!d;x' infile




  • Set values to change:



    old='original'
    new='newvalue'




  • Remove old value (which must exist):



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' infile




  • Add the new input:



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' -e 's/(import:)/1n'"$new"'/g' infile



There is an corner issue in that if the variable $old contains a null value (empty) a newline will gets removed. To solve that we need:



sed -E -e 'H;1h;$!d;x' -e 's/(import:)'"$old:+"(\n)$old""'/1/g' -e 's/(import:)/1n'"$new"'/g' infile





share|improve this answer






















  • 1) I need to add remove the existing importing and add/refresh the new ones 2) Why is single quoting better? 3) I don't really understand the snippet. Could you please elaborate what's happening there?
    – Jim
    Nov 28 at 19:03










  • (1)and(3) I'll try. (2) Because it avoids the interpretation of backslash (and other characters like $, !, etc.) by the shell which change what sed receives as commands.
    – Isaac
    Nov 28 at 23:03














up vote
0
down vote













Your command:



sed "/import:/a$IMPORTED" infile


Is inserting the literal $IMPORTED text on linux and GNU sed. The problem is that the shell is interpreting the backslash before the $. You can either quote correctly:



sed '/import:/a'"$IMPORTED" infile


Or, split the value in its own script part:



sed -e '/import:/a' -e "$IMPORTED" infile


It is always better to single quote sed scripts.



To replace an old value with a new one that may contain an undefined number of lines, you need to




  • First: capture all lines in the pattern space:



    sed 'H;1h;$!d;x' infile




  • Set values to change:



    old='original'
    new='newvalue'




  • Remove old value (which must exist):



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' infile




  • Add the new input:



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' -e 's/(import:)/1n'"$new"'/g' infile



There is an corner issue in that if the variable $old contains a null value (empty) a newline will gets removed. To solve that we need:



sed -E -e 'H;1h;$!d;x' -e 's/(import:)'"$old:+"(\n)$old""'/1/g' -e 's/(import:)/1n'"$new"'/g' infile





share|improve this answer






















  • 1) I need to add remove the existing importing and add/refresh the new ones 2) Why is single quoting better? 3) I don't really understand the snippet. Could you please elaborate what's happening there?
    – Jim
    Nov 28 at 19:03










  • (1)and(3) I'll try. (2) Because it avoids the interpretation of backslash (and other characters like $, !, etc.) by the shell which change what sed receives as commands.
    – Isaac
    Nov 28 at 23:03












up vote
0
down vote










up vote
0
down vote









Your command:



sed "/import:/a$IMPORTED" infile


Is inserting the literal $IMPORTED text on linux and GNU sed. The problem is that the shell is interpreting the backslash before the $. You can either quote correctly:



sed '/import:/a'"$IMPORTED" infile


Or, split the value in its own script part:



sed -e '/import:/a' -e "$IMPORTED" infile


It is always better to single quote sed scripts.



To replace an old value with a new one that may contain an undefined number of lines, you need to




  • First: capture all lines in the pattern space:



    sed 'H;1h;$!d;x' infile




  • Set values to change:



    old='original'
    new='newvalue'




  • Remove old value (which must exist):



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' infile




  • Add the new input:



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' -e 's/(import:)/1n'"$new"'/g' infile



There is an corner issue in that if the variable $old contains a null value (empty) a newline will gets removed. To solve that we need:



sed -E -e 'H;1h;$!d;x' -e 's/(import:)'"$old:+"(\n)$old""'/1/g' -e 's/(import:)/1n'"$new"'/g' infile





share|improve this answer














Your command:



sed "/import:/a$IMPORTED" infile


Is inserting the literal $IMPORTED text on linux and GNU sed. The problem is that the shell is interpreting the backslash before the $. You can either quote correctly:



sed '/import:/a'"$IMPORTED" infile


Or, split the value in its own script part:



sed -e '/import:/a' -e "$IMPORTED" infile


It is always better to single quote sed scripts.



To replace an old value with a new one that may contain an undefined number of lines, you need to




  • First: capture all lines in the pattern space:



    sed 'H;1h;$!d;x' infile




  • Set values to change:



    old='original'
    new='newvalue'




  • Remove old value (which must exist):



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' infile




  • Add the new input:



    sed -E -e 'H;1h;$!d;x' -e 's/(import:)(n)'"$old"'/1/g' -e 's/(import:)/1n'"$new"'/g' infile



There is an corner issue in that if the variable $old contains a null value (empty) a newline will gets removed. To solve that we need:



sed -E -e 'H;1h;$!d;x' -e 's/(import:)'"$old:+"(\n)$old""'/1/g' -e 's/(import:)/1n'"$new"'/g' infile






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 at 1:29

























answered Nov 28 at 9:50









Isaac

10.3k11446




10.3k11446











  • 1) I need to add remove the existing importing and add/refresh the new ones 2) Why is single quoting better? 3) I don't really understand the snippet. Could you please elaborate what's happening there?
    – Jim
    Nov 28 at 19:03










  • (1)and(3) I'll try. (2) Because it avoids the interpretation of backslash (and other characters like $, !, etc.) by the shell which change what sed receives as commands.
    – Isaac
    Nov 28 at 23:03
















  • 1) I need to add remove the existing importing and add/refresh the new ones 2) Why is single quoting better? 3) I don't really understand the snippet. Could you please elaborate what's happening there?
    – Jim
    Nov 28 at 19:03










  • (1)and(3) I'll try. (2) Because it avoids the interpretation of backslash (and other characters like $, !, etc.) by the shell which change what sed receives as commands.
    – Isaac
    Nov 28 at 23:03















1) I need to add remove the existing importing and add/refresh the new ones 2) Why is single quoting better? 3) I don't really understand the snippet. Could you please elaborate what's happening there?
– Jim
Nov 28 at 19:03




1) I need to add remove the existing importing and add/refresh the new ones 2) Why is single quoting better? 3) I don't really understand the snippet. Could you please elaborate what's happening there?
– Jim
Nov 28 at 19:03












(1)and(3) I'll try. (2) Because it avoids the interpretation of backslash (and other characters like $, !, etc.) by the shell which change what sed receives as commands.
– Isaac
Nov 28 at 23:03




(1)and(3) I'll try. (2) Because it avoids the interpretation of backslash (and other characters like $, !, etc.) by the shell which change what sed receives as commands.
– Isaac
Nov 28 at 23:03

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484515%2finsert-new-text-in-the-middle-of-a-file-replacing-old-text-without-re-inserting%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay