sed - replace string with file contents
up vote
18
down vote
favorite
I have two files: file1
and file2
.
file1
has the following contents:
---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"
file2
contains an IP address (1.1.1.1
)
What I want to do is replace localhost
with 1.1.1.1
, so that the end result is:
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
I have tried:
sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1
But I either get the whole line replaced, or the IP going to the line after the one I need to modify.
text-processing sed
add a comment |
up vote
18
down vote
favorite
I have two files: file1
and file2
.
file1
has the following contents:
---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"
file2
contains an IP address (1.1.1.1
)
What I want to do is replace localhost
with 1.1.1.1
, so that the end result is:
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
I have tried:
sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1
But I either get the whole line replaced, or the IP going to the line after the one I need to modify.
text-processing sed
1
not sure, but doescat file1 | sed -e 's/localhost/1.1.1.1/g'
work?
– dchirikov
Jul 8 '14 at 18:57
1
Look at ther
sed command.
– Kevin
Jul 19 '14 at 15:01
add a comment |
up vote
18
down vote
favorite
up vote
18
down vote
favorite
I have two files: file1
and file2
.
file1
has the following contents:
---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"
file2
contains an IP address (1.1.1.1
)
What I want to do is replace localhost
with 1.1.1.1
, so that the end result is:
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
I have tried:
sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1
But I either get the whole line replaced, or the IP going to the line after the one I need to modify.
text-processing sed
I have two files: file1
and file2
.
file1
has the following contents:
---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"
file2
contains an IP address (1.1.1.1
)
What I want to do is replace localhost
with 1.1.1.1
, so that the end result is:
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
I have tried:
sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1
But I either get the whole line replaced, or the IP going to the line after the one I need to modify.
text-processing sed
text-processing sed
edited Aug 20 '16 at 14:52
don_crissti
49k15129157
49k15129157
asked Jul 8 '14 at 18:23
Jay Kah
91113
91113
1
not sure, but doescat file1 | sed -e 's/localhost/1.1.1.1/g'
work?
– dchirikov
Jul 8 '14 at 18:57
1
Look at ther
sed command.
– Kevin
Jul 19 '14 at 15:01
add a comment |
1
not sure, but doescat file1 | sed -e 's/localhost/1.1.1.1/g'
work?
– dchirikov
Jul 8 '14 at 18:57
1
Look at ther
sed command.
– Kevin
Jul 19 '14 at 15:01
1
1
not sure, but does
cat file1 | sed -e 's/localhost/1.1.1.1/g'
work?– dchirikov
Jul 8 '14 at 18:57
not sure, but does
cat file1 | sed -e 's/localhost/1.1.1.1/g'
work?– dchirikov
Jul 8 '14 at 18:57
1
1
Look at the
r
sed command.– Kevin
Jul 19 '14 at 15:01
Look at the
r
sed command.– Kevin
Jul 19 '14 at 15:01
add a comment |
4 Answers
4
active
oldest
votes
up vote
13
down vote
Here is a sed
solution:
% sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
You should use sed -i
to make the change inplace.
If you can use awk
, here is one way to do:
% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
4
+1 forawk
. I imaginesed
is capable of this, but it will be terribly clumsy. This is whereawk
shines!
– HalosGhost
Jul 8 '14 at 19:14
1
@HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
– cuonglm
Jul 8 '14 at 19:25
The command substitution for thesed
solution should be double quoted in case the file contains spaces or glob characters.
– Graeme
Jul 8 '14 at 19:38
@Graeme: Thanks, updated! Feel free to make an editting.
– cuonglm
Jul 8 '14 at 19:39
2
You need to escape both/
and&
in the substitution. That's"$(sed 's:[/\&]:\&:g' file2)"
– Toby Speight
Dec 6 '16 at 12:40
|
show 1 more comment
up vote
6
down vote
You can read the file with the replacement string using shell command substitution, before sed
is used. So sed
will see just a normal substitution:
sed "s/localhost/$(cat file2)/" file1 > changed.txt
13
Does this work on multi-lined files and files with special characters?
– Trevor Hickey
Jun 12 '15 at 18:23
4
@TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
– DarioP
Oct 7 '16 at 11:16
add a comment |
up vote
1
down vote
Try using
join file1 file2
and then, remove any unwanted fields.
add a comment |
up vote
0
down vote
I also had this "problem" today: how to replace a block of text with the contents from another file.
I've solved it by making a bash function (that can be reused in scripts). Hope it helps others
[cent@pcmk-1 tmp]$ cat the_function.sh
# This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
# The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
#
# Usage:
# seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
function substitute_BLOCK_with_FILEcontents {
local BLOCK_StartRegexp="${1}"
local BLOCK_EndRegexp="${2}"
local FILE="${3}"
sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
}
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ source the_function.sh
[cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/result.txt
100
101
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
105
106
107
108
109
110
New contributor
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
Here is a sed
solution:
% sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
You should use sed -i
to make the change inplace.
If you can use awk
, here is one way to do:
% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
4
+1 forawk
. I imaginesed
is capable of this, but it will be terribly clumsy. This is whereawk
shines!
– HalosGhost
Jul 8 '14 at 19:14
1
@HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
– cuonglm
Jul 8 '14 at 19:25
The command substitution for thesed
solution should be double quoted in case the file contains spaces or glob characters.
– Graeme
Jul 8 '14 at 19:38
@Graeme: Thanks, updated! Feel free to make an editting.
– cuonglm
Jul 8 '14 at 19:39
2
You need to escape both/
and&
in the substitution. That's"$(sed 's:[/\&]:\&:g' file2)"
– Toby Speight
Dec 6 '16 at 12:40
|
show 1 more comment
up vote
13
down vote
Here is a sed
solution:
% sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
You should use sed -i
to make the change inplace.
If you can use awk
, here is one way to do:
% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
4
+1 forawk
. I imaginesed
is capable of this, but it will be terribly clumsy. This is whereawk
shines!
– HalosGhost
Jul 8 '14 at 19:14
1
@HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
– cuonglm
Jul 8 '14 at 19:25
The command substitution for thesed
solution should be double quoted in case the file contains spaces or glob characters.
– Graeme
Jul 8 '14 at 19:38
@Graeme: Thanks, updated! Feel free to make an editting.
– cuonglm
Jul 8 '14 at 19:39
2
You need to escape both/
and&
in the substitution. That's"$(sed 's:[/\&]:\&:g' file2)"
– Toby Speight
Dec 6 '16 at 12:40
|
show 1 more comment
up vote
13
down vote
up vote
13
down vote
Here is a sed
solution:
% sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
You should use sed -i
to make the change inplace.
If you can use awk
, here is one way to do:
% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
Here is a sed
solution:
% sed -e "s/localhost/$(sed 's:/:\/:g' file2)/" file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
You should use sed -i
to make the change inplace.
If you can use awk
, here is one way to do:
% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
host: "1.1.1.1"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "1.1.1.1:2181"
edited Jul 8 '14 at 19:44
Graeme
24.8k46296
24.8k46296
answered Jul 8 '14 at 19:13
cuonglm
101k23197299
101k23197299
4
+1 forawk
. I imaginesed
is capable of this, but it will be terribly clumsy. This is whereawk
shines!
– HalosGhost
Jul 8 '14 at 19:14
1
@HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
– cuonglm
Jul 8 '14 at 19:25
The command substitution for thesed
solution should be double quoted in case the file contains spaces or glob characters.
– Graeme
Jul 8 '14 at 19:38
@Graeme: Thanks, updated! Feel free to make an editting.
– cuonglm
Jul 8 '14 at 19:39
2
You need to escape both/
and&
in the substitution. That's"$(sed 's:[/\&]:\&:g' file2)"
– Toby Speight
Dec 6 '16 at 12:40
|
show 1 more comment
4
+1 forawk
. I imaginesed
is capable of this, but it will be terribly clumsy. This is whereawk
shines!
– HalosGhost
Jul 8 '14 at 19:14
1
@HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
– cuonglm
Jul 8 '14 at 19:25
The command substitution for thesed
solution should be double quoted in case the file contains spaces or glob characters.
– Graeme
Jul 8 '14 at 19:38
@Graeme: Thanks, updated! Feel free to make an editting.
– cuonglm
Jul 8 '14 at 19:39
2
You need to escape both/
and&
in the substitution. That's"$(sed 's:[/\&]:\&:g' file2)"
– Toby Speight
Dec 6 '16 at 12:40
4
4
+1 for
awk
. I imagine sed
is capable of this, but it will be terribly clumsy. This is where awk
shines!– HalosGhost
Jul 8 '14 at 19:14
+1 for
awk
. I imagine sed
is capable of this, but it will be terribly clumsy. This is where awk
shines!– HalosGhost
Jul 8 '14 at 19:14
1
1
@HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
– cuonglm
Jul 8 '14 at 19:25
@HalosGhost: It's seem both me and you had misunderstand the OP question, I updated my answer.
– cuonglm
Jul 8 '14 at 19:25
The command substitution for the
sed
solution should be double quoted in case the file contains spaces or glob characters.– Graeme
Jul 8 '14 at 19:38
The command substitution for the
sed
solution should be double quoted in case the file contains spaces or glob characters.– Graeme
Jul 8 '14 at 19:38
@Graeme: Thanks, updated! Feel free to make an editting.
– cuonglm
Jul 8 '14 at 19:39
@Graeme: Thanks, updated! Feel free to make an editting.
– cuonglm
Jul 8 '14 at 19:39
2
2
You need to escape both
/
and &
in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
– Toby Speight
Dec 6 '16 at 12:40
You need to escape both
/
and &
in the substitution. That's "$(sed 's:[/\&]:\&:g' file2)"
– Toby Speight
Dec 6 '16 at 12:40
|
show 1 more comment
up vote
6
down vote
You can read the file with the replacement string using shell command substitution, before sed
is used. So sed
will see just a normal substitution:
sed "s/localhost/$(cat file2)/" file1 > changed.txt
13
Does this work on multi-lined files and files with special characters?
– Trevor Hickey
Jun 12 '15 at 18:23
4
@TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
– DarioP
Oct 7 '16 at 11:16
add a comment |
up vote
6
down vote
You can read the file with the replacement string using shell command substitution, before sed
is used. So sed
will see just a normal substitution:
sed "s/localhost/$(cat file2)/" file1 > changed.txt
13
Does this work on multi-lined files and files with special characters?
– Trevor Hickey
Jun 12 '15 at 18:23
4
@TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
– DarioP
Oct 7 '16 at 11:16
add a comment |
up vote
6
down vote
up vote
6
down vote
You can read the file with the replacement string using shell command substitution, before sed
is used. So sed
will see just a normal substitution:
sed "s/localhost/$(cat file2)/" file1 > changed.txt
You can read the file with the replacement string using shell command substitution, before sed
is used. So sed
will see just a normal substitution:
sed "s/localhost/$(cat file2)/" file1 > changed.txt
answered Jul 8 '14 at 19:12
Volker Siegel
10.7k33258
10.7k33258
13
Does this work on multi-lined files and files with special characters?
– Trevor Hickey
Jun 12 '15 at 18:23
4
@TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
– DarioP
Oct 7 '16 at 11:16
add a comment |
13
Does this work on multi-lined files and files with special characters?
– Trevor Hickey
Jun 12 '15 at 18:23
4
@TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
– DarioP
Oct 7 '16 at 11:16
13
13
Does this work on multi-lined files and files with special characters?
– Trevor Hickey
Jun 12 '15 at 18:23
Does this work on multi-lined files and files with special characters?
– Trevor Hickey
Jun 12 '15 at 18:23
4
4
@TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
– DarioP
Oct 7 '16 at 11:16
@TrevorHickey it doesn't. Sed just fails with an "unterminated `s' command" error.
– DarioP
Oct 7 '16 at 11:16
add a comment |
up vote
1
down vote
Try using
join file1 file2
and then, remove any unwanted fields.
add a comment |
up vote
1
down vote
Try using
join file1 file2
and then, remove any unwanted fields.
add a comment |
up vote
1
down vote
up vote
1
down vote
Try using
join file1 file2
and then, remove any unwanted fields.
Try using
join file1 file2
and then, remove any unwanted fields.
answered Jul 8 '14 at 18:46
unxnut
3,5622918
3,5622918
add a comment |
add a comment |
up vote
0
down vote
I also had this "problem" today: how to replace a block of text with the contents from another file.
I've solved it by making a bash function (that can be reused in scripts). Hope it helps others
[cent@pcmk-1 tmp]$ cat the_function.sh
# This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
# The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
#
# Usage:
# seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
function substitute_BLOCK_with_FILEcontents {
local BLOCK_StartRegexp="${1}"
local BLOCK_EndRegexp="${2}"
local FILE="${3}"
sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
}
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ source the_function.sh
[cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/result.txt
100
101
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
105
106
107
108
109
110
New contributor
add a comment |
up vote
0
down vote
I also had this "problem" today: how to replace a block of text with the contents from another file.
I've solved it by making a bash function (that can be reused in scripts). Hope it helps others
[cent@pcmk-1 tmp]$ cat the_function.sh
# This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
# The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
#
# Usage:
# seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
function substitute_BLOCK_with_FILEcontents {
local BLOCK_StartRegexp="${1}"
local BLOCK_EndRegexp="${2}"
local FILE="${3}"
sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
}
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ source the_function.sh
[cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/result.txt
100
101
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
105
106
107
108
109
110
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
I also had this "problem" today: how to replace a block of text with the contents from another file.
I've solved it by making a bash function (that can be reused in scripts). Hope it helps others
[cent@pcmk-1 tmp]$ cat the_function.sh
# This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
# The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
#
# Usage:
# seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
function substitute_BLOCK_with_FILEcontents {
local BLOCK_StartRegexp="${1}"
local BLOCK_EndRegexp="${2}"
local FILE="${3}"
sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
}
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ source the_function.sh
[cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/result.txt
100
101
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
105
106
107
108
109
110
New contributor
I also had this "problem" today: how to replace a block of text with the contents from another file.
I've solved it by making a bash function (that can be reused in scripts). Hope it helps others
[cent@pcmk-1 tmp]$ cat the_function.sh
# This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
# The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
#
# Usage:
# seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
function substitute_BLOCK_with_FILEcontents {
local BLOCK_StartRegexp="${1}"
local BLOCK_EndRegexp="${2}"
local FILE="${3}"
sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
}
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ source the_function.sh
[cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/result.txt
100
101
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
105
106
107
108
109
110
New contributor
New contributor
answered Dec 3 at 12:09
zipizap
101
101
New contributor
New contributor
add a comment |
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%2f141387%2fsed-replace-string-with-file-contents%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
1
not sure, but does
cat file1 | sed -e 's/localhost/1.1.1.1/g'
work?– dchirikov
Jul 8 '14 at 18:57
1
Look at the
r
sed command.– Kevin
Jul 19 '14 at 15:01