How do I Find a string in a file and place a string above it?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
2
down vote
favorite
I would like to find the String __MARKER__
in someFile.txt
and place a String above it (e.g. Hello World!
).
How would I do that using sed
?
sed -i -e "/__MARKER__//g" someFile.txt # Currently removes __MARKER__.
Contents of someFile.txt
:
__MARKER__
Contents of someFile.txt
after manipulation:
Hello World!
__MARKER__
bash sed
add a comment |Â
up vote
2
down vote
favorite
I would like to find the String __MARKER__
in someFile.txt
and place a String above it (e.g. Hello World!
).
How would I do that using sed
?
sed -i -e "/__MARKER__//g" someFile.txt # Currently removes __MARKER__.
Contents of someFile.txt
:
__MARKER__
Contents of someFile.txt
after manipulation:
Hello World!
__MARKER__
bash sed
This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
â grg
Jun 6 at 20:43
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I would like to find the String __MARKER__
in someFile.txt
and place a String above it (e.g. Hello World!
).
How would I do that using sed
?
sed -i -e "/__MARKER__//g" someFile.txt # Currently removes __MARKER__.
Contents of someFile.txt
:
__MARKER__
Contents of someFile.txt
after manipulation:
Hello World!
__MARKER__
bash sed
I would like to find the String __MARKER__
in someFile.txt
and place a String above it (e.g. Hello World!
).
How would I do that using sed
?
sed -i -e "/__MARKER__//g" someFile.txt # Currently removes __MARKER__.
Contents of someFile.txt
:
__MARKER__
Contents of someFile.txt
after manipulation:
Hello World!
__MARKER__
bash sed
edited Jun 6 at 19:44
![](https://i.stack.imgur.com/kCmqs.png?s=32&g=1)
![](https://i.stack.imgur.com/kCmqs.png?s=32&g=1)
PerlDuck
3,62911030
3,62911030
asked Jun 6 at 19:31
![](https://lh6.googleusercontent.com/-chNcM5sNnmU/AAAAAAAAAAI/AAAAAAAAAIA/K0oQdb41DQc/photo.jpg?sz=32)
![](https://lh6.googleusercontent.com/-chNcM5sNnmU/AAAAAAAAAAI/AAAAAAAAAIA/K0oQdb41DQc/photo.jpg?sz=32)
Nicholas Adamou
628
628
This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
â grg
Jun 6 at 20:43
add a comment |Â
This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
â grg
Jun 6 at 20:43
This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
â grg
Jun 6 at 20:43
This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
â grg
Jun 6 at 20:43
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
sed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
replaces __MARKER__
with Hello world
, a newline, and the match (&
), i.e. the text __MARKER__
.
The &
just saves typing __MARKER__
again, as in
sed -i -e 's/__MARKER__/Hello worldn__MARKER__/' someFile.txt
After runningsed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
the contents ofsomeFile.txt
isHello worldn__MARKER__
.
â Nicholas Adamou
Jun 6 at 19:46
2
That's weird because for me it works exactly as I've written and I didn't escape any backslashes (by doubling them). It just worked as is. Are you on Windows?
â PerlDuck
Jun 6 at 19:51
add a comment |Â
up vote
4
down vote
If there is only a single instance of __MARKER__
or you want to insert the new string before every instance, you can use
sed -i '/__MARKER__/ iHello World!' someFile.txt
This uses the insert command rather than the substitute command - note that on POSIX (non-GNU) sed
you may need to put the insert string on a separate line:
sed -i '/__MARKER__/ i
Hello World!
' someFile.txt
If there are multiple instances, and you only wish to insert the string before the first one, then
sed -i '0,/__MARKER__/ s//Hello World!n&/' someFile.txt
Produces error:unterminated substitute pattern.
â Nicholas Adamou
Jun 6 at 19:50
@NicholasAdamou are you actually using Ubuntu? GNU sed? what is the output ofsed --version
?
â steeldriver
Jun 6 at 19:53
I'm running MacOS.
â Nicholas Adamou
Jun 6 at 19:56
1
@NicholasAdamou then the forum for you is apple.stackexchange.com
â steeldriver
Jun 6 at 19:56
@NicholasAdamou unix.stackexchange.com/a/256615/108199
â tjt263
Jun 13 at 14:35
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
sed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
replaces __MARKER__
with Hello world
, a newline, and the match (&
), i.e. the text __MARKER__
.
The &
just saves typing __MARKER__
again, as in
sed -i -e 's/__MARKER__/Hello worldn__MARKER__/' someFile.txt
After runningsed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
the contents ofsomeFile.txt
isHello worldn__MARKER__
.
â Nicholas Adamou
Jun 6 at 19:46
2
That's weird because for me it works exactly as I've written and I didn't escape any backslashes (by doubling them). It just worked as is. Are you on Windows?
â PerlDuck
Jun 6 at 19:51
add a comment |Â
up vote
4
down vote
accepted
sed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
replaces __MARKER__
with Hello world
, a newline, and the match (&
), i.e. the text __MARKER__
.
The &
just saves typing __MARKER__
again, as in
sed -i -e 's/__MARKER__/Hello worldn__MARKER__/' someFile.txt
After runningsed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
the contents ofsomeFile.txt
isHello worldn__MARKER__
.
â Nicholas Adamou
Jun 6 at 19:46
2
That's weird because for me it works exactly as I've written and I didn't escape any backslashes (by doubling them). It just worked as is. Are you on Windows?
â PerlDuck
Jun 6 at 19:51
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
sed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
replaces __MARKER__
with Hello world
, a newline, and the match (&
), i.e. the text __MARKER__
.
The &
just saves typing __MARKER__
again, as in
sed -i -e 's/__MARKER__/Hello worldn__MARKER__/' someFile.txt
sed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
replaces __MARKER__
with Hello world
, a newline, and the match (&
), i.e. the text __MARKER__
.
The &
just saves typing __MARKER__
again, as in
sed -i -e 's/__MARKER__/Hello worldn__MARKER__/' someFile.txt
answered Jun 6 at 19:38
![](https://i.stack.imgur.com/kCmqs.png?s=32&g=1)
![](https://i.stack.imgur.com/kCmqs.png?s=32&g=1)
PerlDuck
3,62911030
3,62911030
After runningsed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
the contents ofsomeFile.txt
isHello worldn__MARKER__
.
â Nicholas Adamou
Jun 6 at 19:46
2
That's weird because for me it works exactly as I've written and I didn't escape any backslashes (by doubling them). It just worked as is. Are you on Windows?
â PerlDuck
Jun 6 at 19:51
add a comment |Â
After runningsed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
the contents ofsomeFile.txt
isHello worldn__MARKER__
.
â Nicholas Adamou
Jun 6 at 19:46
2
That's weird because for me it works exactly as I've written and I didn't escape any backslashes (by doubling them). It just worked as is. Are you on Windows?
â PerlDuck
Jun 6 at 19:51
After running
sed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
the contents of someFile.txt
is Hello worldn__MARKER__
.â Nicholas Adamou
Jun 6 at 19:46
After running
sed -i -e 's/__MARKER__/Hello worldn&/' someFile.txt
the contents of someFile.txt
is Hello worldn__MARKER__
.â Nicholas Adamou
Jun 6 at 19:46
2
2
That's weird because for me it works exactly as I've written and I didn't escape any backslashes (by doubling them). It just worked as is. Are you on Windows?
â PerlDuck
Jun 6 at 19:51
That's weird because for me it works exactly as I've written and I didn't escape any backslashes (by doubling them). It just worked as is. Are you on Windows?
â PerlDuck
Jun 6 at 19:51
add a comment |Â
up vote
4
down vote
If there is only a single instance of __MARKER__
or you want to insert the new string before every instance, you can use
sed -i '/__MARKER__/ iHello World!' someFile.txt
This uses the insert command rather than the substitute command - note that on POSIX (non-GNU) sed
you may need to put the insert string on a separate line:
sed -i '/__MARKER__/ i
Hello World!
' someFile.txt
If there are multiple instances, and you only wish to insert the string before the first one, then
sed -i '0,/__MARKER__/ s//Hello World!n&/' someFile.txt
Produces error:unterminated substitute pattern.
â Nicholas Adamou
Jun 6 at 19:50
@NicholasAdamou are you actually using Ubuntu? GNU sed? what is the output ofsed --version
?
â steeldriver
Jun 6 at 19:53
I'm running MacOS.
â Nicholas Adamou
Jun 6 at 19:56
1
@NicholasAdamou then the forum for you is apple.stackexchange.com
â steeldriver
Jun 6 at 19:56
@NicholasAdamou unix.stackexchange.com/a/256615/108199
â tjt263
Jun 13 at 14:35
add a comment |Â
up vote
4
down vote
If there is only a single instance of __MARKER__
or you want to insert the new string before every instance, you can use
sed -i '/__MARKER__/ iHello World!' someFile.txt
This uses the insert command rather than the substitute command - note that on POSIX (non-GNU) sed
you may need to put the insert string on a separate line:
sed -i '/__MARKER__/ i
Hello World!
' someFile.txt
If there are multiple instances, and you only wish to insert the string before the first one, then
sed -i '0,/__MARKER__/ s//Hello World!n&/' someFile.txt
Produces error:unterminated substitute pattern.
â Nicholas Adamou
Jun 6 at 19:50
@NicholasAdamou are you actually using Ubuntu? GNU sed? what is the output ofsed --version
?
â steeldriver
Jun 6 at 19:53
I'm running MacOS.
â Nicholas Adamou
Jun 6 at 19:56
1
@NicholasAdamou then the forum for you is apple.stackexchange.com
â steeldriver
Jun 6 at 19:56
@NicholasAdamou unix.stackexchange.com/a/256615/108199
â tjt263
Jun 13 at 14:35
add a comment |Â
up vote
4
down vote
up vote
4
down vote
If there is only a single instance of __MARKER__
or you want to insert the new string before every instance, you can use
sed -i '/__MARKER__/ iHello World!' someFile.txt
This uses the insert command rather than the substitute command - note that on POSIX (non-GNU) sed
you may need to put the insert string on a separate line:
sed -i '/__MARKER__/ i
Hello World!
' someFile.txt
If there are multiple instances, and you only wish to insert the string before the first one, then
sed -i '0,/__MARKER__/ s//Hello World!n&/' someFile.txt
If there is only a single instance of __MARKER__
or you want to insert the new string before every instance, you can use
sed -i '/__MARKER__/ iHello World!' someFile.txt
This uses the insert command rather than the substitute command - note that on POSIX (non-GNU) sed
you may need to put the insert string on a separate line:
sed -i '/__MARKER__/ i
Hello World!
' someFile.txt
If there are multiple instances, and you only wish to insert the string before the first one, then
sed -i '0,/__MARKER__/ s//Hello World!n&/' someFile.txt
answered Jun 6 at 19:45
steeldriver
61.9k1196163
61.9k1196163
Produces error:unterminated substitute pattern.
â Nicholas Adamou
Jun 6 at 19:50
@NicholasAdamou are you actually using Ubuntu? GNU sed? what is the output ofsed --version
?
â steeldriver
Jun 6 at 19:53
I'm running MacOS.
â Nicholas Adamou
Jun 6 at 19:56
1
@NicholasAdamou then the forum for you is apple.stackexchange.com
â steeldriver
Jun 6 at 19:56
@NicholasAdamou unix.stackexchange.com/a/256615/108199
â tjt263
Jun 13 at 14:35
add a comment |Â
Produces error:unterminated substitute pattern.
â Nicholas Adamou
Jun 6 at 19:50
@NicholasAdamou are you actually using Ubuntu? GNU sed? what is the output ofsed --version
?
â steeldriver
Jun 6 at 19:53
I'm running MacOS.
â Nicholas Adamou
Jun 6 at 19:56
1
@NicholasAdamou then the forum for you is apple.stackexchange.com
â steeldriver
Jun 6 at 19:56
@NicholasAdamou unix.stackexchange.com/a/256615/108199
â tjt263
Jun 13 at 14:35
Produces error:
unterminated substitute pattern.
â Nicholas Adamou
Jun 6 at 19:50
Produces error:
unterminated substitute pattern.
â Nicholas Adamou
Jun 6 at 19:50
@NicholasAdamou are you actually using Ubuntu? GNU sed? what is the output of
sed --version
?â steeldriver
Jun 6 at 19:53
@NicholasAdamou are you actually using Ubuntu? GNU sed? what is the output of
sed --version
?â steeldriver
Jun 6 at 19:53
I'm running MacOS.
â Nicholas Adamou
Jun 6 at 19:56
I'm running MacOS.
â Nicholas Adamou
Jun 6 at 19:56
1
1
@NicholasAdamou then the forum for you is apple.stackexchange.com
â steeldriver
Jun 6 at 19:56
@NicholasAdamou then the forum for you is apple.stackexchange.com
â steeldriver
Jun 6 at 19:56
@NicholasAdamou unix.stackexchange.com/a/256615/108199
â tjt263
Jun 13 at 14:35
@NicholasAdamou unix.stackexchange.com/a/256615/108199
â tjt263
Jun 13 at 14:35
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1044271%2fhow-do-i-find-a-string-in-a-file-and-place-a-string-above-it%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
â grg
Jun 6 at 20:43