Removing first occurrence of a string in a file with sed on mac [duplicate]
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
bash sed
marked as duplicate by tripleee
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.
add a comment |
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
bash sed
marked as duplicate by tripleee
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
add a comment |
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
bash sed
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
bash sed
asked Nov 14 '18 at 13:24
Chris G.Chris G.
6,6872472148
6,6872472148
marked as duplicate by tripleee
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
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
1
Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt
– Chris G.
Nov 14 '18 at 13:49
add a comment |
Use:
sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file
2
MacOSsed
does not support the GNUsed
extension0,
– tripleee
Nov 14 '18 at 13:45
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt
– Chris G.
Nov 14 '18 at 13:49
add a comment |
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.
1
Thanks it works - just printing to console - I now do "awk ..." >> newfile.txt
– Chris G.
Nov 14 '18 at 13:49
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Use:
sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file
2
MacOSsed
does not support the GNUsed
extension0,
– tripleee
Nov 14 '18 at 13:45
add a comment |
Use:
sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file
2
MacOSsed
does not support the GNUsed
extension0,
– tripleee
Nov 14 '18 at 13:45
add a comment |
Use:
sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file
Use:
sed -i '0,/.*CHANGE_THIS.*/s//my changed line/' file
answered Nov 14 '18 at 13:41
Incrivel Monstro VerdeIncrivel Monstro Verde
3354
3354
2
MacOSsed
does not support the GNUsed
extension0,
– tripleee
Nov 14 '18 at 13:45
add a comment |
2
MacOSsed
does not support the GNUsed
extension0,
– 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
add a comment |
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