Insert new text in the middle of a file replacing old text without re-inserting the same things over and over?
Clash 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?
text-processing sed regular-expression perl
|
show 2 more comments
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?
text-processing sed regular-expression perl
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 bysed
.
– Jim
Nov 27 at 21:50
1
sed
cand
elete 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
|
show 2 more comments
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?
text-processing sed regular-expression perl
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
text-processing sed regular-expression perl
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 bysed
.
– Jim
Nov 27 at 21:50
1
sed
cand
elete 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
|
show 2 more comments
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 bysed
.
– Jim
Nov 27 at 21:50
1
sed
cand
elete 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 d
elete 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 d
elete 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
|
show 2 more comments
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 bysed
.– Jim
Nov 27 at 21:50
1
sed
cand
elete 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