BSD sed vs GNU - is it capable of nested matches?
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
This works perfectly well on any Linux :
$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar
But fails on OSXs ancient BSD variant :
⯠echo foo bar | sed -n '/foo//bar/;p;'
sed: 1: "/foo//bar/;p;": extra characters at the end of } command
Am I missing some magical incantation?
Is there a way to write this in a portable manner ?
I'd hate to have to revert to a pipeline of grep | grep | grep
commands.
Update : low rep here so can't upvote but thanks all repliers for your well considered advice.
sed osx bsd compatibility
add a comment |Â
up vote
4
down vote
favorite
This works perfectly well on any Linux :
$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar
But fails on OSXs ancient BSD variant :
⯠echo foo bar | sed -n '/foo//bar/;p;'
sed: 1: "/foo//bar/;p;": extra characters at the end of } command
Am I missing some magical incantation?
Is there a way to write this in a portable manner ?
I'd hate to have to revert to a pipeline of grep | grep | grep
commands.
Update : low rep here so can't upvote but thanks all repliers for your well considered advice.
sed osx bsd compatibility
1
If you want the lines where all the keywords appear, you could also invert the condition to make it more linear:sed '/foo/!d; /bar/!d'
â ilkkachu
May 16 at 16:16
Great pointer @ilkkachu - thanks
â Bryan Hunt
May 17 at 10:24
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
This works perfectly well on any Linux :
$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar
But fails on OSXs ancient BSD variant :
⯠echo foo bar | sed -n '/foo//bar/;p;'
sed: 1: "/foo//bar/;p;": extra characters at the end of } command
Am I missing some magical incantation?
Is there a way to write this in a portable manner ?
I'd hate to have to revert to a pipeline of grep | grep | grep
commands.
Update : low rep here so can't upvote but thanks all repliers for your well considered advice.
sed osx bsd compatibility
This works perfectly well on any Linux :
$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar
But fails on OSXs ancient BSD variant :
⯠echo foo bar | sed -n '/foo//bar/;p;'
sed: 1: "/foo//bar/;p;": extra characters at the end of } command
Am I missing some magical incantation?
Is there a way to write this in a portable manner ?
I'd hate to have to revert to a pipeline of grep | grep | grep
commands.
Update : low rep here so can't upvote but thanks all repliers for your well considered advice.
sed osx bsd compatibility
edited May 17 at 8:15
asked May 16 at 15:56
Bryan Hunt
234
234
1
If you want the lines where all the keywords appear, you could also invert the condition to make it more linear:sed '/foo/!d; /bar/!d'
â ilkkachu
May 16 at 16:16
Great pointer @ilkkachu - thanks
â Bryan Hunt
May 17 at 10:24
add a comment |Â
1
If you want the lines where all the keywords appear, you could also invert the condition to make it more linear:sed '/foo/!d; /bar/!d'
â ilkkachu
May 16 at 16:16
Great pointer @ilkkachu - thanks
â Bryan Hunt
May 17 at 10:24
1
1
If you want the lines where all the keywords appear, you could also invert the condition to make it more linear:
sed '/foo/!d; /bar/!d'
â ilkkachu
May 16 at 16:16
If you want the lines where all the keywords appear, you could also invert the condition to make it more linear:
sed '/foo/!d; /bar/!d'
â ilkkachu
May 16 at 16:16
Great pointer @ilkkachu - thanks
â Bryan Hunt
May 17 at 10:24
Great pointer @ilkkachu - thanks
â Bryan Hunt
May 17 at 10:24
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
A sed
editing command should be terminated by ;
or a literal newline. GNU sed
is very forgiving about this.
Your script:
/foo//bar/;p;
Expanded:
/foo/
/bar/
p
This would work as a sed
script fed to sed
through -f
.
If we make sure to replace newlines with ;
(only needed at the end of commands and ...
groups of commands) so that we can use it on the command line, we get
/foo//bar/p;;
This works with OpenBSD sed
(the original did not, due to that second ;
missing).
In this particular case, this may be further simplified to
/foo//bar/p;
add a comment |Â
up vote
2
down vote
This appears to work on BSD sed
:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
foo bar
As does two layers of nesting:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
foo bar
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
A sed
editing command should be terminated by ;
or a literal newline. GNU sed
is very forgiving about this.
Your script:
/foo//bar/;p;
Expanded:
/foo/
/bar/
p
This would work as a sed
script fed to sed
through -f
.
If we make sure to replace newlines with ;
(only needed at the end of commands and ...
groups of commands) so that we can use it on the command line, we get
/foo//bar/p;;
This works with OpenBSD sed
(the original did not, due to that second ;
missing).
In this particular case, this may be further simplified to
/foo//bar/p;
add a comment |Â
up vote
5
down vote
accepted
A sed
editing command should be terminated by ;
or a literal newline. GNU sed
is very forgiving about this.
Your script:
/foo//bar/;p;
Expanded:
/foo/
/bar/
p
This would work as a sed
script fed to sed
through -f
.
If we make sure to replace newlines with ;
(only needed at the end of commands and ...
groups of commands) so that we can use it on the command line, we get
/foo//bar/p;;
This works with OpenBSD sed
(the original did not, due to that second ;
missing).
In this particular case, this may be further simplified to
/foo//bar/p;
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
A sed
editing command should be terminated by ;
or a literal newline. GNU sed
is very forgiving about this.
Your script:
/foo//bar/;p;
Expanded:
/foo/
/bar/
p
This would work as a sed
script fed to sed
through -f
.
If we make sure to replace newlines with ;
(only needed at the end of commands and ...
groups of commands) so that we can use it on the command line, we get
/foo//bar/p;;
This works with OpenBSD sed
(the original did not, due to that second ;
missing).
In this particular case, this may be further simplified to
/foo//bar/p;
A sed
editing command should be terminated by ;
or a literal newline. GNU sed
is very forgiving about this.
Your script:
/foo//bar/;p;
Expanded:
/foo/
/bar/
p
This would work as a sed
script fed to sed
through -f
.
If we make sure to replace newlines with ;
(only needed at the end of commands and ...
groups of commands) so that we can use it on the command line, we get
/foo//bar/p;;
This works with OpenBSD sed
(the original did not, due to that second ;
missing).
In this particular case, this may be further simplified to
/foo//bar/p;
edited May 16 at 16:19
answered May 16 at 16:02
Kusalananda
102k13199315
102k13199315
add a comment |Â
add a comment |Â
up vote
2
down vote
This appears to work on BSD sed
:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
foo bar
As does two layers of nesting:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
foo bar
add a comment |Â
up vote
2
down vote
This appears to work on BSD sed
:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
foo bar
As does two layers of nesting:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
foo bar
add a comment |Â
up vote
2
down vote
up vote
2
down vote
This appears to work on BSD sed
:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
foo bar
As does two layers of nesting:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
foo bar
This appears to work on BSD sed
:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
foo bar
As does two layers of nesting:
$ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
foo bar
answered May 16 at 16:02
DopeGhoti
40k54779
40k54779
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444189%2fbsd-sed-vs-gnu-is-it-capable-of-nested-matches%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
If you want the lines where all the keywords appear, you could also invert the condition to make it more linear:
sed '/foo/!d; /bar/!d'
â ilkkachu
May 16 at 16:16
Great pointer @ilkkachu - thanks
â Bryan Hunt
May 17 at 10:24