How do I Find a string in a file and place a string above it?

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








up vote
2
down vote

favorite
1












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__






share|improve this question





















  • This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
    – grg
    Jun 6 at 20:43














up vote
2
down vote

favorite
1












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__






share|improve this question





















  • This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
    – grg
    Jun 6 at 20:43












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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__






share|improve this question













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__








share|improve this question












share|improve this question




share|improve this question








edited Jun 6 at 19:44









PerlDuck

3,62911030




3,62911030









asked Jun 6 at 19:31









Nicholas Adamou

628




628











  • 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




This question was cross-posted to Ask Different apple.stackexchange.com/q/327258/37797
– grg
Jun 6 at 20:43










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





share|improve this answer





















  • 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




    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


















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





share|improve this answer





















  • 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










  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
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: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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





share|improve this answer





















  • 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




    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















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





share|improve this answer





















  • 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




    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













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





share|improve this answer













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






share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 6 at 19:38









PerlDuck

3,62911030




3,62911030











  • 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




    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






  • 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













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





share|improve this answer





















  • 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










  • 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














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





share|improve this answer





















  • 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










  • 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












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





share|improve this answer













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






share|improve this answer













share|improve this answer



share|improve this answer











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 of sed --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










  • @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






  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491