sed command to remove first occurrence of a word
I want to know how to remove the first occurrence of a word in a file.
Example: I want to remove the admin, from the first occurrence only.
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=admin,wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
I have tried:
sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver
But that removes both cases, any ideas?
macos sed
add a comment |
I want to know how to remove the first occurrence of a word in a file.
Example: I want to remove the admin, from the first occurrence only.
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=admin,wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
I have tried:
sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver
But that removes both cases, any ideas?
macos sed
add a comment |
I want to know how to remove the first occurrence of a word in a file.
Example: I want to remove the admin, from the first occurrence only.
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=admin,wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
I have tried:
sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver
But that removes both cases, any ideas?
macos sed
I want to know how to remove the first occurrence of a word in a file.
Example: I want to remove the admin, from the first occurrence only.
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=admin,wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
I have tried:
sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver
But that removes both cases, any ideas?
macos sed
macos sed
asked Mar 30 '15 at 21:03
Technic1anTechnic1an
1,16951317
1,16951317
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Using sed
To remove the first occurrence in the file:
$ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
(The above was tested with GNU sed.)
How it works
-e ':a' -e '$!N;ba;'
This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)
-e 's/admin,//1'
This performs the substitution on the first occurrence of
admin
and only the first occurrence.
Using BSD (OSX) sed
Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:
sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
This reads the whole file in at once and then does the substitution once the last line has been read.
In more detail:
-n
By default, sed would print each line. This turns that off.
1h
This places the first line in the hold buffer.
1!H
For all subsequent lines, this appends them to the hold buffer.
$ x; s/admin,//; p;
For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file.
s/admin,//
does the substitution andp
prints the result.
Using awk
$ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file
This results in:
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
How it works
/admin,/ && !fsub(/admin,/, ""); f=1
For each line, check to see if it contains the word
admin,
and if the flagf
still has its default value of zero. If so, remove the first occurrence ofadmin,
and set the flag to one.1
Print each line.
1
is awk's cryptic shorthand forprint $0
.
Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)
– Technic1an
Mar 30 '15 at 21:28
2
OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) useb
instructions in one-liners. Trysed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
(that works essentially the same way but uses a different way to assemble the whole file before matching).
– Wintermute
Mar 30 '15 at 21:38
1
Either works. BSD sed does not accept just-i
without an argument, though (but-i ''
works). By the way: the reasonb
instructions don't work in one-liners in BSD sed is that something likeba; }; stuff
takes everything after theb
until the end of the line as a label name and attempts to jump to a labela; }; stuff
. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it:sed 'ba; stuff'
givessed: 1: "ba; stuff": undefined label 'a; stuff'
.
– Wintermute
Mar 30 '15 at 21:52
2
Chaining commands with;
in BSD sed is fine. Vis-a-vis, the thing to know is that with BSD sed, you need a
;
before the}
that GNU sed doesn't require (p
vs.p;
). As for the code:1h
copies the current line to the hold buffer if it is the first.1!H
appends it there if it isn't the first.$ ...
does something if the current line is the last (at that point the whole file is in the hold buffer).x
swaps hold buffer and pattern space,s/admin,//
removes the firstadmin,
from the PS,p
prints the PS. The-n
option disables auto-printing.
– Wintermute
Mar 30 '15 at 22:12
1
@Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.
– John1024
Mar 30 '15 at 22:19
|
show 5 more comments
Using sed, without reading the whole file:
sed 's/admin,//;tl;b;:l;n;bl' file
This waits for a successful substitution and then goes into a loop of printing the rest of the file.
s/admin,//;tl;b;
: substitute admin with empty string, if substitution occurs jump to labell
otherwise print the line and exit (next line will be processed the same way).:l;n;bl
: define label l, read line, jump to l.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2fstackoverflow.com%2fquestions%2f29356239%2fsed-command-to-remove-first-occurrence-of-a-word%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using sed
To remove the first occurrence in the file:
$ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
(The above was tested with GNU sed.)
How it works
-e ':a' -e '$!N;ba;'
This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)
-e 's/admin,//1'
This performs the substitution on the first occurrence of
admin
and only the first occurrence.
Using BSD (OSX) sed
Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:
sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
This reads the whole file in at once and then does the substitution once the last line has been read.
In more detail:
-n
By default, sed would print each line. This turns that off.
1h
This places the first line in the hold buffer.
1!H
For all subsequent lines, this appends them to the hold buffer.
$ x; s/admin,//; p;
For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file.
s/admin,//
does the substitution andp
prints the result.
Using awk
$ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file
This results in:
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
How it works
/admin,/ && !fsub(/admin,/, ""); f=1
For each line, check to see if it contains the word
admin,
and if the flagf
still has its default value of zero. If so, remove the first occurrence ofadmin,
and set the flag to one.1
Print each line.
1
is awk's cryptic shorthand forprint $0
.
Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)
– Technic1an
Mar 30 '15 at 21:28
2
OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) useb
instructions in one-liners. Trysed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
(that works essentially the same way but uses a different way to assemble the whole file before matching).
– Wintermute
Mar 30 '15 at 21:38
1
Either works. BSD sed does not accept just-i
without an argument, though (but-i ''
works). By the way: the reasonb
instructions don't work in one-liners in BSD sed is that something likeba; }; stuff
takes everything after theb
until the end of the line as a label name and attempts to jump to a labela; }; stuff
. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it:sed 'ba; stuff'
givessed: 1: "ba; stuff": undefined label 'a; stuff'
.
– Wintermute
Mar 30 '15 at 21:52
2
Chaining commands with;
in BSD sed is fine. Vis-a-vis, the thing to know is that with BSD sed, you need a
;
before the}
that GNU sed doesn't require (p
vs.p;
). As for the code:1h
copies the current line to the hold buffer if it is the first.1!H
appends it there if it isn't the first.$ ...
does something if the current line is the last (at that point the whole file is in the hold buffer).x
swaps hold buffer and pattern space,s/admin,//
removes the firstadmin,
from the PS,p
prints the PS. The-n
option disables auto-printing.
– Wintermute
Mar 30 '15 at 22:12
1
@Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.
– John1024
Mar 30 '15 at 22:19
|
show 5 more comments
Using sed
To remove the first occurrence in the file:
$ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
(The above was tested with GNU sed.)
How it works
-e ':a' -e '$!N;ba;'
This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)
-e 's/admin,//1'
This performs the substitution on the first occurrence of
admin
and only the first occurrence.
Using BSD (OSX) sed
Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:
sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
This reads the whole file in at once and then does the substitution once the last line has been read.
In more detail:
-n
By default, sed would print each line. This turns that off.
1h
This places the first line in the hold buffer.
1!H
For all subsequent lines, this appends them to the hold buffer.
$ x; s/admin,//; p;
For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file.
s/admin,//
does the substitution andp
prints the result.
Using awk
$ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file
This results in:
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
How it works
/admin,/ && !fsub(/admin,/, ""); f=1
For each line, check to see if it contains the word
admin,
and if the flagf
still has its default value of zero. If so, remove the first occurrence ofadmin,
and set the flag to one.1
Print each line.
1
is awk's cryptic shorthand forprint $0
.
Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)
– Technic1an
Mar 30 '15 at 21:28
2
OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) useb
instructions in one-liners. Trysed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
(that works essentially the same way but uses a different way to assemble the whole file before matching).
– Wintermute
Mar 30 '15 at 21:38
1
Either works. BSD sed does not accept just-i
without an argument, though (but-i ''
works). By the way: the reasonb
instructions don't work in one-liners in BSD sed is that something likeba; }; stuff
takes everything after theb
until the end of the line as a label name and attempts to jump to a labela; }; stuff
. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it:sed 'ba; stuff'
givessed: 1: "ba; stuff": undefined label 'a; stuff'
.
– Wintermute
Mar 30 '15 at 21:52
2
Chaining commands with;
in BSD sed is fine. Vis-a-vis, the thing to know is that with BSD sed, you need a
;
before the}
that GNU sed doesn't require (p
vs.p;
). As for the code:1h
copies the current line to the hold buffer if it is the first.1!H
appends it there if it isn't the first.$ ...
does something if the current line is the last (at that point the whole file is in the hold buffer).x
swaps hold buffer and pattern space,s/admin,//
removes the firstadmin,
from the PS,p
prints the PS. The-n
option disables auto-printing.
– Wintermute
Mar 30 '15 at 22:12
1
@Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.
– John1024
Mar 30 '15 at 22:19
|
show 5 more comments
Using sed
To remove the first occurrence in the file:
$ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
(The above was tested with GNU sed.)
How it works
-e ':a' -e '$!N;ba;'
This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)
-e 's/admin,//1'
This performs the substitution on the first occurrence of
admin
and only the first occurrence.
Using BSD (OSX) sed
Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:
sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
This reads the whole file in at once and then does the substitution once the last line has been read.
In more detail:
-n
By default, sed would print each line. This turns that off.
1h
This places the first line in the hold buffer.
1!H
For all subsequent lines, this appends them to the hold buffer.
$ x; s/admin,//; p;
For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file.
s/admin,//
does the substitution andp
prints the result.
Using awk
$ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file
This results in:
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
How it works
/admin,/ && !fsub(/admin,/, ""); f=1
For each line, check to see if it contains the word
admin,
and if the flagf
still has its default value of zero. If so, remove the first occurrence ofadmin,
and set the flag to one.1
Print each line.
1
is awk's cryptic shorthand forprint $0
.
Using sed
To remove the first occurrence in the file:
$ sed -e ':a' -e '$!N;ba;' -e 's/admin,//1' file
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
(The above was tested with GNU sed.)
How it works
-e ':a' -e '$!N;ba;'
This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)
-e 's/admin,//1'
This performs the substitution on the first occurrence of
admin
and only the first occurrence.
Using BSD (OSX) sed
Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:
sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
This reads the whole file in at once and then does the substitution once the last line has been read.
In more detail:
-n
By default, sed would print each line. This turns that off.
1h
This places the first line in the hold buffer.
1!H
For all subsequent lines, this appends them to the hold buffer.
$ x; s/admin,//; p;
For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file.
s/admin,//
does the substitution andp
prints the result.
Using awk
$ awk '/admin/ && !fsub(/admin,/, ""); f=1 1' file >file.tmp && mv file.tmp file
This results in:
account required pam_opendirectory.so
account sufficient pam_self.so
account required pam_group.so no_warn group=wheel fail_safe
account required pam_group.so no_warn deny group=admin,wheel ruser fail_safe
How it works
/admin,/ && !fsub(/admin,/, ""); f=1
For each line, check to see if it contains the word
admin,
and if the flagf
still has its default value of zero. If so, remove the first occurrence ofadmin,
and set the flag to one.1
Print each line.
1
is awk's cryptic shorthand forprint $0
.
edited Mar 30 '15 at 22:17
answered Mar 30 '15 at 21:25
John1024John1024
77.2k87198
77.2k87198
Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)
– Technic1an
Mar 30 '15 at 21:28
2
OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) useb
instructions in one-liners. Trysed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
(that works essentially the same way but uses a different way to assemble the whole file before matching).
– Wintermute
Mar 30 '15 at 21:38
1
Either works. BSD sed does not accept just-i
without an argument, though (but-i ''
works). By the way: the reasonb
instructions don't work in one-liners in BSD sed is that something likeba; }; stuff
takes everything after theb
until the end of the line as a label name and attempts to jump to a labela; }; stuff
. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it:sed 'ba; stuff'
givessed: 1: "ba; stuff": undefined label 'a; stuff'
.
– Wintermute
Mar 30 '15 at 21:52
2
Chaining commands with;
in BSD sed is fine. Vis-a-vis, the thing to know is that with BSD sed, you need a
;
before the}
that GNU sed doesn't require (p
vs.p;
). As for the code:1h
copies the current line to the hold buffer if it is the first.1!H
appends it there if it isn't the first.$ ...
does something if the current line is the last (at that point the whole file is in the hold buffer).x
swaps hold buffer and pattern space,s/admin,//
removes the firstadmin,
from the PS,p
prints the PS. The-n
option disables auto-printing.
– Wintermute
Mar 30 '15 at 22:12
1
@Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.
– John1024
Mar 30 '15 at 22:19
|
show 5 more comments
Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)
– Technic1an
Mar 30 '15 at 21:28
2
OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) useb
instructions in one-liners. Trysed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
(that works essentially the same way but uses a different way to assemble the whole file before matching).
– Wintermute
Mar 30 '15 at 21:38
1
Either works. BSD sed does not accept just-i
without an argument, though (but-i ''
works). By the way: the reasonb
instructions don't work in one-liners in BSD sed is that something likeba; }; stuff
takes everything after theb
until the end of the line as a label name and attempts to jump to a labela; }; stuff
. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it:sed 'ba; stuff'
givessed: 1: "ba; stuff": undefined label 'a; stuff'
.
– Wintermute
Mar 30 '15 at 21:52
2
Chaining commands with;
in BSD sed is fine. Vis-a-vis, the thing to know is that with BSD sed, you need a
;
before the}
that GNU sed doesn't require (p
vs.p;
). As for the code:1h
copies the current line to the hold buffer if it is the first.1!H
appends it there if it isn't the first.$ ...
does something if the current line is the last (at that point the whole file is in the hold buffer).x
swaps hold buffer and pattern space,s/admin,//
removes the firstadmin,
from the PS,p
prints the PS. The-n
option disables auto-printing.
– Wintermute
Mar 30 '15 at 22:12
1
@Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.
– John1024
Mar 30 '15 at 22:19
Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)
– Technic1an
Mar 30 '15 at 21:28
Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s)
– Technic1an
Mar 30 '15 at 21:28
2
2
OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use
b
instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
(that works essentially the same way but uses a different way to assemble the whole file before matching).– Wintermute
Mar 30 '15 at 21:38
OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use
b
instructions in one-liners. Try sed -i.bak -n '1h; 1!H; $ x; s/admin,//; p; ' file
(that works essentially the same way but uses a different way to assemble the whole file before matching).– Wintermute
Mar 30 '15 at 21:38
1
1
Either works. BSD sed does not accept just
-i
without an argument, though (but -i ''
works). By the way: the reason b
instructions don't work in one-liners in BSD sed is that something like ba; }; stuff
takes everything after the b
until the end of the line as a label name and attempts to jump to a label a; }; stuff
. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff'
gives sed: 1: "ba; stuff": undefined label 'a; stuff'
.– Wintermute
Mar 30 '15 at 21:52
Either works. BSD sed does not accept just
-i
without an argument, though (but -i ''
works). By the way: the reason b
instructions don't work in one-liners in BSD sed is that something like ba; }; stuff
takes everything after the b
until the end of the line as a label name and attempts to jump to a label a; }; stuff
. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: sed 'ba; stuff'
gives sed: 1: "ba; stuff": undefined label 'a; stuff'
.– Wintermute
Mar 30 '15 at 21:52
2
2
Chaining commands with
;
in BSD sed is fine. Vis-a-vis
, the thing to know is that with BSD sed, you need a ;
before the }
that GNU sed doesn't require (p
vs. p;
). As for the code: 1h
copies the current line to the hold buffer if it is the first. 1!H
appends it there if it isn't the first. $ ...
does something if the current line is the last (at that point the whole file is in the hold buffer). x
swaps hold buffer and pattern space, s/admin,//
removes the first admin,
from the PS, p
prints the PS. The -n
option disables auto-printing.– Wintermute
Mar 30 '15 at 22:12
Chaining commands with
;
in BSD sed is fine. Vis-a-vis
, the thing to know is that with BSD sed, you need a ;
before the }
that GNU sed doesn't require (p
vs. p;
). As for the code: 1h
copies the current line to the hold buffer if it is the first. 1!H
appends it there if it isn't the first. $ ...
does something if the current line is the last (at that point the whole file is in the hold buffer). x
swaps hold buffer and pattern space, s/admin,//
removes the first admin,
from the PS, p
prints the PS. The -n
option disables auto-printing.– Wintermute
Mar 30 '15 at 22:12
1
1
@Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.
– John1024
Mar 30 '15 at 22:19
@Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place.
– John1024
Mar 30 '15 at 22:19
|
show 5 more comments
Using sed, without reading the whole file:
sed 's/admin,//;tl;b;:l;n;bl' file
This waits for a successful substitution and then goes into a loop of printing the rest of the file.
s/admin,//;tl;b;
: substitute admin with empty string, if substitution occurs jump to labell
otherwise print the line and exit (next line will be processed the same way).:l;n;bl
: define label l, read line, jump to l.
add a comment |
Using sed, without reading the whole file:
sed 's/admin,//;tl;b;:l;n;bl' file
This waits for a successful substitution and then goes into a loop of printing the rest of the file.
s/admin,//;tl;b;
: substitute admin with empty string, if substitution occurs jump to labell
otherwise print the line and exit (next line will be processed the same way).:l;n;bl
: define label l, read line, jump to l.
add a comment |
Using sed, without reading the whole file:
sed 's/admin,//;tl;b;:l;n;bl' file
This waits for a successful substitution and then goes into a loop of printing the rest of the file.
s/admin,//;tl;b;
: substitute admin with empty string, if substitution occurs jump to labell
otherwise print the line and exit (next line will be processed the same way).:l;n;bl
: define label l, read line, jump to l.
Using sed, without reading the whole file:
sed 's/admin,//;tl;b;:l;n;bl' file
This waits for a successful substitution and then goes into a loop of printing the rest of the file.
s/admin,//;tl;b;
: substitute admin with empty string, if substitution occurs jump to labell
otherwise print the line and exit (next line will be processed the same way).:l;n;bl
: define label l, read line, jump to l.
answered Nov 13 '18 at 12:19
perrealperreal
72.8k10111140
72.8k10111140
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f29356239%2fsed-command-to-remove-first-occurrence-of-a-word%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