sed on osx - extract all text that is between square brackets
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Given this stream:
[foo] 123 [bar]
[gar] dsa [har] 345
[uf] 88 [gc] 43 [br]
I want to process this with sed (or anything else) so the output would be
foo bar
gar har
uf gc br
Tried cat myfile | sed -e 's/^.*[//;s/].*$//'
But it gives me only the last instance
My real input is something like:
53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]
So I'd like to get for the first line:
the contacts are duplicated https://trello.com/c/663
Thanks
sed osx regular-expression
New contributor
add a comment |Â
up vote
2
down vote
favorite
Given this stream:
[foo] 123 [bar]
[gar] dsa [har] 345
[uf] 88 [gc] 43 [br]
I want to process this with sed (or anything else) so the output would be
foo bar
gar har
uf gc br
Tried cat myfile | sed -e 's/^.*[//;s/].*$//'
But it gives me only the last instance
My real input is something like:
53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]
So I'd like to get for the first line:
the contacts are duplicated https://trello.com/c/663
Thanks
sed osx regular-expression
New contributor
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Given this stream:
[foo] 123 [bar]
[gar] dsa [har] 345
[uf] 88 [gc] 43 [br]
I want to process this with sed (or anything else) so the output would be
foo bar
gar har
uf gc br
Tried cat myfile | sed -e 's/^.*[//;s/].*$//'
But it gives me only the last instance
My real input is something like:
53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]
So I'd like to get for the first line:
the contacts are duplicated https://trello.com/c/663
Thanks
sed osx regular-expression
New contributor
Given this stream:
[foo] 123 [bar]
[gar] dsa [har] 345
[uf] 88 [gc] 43 [br]
I want to process this with sed (or anything else) so the output would be
foo bar
gar har
uf gc br
Tried cat myfile | sed -e 's/^.*[//;s/].*$//'
But it gives me only the last instance
My real input is something like:
53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]
So I'd like to get for the first line:
the contacts are duplicated https://trello.com/c/663
Thanks
sed osx regular-expression
sed osx regular-expression
New contributor
New contributor
edited 2 hours ago
New contributor
asked 2 hours ago
YardenST
1135
1135
New contributor
New contributor
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
3
down vote
accepted
awk works well for this too: using [
or ]
as the field separator, print every even-numbered field:
awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file
With sed, I'd write
sed -E 's/(^|])[^*($|[)/ /g' file
The sed silution adds leading and trailing spaces (per line).
â Isaac
1 hour ago
add a comment |Â
up vote
2
down vote
An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.
Since Perl should be available on macos by default, perhaps this is viable alternative.
Using Perl, you could say
perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'
(note that this adds a space at the end of the line)
For an explanation of the pattern, see https://regexr.com/41gi5
New contributor
Nice. An alternative:perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
â glenn jackman
1 hour ago
add a comment |Â
up vote
1
down vote
This seems to work:
$ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
foo bar
gar har
uf gc br
thanks, but I getsed: input: No such file or directory
â YardenST
2 hours ago
@YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
â Chris Stratton
2 hours ago
Thanks got it :) but it does not seem to work with my input. My input is coming fromgit log commit1..commit2 --oneline
. Ive updated the question with an example
â YardenST
2 hours ago
add a comment |Â
up vote
1
down vote
sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '
The algorithm is:
ignore lines that do not contain brackets,
remove text before first bracket,
replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,
and remove the initial space, leaving only spaces in between.
add a comment |Â
up vote
1
down vote
This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, repÃÂeatly.
$ sed 's/[^*[([^]]*)][^*/1 /g' file
foo bar
gar har
uf gc br
Description:
sed ' # start a sed script
s/ # start a substitute command
[^* # match all leading characters (except [)
[ # match an explicit [
([^]]*) # capture text inside brackets.
] # match the closing ]
[^* # match trailing text (if any).
/ 1/ # replace everything matched by the captured text.
g # repeat for all the line.
' file # close script. Apply to file.
This add a trailing space per match. If that must be removed, add a removal at the end:
sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file
If you have GNU grep, this may help (one line per capture).
grep -Po '[K[^]]*(?=])'
And, if the above doesn't work, awk could also do it:
awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
awk works well for this too: using [
or ]
as the field separator, print every even-numbered field:
awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file
With sed, I'd write
sed -E 's/(^|])[^*($|[)/ /g' file
The sed silution adds leading and trailing spaces (per line).
â Isaac
1 hour ago
add a comment |Â
up vote
3
down vote
accepted
awk works well for this too: using [
or ]
as the field separator, print every even-numbered field:
awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file
With sed, I'd write
sed -E 's/(^|])[^*($|[)/ /g' file
The sed silution adds leading and trailing spaces (per line).
â Isaac
1 hour ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
awk works well for this too: using [
or ]
as the field separator, print every even-numbered field:
awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file
With sed, I'd write
sed -E 's/(^|])[^*($|[)/ /g' file
awk works well for this too: using [
or ]
as the field separator, print every even-numbered field:
awk -F '' 'for (i=2; i<=NF; i+=2) printf "%s ", $i; print ""' file
With sed, I'd write
sed -E 's/(^|])[^*($|[)/ /g' file
answered 1 hour ago
glenn jackman
49.3k467106
49.3k467106
The sed silution adds leading and trailing spaces (per line).
â Isaac
1 hour ago
add a comment |Â
The sed silution adds leading and trailing spaces (per line).
â Isaac
1 hour ago
The sed silution adds leading and trailing spaces (per line).
â Isaac
1 hour ago
The sed silution adds leading and trailing spaces (per line).
â Isaac
1 hour ago
add a comment |Â
up vote
2
down vote
An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.
Since Perl should be available on macos by default, perhaps this is viable alternative.
Using Perl, you could say
perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'
(note that this adds a space at the end of the line)
For an explanation of the pattern, see https://regexr.com/41gi5
New contributor
Nice. An alternative:perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
â glenn jackman
1 hour ago
add a comment |Â
up vote
2
down vote
An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.
Since Perl should be available on macos by default, perhaps this is viable alternative.
Using Perl, you could say
perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'
(note that this adds a space at the end of the line)
For an explanation of the pattern, see https://regexr.com/41gi5
New contributor
Nice. An alternative:perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
â glenn jackman
1 hour ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.
Since Perl should be available on macos by default, perhaps this is viable alternative.
Using Perl, you could say
perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'
(note that this adds a space at the end of the line)
For an explanation of the pattern, see https://regexr.com/41gi5
New contributor
An idiomatic way to do that is using look around assertions, see e.g. https://www.regular-expressions.info/lookaround.html, but these are not supported in sed, only in PCRE compliant RE processors.
Since Perl should be available on macos by default, perhaps this is viable alternative.
Using Perl, you could say
perl -pe 's/.+?(?<=[)(.+?)(?=]).+?/$1 /g'
(note that this adds a space at the end of the line)
For an explanation of the pattern, see https://regexr.com/41gi5
New contributor
New contributor
answered 1 hour ago
wwerner
1212
1212
New contributor
New contributor
Nice. An alternative:perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
â glenn jackman
1 hour ago
add a comment |Â
Nice. An alternative:perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
â glenn jackman
1 hour ago
Nice. An alternative:
perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
â glenn jackman
1 hour ago
Nice. An alternative:
perl -nE 'say join " ", (/ (?<=[) .*? (?=]) /xg)' file
â glenn jackman
1 hour ago
add a comment |Â
up vote
1
down vote
This seems to work:
$ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
foo bar
gar har
uf gc br
thanks, but I getsed: input: No such file or directory
â YardenST
2 hours ago
@YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
â Chris Stratton
2 hours ago
Thanks got it :) but it does not seem to work with my input. My input is coming fromgit log commit1..commit2 --oneline
. Ive updated the question with an example
â YardenST
2 hours ago
add a comment |Â
up vote
1
down vote
This seems to work:
$ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
foo bar
gar har
uf gc br
thanks, but I getsed: input: No such file or directory
â YardenST
2 hours ago
@YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
â Chris Stratton
2 hours ago
Thanks got it :) but it does not seem to work with my input. My input is coming fromgit log commit1..commit2 --oneline
. Ive updated the question with an example
â YardenST
2 hours ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This seems to work:
$ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
foo bar
gar har
uf gc br
This seems to work:
$ sed -E 's/ [^[a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d ''
foo bar
gar har
uf gc br
answered 2 hours ago
DopeGhoti
41.7k55180
41.7k55180
thanks, but I getsed: input: No such file or directory
â YardenST
2 hours ago
@YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
â Chris Stratton
2 hours ago
Thanks got it :) but it does not seem to work with my input. My input is coming fromgit log commit1..commit2 --oneline
. Ive updated the question with an example
â YardenST
2 hours ago
add a comment |Â
thanks, but I getsed: input: No such file or directory
â YardenST
2 hours ago
@YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
â Chris Stratton
2 hours ago
Thanks got it :) but it does not seem to work with my input. My input is coming fromgit log commit1..commit2 --oneline
. Ive updated the question with an example
â YardenST
2 hours ago
thanks, but I get
sed: input: No such file or directory
â YardenST
2 hours ago
thanks, but I get
sed: input: No such file or directory
â YardenST
2 hours ago
@YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
â Chris Stratton
2 hours ago
@YardenST "input" was a placeholder for an input file you would need to supply yours there or omit that and pipe in the source.
â Chris Stratton
2 hours ago
Thanks got it :) but it does not seem to work with my input. My input is coming from
git log commit1..commit2 --oneline
. Ive updated the question with an exampleâ YardenST
2 hours ago
Thanks got it :) but it does not seem to work with my input. My input is coming from
git log commit1..commit2 --oneline
. Ive updated the question with an exampleâ YardenST
2 hours ago
add a comment |Â
up vote
1
down vote
sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '
The algorithm is:
ignore lines that do not contain brackets,
remove text before first bracket,
replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,
and remove the initial space, leaving only spaces in between.
add a comment |Â
up vote
1
down vote
sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '
The algorithm is:
ignore lines that do not contain brackets,
remove text before first bracket,
replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,
and remove the initial space, leaving only spaces in between.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '
The algorithm is:
ignore lines that do not contain brackets,
remove text before first bracket,
replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,
and remove the initial space, leaving only spaces in between.
sed -n '/[/ s-[^*--; s-[([^]]*)][^*- 1-g; s- --p '
The algorithm is:
ignore lines that do not contain brackets,
remove text before first bracket,
replace pairs of backets and optional trailing text by spaces, leaving the text inside the brackets,
and remove the initial space, leaving only spaces in between.
answered 2 hours ago
Juergen
787
787
add a comment |Â
add a comment |Â
up vote
1
down vote
This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, repÃÂeatly.
$ sed 's/[^*[([^]]*)][^*/1 /g' file
foo bar
gar har
uf gc br
Description:
sed ' # start a sed script
s/ # start a substitute command
[^* # match all leading characters (except [)
[ # match an explicit [
([^]]*) # capture text inside brackets.
] # match the closing ]
[^* # match trailing text (if any).
/ 1/ # replace everything matched by the captured text.
g # repeat for all the line.
' file # close script. Apply to file.
This add a trailing space per match. If that must be removed, add a removal at the end:
sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file
If you have GNU grep, this may help (one line per capture).
grep -Po '[K[^]]*(?=])'
And, if the above doesn't work, awk could also do it:
awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file
add a comment |Â
up vote
1
down vote
This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, repÃÂeatly.
$ sed 's/[^*[([^]]*)][^*/1 /g' file
foo bar
gar har
uf gc br
Description:
sed ' # start a sed script
s/ # start a substitute command
[^* # match all leading characters (except [)
[ # match an explicit [
([^]]*) # capture text inside brackets.
] # match the closing ]
[^* # match trailing text (if any).
/ 1/ # replace everything matched by the captured text.
g # repeat for all the line.
' file # close script. Apply to file.
This add a trailing space per match. If that must be removed, add a removal at the end:
sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file
If you have GNU grep, this may help (one line per capture).
grep -Po '[K[^]]*(?=])'
And, if the above doesn't work, awk could also do it:
awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, repÃÂeatly.
$ sed 's/[^*[([^]]*)][^*/1 /g' file
foo bar
gar har
uf gc br
Description:
sed ' # start a sed script
s/ # start a substitute command
[^* # match all leading characters (except [)
[ # match an explicit [
([^]]*) # capture text inside brackets.
] # match the closing ]
[^* # match trailing text (if any).
/ 1/ # replace everything matched by the captured text.
g # repeat for all the line.
' file # close script. Apply to file.
This add a trailing space per match. If that must be removed, add a removal at the end:
sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file
If you have GNU grep, this may help (one line per capture).
grep -Po '[K[^]]*(?=])'
And, if the above doesn't work, awk could also do it:
awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file
This will match anything inside the first (opening) square bracket to the first (closing) square backet that follows, repÃÂeatly.
$ sed 's/[^*[([^]]*)][^*/1 /g' file
foo bar
gar har
uf gc br
Description:
sed ' # start a sed script
s/ # start a substitute command
[^* # match all leading characters (except [)
[ # match an explicit [
([^]]*) # capture text inside brackets.
] # match the closing ]
[^* # match trailing text (if any).
/ 1/ # replace everything matched by the captured text.
g # repeat for all the line.
' file # close script. Apply to file.
This add a trailing space per match. If that must be removed, add a removal at the end:
sed -e 's/[^*[([^]]*)][^*/1 /g' -e 's/ $//' file
If you have GNU grep, this may help (one line per capture).
grep -Po '[K[^]]*(?=])'
And, if the above doesn't work, awk could also do it:
awk 'print gensub(/[([^]]*)][^*/,"\1 ","g")' file
edited 52 mins ago
answered 1 hour ago
Isaac
8,50011140
8,50011140
add a comment |Â
add a comment |Â
YardenST is a new contributor. Be nice, and check out our Code of Conduct.
YardenST is a new contributor. Be nice, and check out our Code of Conduct.
YardenST is a new contributor. Be nice, and check out our Code of Conduct.
YardenST is a new contributor. Be nice, and check out our Code of Conduct.
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%2f476352%2fsed-on-osx-extract-all-text-that-is-between-square-brackets%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