Removing first occurrence of a string in a file with sed on mac [duplicate]












1
















This question already has an answer here:




  • How to use sed to replace only the first occurrence in a file?

    19 answers




From this I found the command and it works without any command substitution. But if I try to substitute a variable in the command, I get the following problems.



Note: I am using sed command(BSD OSX) / MacOS



To reproduce just create a file.txt with this:



SOME_TEXT
CHANGE_THIS
CHANGE_THIS
CHANGE_THIS


Add TMP variable in you terminal:



TMP=export PATH=/Users/Name/with pace/and_variables:$PATH


Now with this command:



sed -i '' "1h; 1 $ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt


I get this error:



sed: 1: "1h; 1 $ { x; s/CHANGE_T ...": invalid command code $


In a privious answer I got this("," added to "1,$":



sed -i '' "1h; 1,$ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt   


expected output file.txt



SOME_TEXT
export PATH=/Users/Name/with pace/and_variables:$PATH
CHANGE_THIS
CHANGE_THIS


I get this:



SOME_TEXT
SOME_TEXT
SOME_TEXT
SOME_TEXT









share|improve this question













marked as duplicate by tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 5:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • Why use a variable at all, when you can just paste the text directly into the sed command? If you really want to do it like this, try "$TMP".

    – max
    Nov 14 '18 at 13:36


















1
















This question already has an answer here:




  • How to use sed to replace only the first occurrence in a file?

    19 answers




From this I found the command and it works without any command substitution. But if I try to substitute a variable in the command, I get the following problems.



Note: I am using sed command(BSD OSX) / MacOS



To reproduce just create a file.txt with this:



SOME_TEXT
CHANGE_THIS
CHANGE_THIS
CHANGE_THIS


Add TMP variable in you terminal:



TMP=export PATH=/Users/Name/with pace/and_variables:$PATH


Now with this command:



sed -i '' "1h; 1 $ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt


I get this error:



sed: 1: "1h; 1 $ { x; s/CHANGE_T ...": invalid command code $


In a privious answer I got this("," added to "1,$":



sed -i '' "1h; 1,$ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt   


expected output file.txt



SOME_TEXT
export PATH=/Users/Name/with pace/and_variables:$PATH
CHANGE_THIS
CHANGE_THIS


I get this:



SOME_TEXT
SOME_TEXT
SOME_TEXT
SOME_TEXT









share|improve this question













marked as duplicate by tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 5:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • Why use a variable at all, when you can just paste the text directly into the sed command? If you really want to do it like this, try "$TMP".

    – max
    Nov 14 '18 at 13:36
















1












1








1









This question already has an answer here:




  • How to use sed to replace only the first occurrence in a file?

    19 answers




From this I found the command and it works without any command substitution. But if I try to substitute a variable in the command, I get the following problems.



Note: I am using sed command(BSD OSX) / MacOS



To reproduce just create a file.txt with this:



SOME_TEXT
CHANGE_THIS
CHANGE_THIS
CHANGE_THIS


Add TMP variable in you terminal:



TMP=export PATH=/Users/Name/with pace/and_variables:$PATH


Now with this command:



sed -i '' "1h; 1 $ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt


I get this error:



sed: 1: "1h; 1 $ { x; s/CHANGE_T ...": invalid command code $


In a privious answer I got this("," added to "1,$":



sed -i '' "1h; 1,$ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt   


expected output file.txt



SOME_TEXT
export PATH=/Users/Name/with pace/and_variables:$PATH
CHANGE_THIS
CHANGE_THIS


I get this:



SOME_TEXT
SOME_TEXT
SOME_TEXT
SOME_TEXT









share|improve this question















This question already has an answer here:




  • How to use sed to replace only the first occurrence in a file?

    19 answers




From this I found the command and it works without any command substitution. But if I try to substitute a variable in the command, I get the following problems.



Note: I am using sed command(BSD OSX) / MacOS



To reproduce just create a file.txt with this:



SOME_TEXT
CHANGE_THIS
CHANGE_THIS
CHANGE_THIS


Add TMP variable in you terminal:



TMP=export PATH=/Users/Name/with pace/and_variables:$PATH


Now with this command:



sed -i '' "1h; 1 $ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt


I get this error:



sed: 1: "1h; 1 $ { x; s/CHANGE_T ...": invalid command code $


In a privious answer I got this("," added to "1,$":



sed -i '' "1h; 1,$ { x; s/CHANGE_THIS/$TMP/; p; }" file.txt   


expected output file.txt



SOME_TEXT
export PATH=/Users/Name/with pace/and_variables:$PATH
CHANGE_THIS
CHANGE_THIS


I get this:



SOME_TEXT
SOME_TEXT
SOME_TEXT
SOME_TEXT




This question already has an answer here:




  • How to use sed to replace only the first occurrence in a file?

    19 answers








bash sed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:24









Chris G.Chris G.

6,6872472148




6,6872472148




marked as duplicate by tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 5:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 5:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Why use a variable at all, when you can just paste the text directly into the sed command? If you really want to do it like this, try "$TMP".

    – max
    Nov 14 '18 at 13:36





















  • Why use a variable at all, when you can just paste the text directly into the sed command? If you really want to do it like this, try "$TMP".

    – max
    Nov 14 '18 at 13:36



















Why use a variable at all, when you can just paste the text directly into the sed command? If you really want to do it like this, try "$TMP".

– max
Nov 14 '18 at 13:36







Why use a variable at all, when you can just paste the text directly into the sed command? If you really want to do it like this, try "$TMP".

– max
Nov 14 '18 at 13:36














2 Answers
2






active

oldest

votes


















0














Could you please try following if you are ok with awk.



##Creating shell variable named TM here.
TMP="export PATH=/Users/Name/with pace/and_variables:$PATH"
awk -v tmp="$TMP" '/CHANGE_THIS/ && ++flag==1{$0=tmp} 1' Input_file


In case you want to save output into Input_file itself then append > temp_file && mv temp_file Input_file in above code too.



Adding explanation for above code too here.



awk -v tmp="$TMP" '              ##Creating an awk variable named tmp whose value is bash variable TMP value.
/CHANGE_THIS/ && ++flag==1{ ##Checking condition if a line is having string CHANGE_THIS and variable flag value is 1 then do following.
$0=tmp ##Setting current line value to value of tmp awk variable here.
} ##Closing the block for condition here.
1 ##By mentioning 1 printing edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.





share|improve this answer





















  • 1





    Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt

    – Chris G.
    Nov 14 '18 at 13:49



















1














Use:



sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file





share|improve this answer



















  • 2





    MacOS sed does not support the GNU sed extension 0,

    – tripleee
    Nov 14 '18 at 13:45


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Could you please try following if you are ok with awk.



##Creating shell variable named TM here.
TMP="export PATH=/Users/Name/with pace/and_variables:$PATH"
awk -v tmp="$TMP" '/CHANGE_THIS/ && ++flag==1{$0=tmp} 1' Input_file


In case you want to save output into Input_file itself then append > temp_file && mv temp_file Input_file in above code too.



Adding explanation for above code too here.



awk -v tmp="$TMP" '              ##Creating an awk variable named tmp whose value is bash variable TMP value.
/CHANGE_THIS/ && ++flag==1{ ##Checking condition if a line is having string CHANGE_THIS and variable flag value is 1 then do following.
$0=tmp ##Setting current line value to value of tmp awk variable here.
} ##Closing the block for condition here.
1 ##By mentioning 1 printing edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.





share|improve this answer





















  • 1





    Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt

    – Chris G.
    Nov 14 '18 at 13:49
















0














Could you please try following if you are ok with awk.



##Creating shell variable named TM here.
TMP="export PATH=/Users/Name/with pace/and_variables:$PATH"
awk -v tmp="$TMP" '/CHANGE_THIS/ && ++flag==1{$0=tmp} 1' Input_file


In case you want to save output into Input_file itself then append > temp_file && mv temp_file Input_file in above code too.



Adding explanation for above code too here.



awk -v tmp="$TMP" '              ##Creating an awk variable named tmp whose value is bash variable TMP value.
/CHANGE_THIS/ && ++flag==1{ ##Checking condition if a line is having string CHANGE_THIS and variable flag value is 1 then do following.
$0=tmp ##Setting current line value to value of tmp awk variable here.
} ##Closing the block for condition here.
1 ##By mentioning 1 printing edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.





share|improve this answer





















  • 1





    Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt

    – Chris G.
    Nov 14 '18 at 13:49














0












0








0







Could you please try following if you are ok with awk.



##Creating shell variable named TM here.
TMP="export PATH=/Users/Name/with pace/and_variables:$PATH"
awk -v tmp="$TMP" '/CHANGE_THIS/ && ++flag==1{$0=tmp} 1' Input_file


In case you want to save output into Input_file itself then append > temp_file && mv temp_file Input_file in above code too.



Adding explanation for above code too here.



awk -v tmp="$TMP" '              ##Creating an awk variable named tmp whose value is bash variable TMP value.
/CHANGE_THIS/ && ++flag==1{ ##Checking condition if a line is having string CHANGE_THIS and variable flag value is 1 then do following.
$0=tmp ##Setting current line value to value of tmp awk variable here.
} ##Closing the block for condition here.
1 ##By mentioning 1 printing edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.





share|improve this answer















Could you please try following if you are ok with awk.



##Creating shell variable named TM here.
TMP="export PATH=/Users/Name/with pace/and_variables:$PATH"
awk -v tmp="$TMP" '/CHANGE_THIS/ && ++flag==1{$0=tmp} 1' Input_file


In case you want to save output into Input_file itself then append > temp_file && mv temp_file Input_file in above code too.



Adding explanation for above code too here.



awk -v tmp="$TMP" '              ##Creating an awk variable named tmp whose value is bash variable TMP value.
/CHANGE_THIS/ && ++flag==1{ ##Checking condition if a line is having string CHANGE_THIS and variable flag value is 1 then do following.
$0=tmp ##Setting current line value to value of tmp awk variable here.
} ##Closing the block for condition here.
1 ##By mentioning 1 printing edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 13:45

























answered Nov 14 '18 at 13:39









RavinderSingh13RavinderSingh13

26.3k41438




26.3k41438








  • 1





    Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt

    – Chris G.
    Nov 14 '18 at 13:49














  • 1





    Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt

    – Chris G.
    Nov 14 '18 at 13:49








1




1





Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt

– Chris G.
Nov 14 '18 at 13:49





Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt

– Chris G.
Nov 14 '18 at 13:49













1














Use:



sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file





share|improve this answer



















  • 2





    MacOS sed does not support the GNU sed extension 0,

    – tripleee
    Nov 14 '18 at 13:45
















1














Use:



sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file





share|improve this answer



















  • 2





    MacOS sed does not support the GNU sed extension 0,

    – tripleee
    Nov 14 '18 at 13:45














1












1








1







Use:



sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file





share|improve this answer













Use:



sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 13:41









Incrivel Monstro VerdeIncrivel Monstro Verde

3354




3354








  • 2





    MacOS sed does not support the GNU sed extension 0,

    – tripleee
    Nov 14 '18 at 13:45














  • 2





    MacOS sed does not support the GNU sed extension 0,

    – tripleee
    Nov 14 '18 at 13:45








2




2





MacOS sed does not support the GNU sed extension 0,

– tripleee
Nov 14 '18 at 13:45





MacOS sed does not support the GNU sed extension 0,

– tripleee
Nov 14 '18 at 13:45



這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud