Cryptic sed command syntax confusion
Can someone explain, how this sed command works here?
pkg info | sed -e 's/([^.]*).*/1/' -e 's/(.*)-.*/1/'
This command removes version numbers from packages and prints into stdout like this
yajl-2.1.0 Portable JSON parsing and serialization library in ANSI C
youtube_dl-2018.12.03 Program for downloading videos from YouTube.com
zathura-0.4.1 Customizable lightweight pdf viewer
zathura-pdf-poppler-0.2.9_1 Poppler render PDF plugin for Zathura PDF viewer
zip-3.0_1 Create/update ZIP files compatible with PKZIP
zsh-5.6.2 The Z shell
and turns into this
yajl
youtube_dl
zathura
zathura-pdf-poppler
zip
zsh
But I am having a hard time understanding the parts ([^.]*).*
(.*)-.*
. I understand the case of ,
-e
, s
. But those wildcards seems very cryptic here.
regex unix sed
add a comment |
Can someone explain, how this sed command works here?
pkg info | sed -e 's/([^.]*).*/1/' -e 's/(.*)-.*/1/'
This command removes version numbers from packages and prints into stdout like this
yajl-2.1.0 Portable JSON parsing and serialization library in ANSI C
youtube_dl-2018.12.03 Program for downloading videos from YouTube.com
zathura-0.4.1 Customizable lightweight pdf viewer
zathura-pdf-poppler-0.2.9_1 Poppler render PDF plugin for Zathura PDF viewer
zip-3.0_1 Create/update ZIP files compatible with PKZIP
zsh-5.6.2 The Z shell
and turns into this
yajl
youtube_dl
zathura
zathura-pdf-poppler
zip
zsh
But I am having a hard time understanding the parts ([^.]*).*
(.*)-.*
. I understand the case of ,
-e
, s
. But those wildcards seems very cryptic here.
regex unix sed
The above commands could have been writtensed 's/..*//;s/-[^-]*$//' file
which perhaps makes them clearer.
– potong
Dec 30 '18 at 11:44
add a comment |
Can someone explain, how this sed command works here?
pkg info | sed -e 's/([^.]*).*/1/' -e 's/(.*)-.*/1/'
This command removes version numbers from packages and prints into stdout like this
yajl-2.1.0 Portable JSON parsing and serialization library in ANSI C
youtube_dl-2018.12.03 Program for downloading videos from YouTube.com
zathura-0.4.1 Customizable lightweight pdf viewer
zathura-pdf-poppler-0.2.9_1 Poppler render PDF plugin for Zathura PDF viewer
zip-3.0_1 Create/update ZIP files compatible with PKZIP
zsh-5.6.2 The Z shell
and turns into this
yajl
youtube_dl
zathura
zathura-pdf-poppler
zip
zsh
But I am having a hard time understanding the parts ([^.]*).*
(.*)-.*
. I understand the case of ,
-e
, s
. But those wildcards seems very cryptic here.
regex unix sed
Can someone explain, how this sed command works here?
pkg info | sed -e 's/([^.]*).*/1/' -e 's/(.*)-.*/1/'
This command removes version numbers from packages and prints into stdout like this
yajl-2.1.0 Portable JSON parsing and serialization library in ANSI C
youtube_dl-2018.12.03 Program for downloading videos from YouTube.com
zathura-0.4.1 Customizable lightweight pdf viewer
zathura-pdf-poppler-0.2.9_1 Poppler render PDF plugin for Zathura PDF viewer
zip-3.0_1 Create/update ZIP files compatible with PKZIP
zsh-5.6.2 The Z shell
and turns into this
yajl
youtube_dl
zathura
zathura-pdf-poppler
zip
zsh
But I am having a hard time understanding the parts ([^.]*).*
(.*)-.*
. I understand the case of ,
-e
, s
. But those wildcards seems very cryptic here.
regex unix sed
regex unix sed
edited Dec 31 '18 at 7:13
Nikolai Lvovich
asked Dec 30 '18 at 9:30
Nikolai LvovichNikolai Lvovich
105
105
The above commands could have been writtensed 's/..*//;s/-[^-]*$//' file
which perhaps makes them clearer.
– potong
Dec 30 '18 at 11:44
add a comment |
The above commands could have been writtensed 's/..*//;s/-[^-]*$//' file
which perhaps makes them clearer.
– potong
Dec 30 '18 at 11:44
The above commands could have been written
sed 's/..*//;s/-[^-]*$//' file
which perhaps makes them clearer.– potong
Dec 30 '18 at 11:44
The above commands could have been written
sed 's/..*//;s/-[^-]*$//' file
which perhaps makes them clearer.– potong
Dec 30 '18 at 11:44
add a comment |
3 Answers
3
active
oldest
votes
In your regex ([^.]*).*
, (
which actually is (
is the start of a capturing group and then [^.]*
captures every character except a literal dot and *
means zero or more, then )
is the mark of closing of group that we started, then .*
captures whatever remains after capturing group1.
Similar will be the explanation for (.*)-.*
regex, where (.*)
will capture everything greedily in capturing group but will stop at last hyphen -
and then will match hyphen and further .*
will match remaining text.
To explain with an example, lets take youtube_dl-2018.12.03
.
Here, ([^.]*)
will capture everything until dot, hence it will capture youtube_dl-2018
and then remaining .*
will capture .12.03
. Then it will be replaced by 1
which means youtube_dl-2018
will be passed to the next regex -e 's/(.*)-.*/1/'
.
Then in your second regex, (.*)-.*
, (.*)
will capture youtube_dl
and put in group1 because after that there is a hyphen and .*
will capture remaining text which is 2018
. And as it is replaced by 1
hence final text will become youtube_dl
.
Seeing your data, I believe, you can also simplify your command to this, as your first regex in sed
command seems redundant. Try this following command and see if it outputs same result?
pkg info | sed -e 's/(.*)-.*/1/'
You can only use this simplified command, as none of your data contains a .
before a -
, otherwise you should use your own command which has two sed
rules.
Also, on another note, if you use -r
, (or -E for OS X), for extended regex, you don't need to escape the parentheses and you can write your regex as,
pkg info | sed -r 's/([^.]*).*/1/' -r 's/(.*)-.*/1/'
Thanks @ agc for formatting and refining my answer :)
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 5:43
1
Understood. Thanks for the explanation :)
– Nikolai Lvovich
Dec 31 '18 at 7:27
@NikolaiLvovich: If my answer helped you, I would appreciate accepting/upvoting my answer. Thank you.
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 7:42
1
Done. sed -E is awesome :)
– Nikolai Lvovich
Dec 31 '18 at 8:21
add a comment |
It is a difficult way for saying:
Remove all substrings starting with a dot or hypen.
The part before the delimiter is matched and remembered.
Alternatives:
# Incorrect: removes from first, not last hypen:
# pkg info | sed 's/[-.].*//'
# pkg info | cut -d "-" -f1 | cut -d"." -f1
# pkg info | awk -F "-|[.]" '{print $1}'
# The dot is not needed when you remove the substring starting with the last hypen
pkg info | sed 's/-[^-]*$//'
pkg info | rev | cut -d"-" -f2- | rev
pkg info | awk -F "[.]" '{print $1}' | awk -F "[-]" -vOFS='-' 'NF>1 { NF--;print;}'
2
Your logic for substitution needs a little correction due to which it will not work correctly forzathura-pdf-poppler-0.2.9_1
string. Your commands will outputzathura
where as actual output should bezathura-pdf-poppler
. Hence, the correct statement should be Remove all substrings starting with "LAST" occurrence of hyphen. as package names can have hyphen within them and at the same time, hyphen is also the separator for package name and version, which makes the dot as redundant in the regex.
– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 17:30
Tx, I corrected the answer.
– Walter A
Dec 31 '18 at 8:14
add a comment |
Silly invisible-text GNU
grep
method that works on the console,
but which would fail if sent to a file or piped to a filter:
pkg info | GREP_COLORS='ms=30;30;30' grep '-[^-]*s.*$'
How it works:
grep
is used to find the last hyphen before a
space, and everything after that, (i.e. everything we don't want
to see), whichgrep
shows in highlighted colors as defined in the
GREP_COLORS
environmental variable. Since the highlight colors
30;30;30
is a black font, (on a black background), the unwanted
text is invisible.
If the terminal background is already black,
GREP_COLORS='ms=30
would be sufficient.
sed
method based on not printing thegrep
regex:
pkg info | sed 's#(^.*)(-[^-]*[[:space:]].*$)#1#'
...this method can be sent to pipes and filters. Shorter version using GNU
sed
:
pkg info | sed 's#(^.*)(-.*s.*)#1#'
1
No idea why this is not working on FreeBSD. Maybe this is GNU grep only feature?
– Nikolai Lvovich
Jan 1 at 7:26
@NikolaiLvovich, Probably is a GNU feature at that, based on a skim of BSDman sed
.
– agc
Jan 1 at 7:45
Interesting! The sed method is working a little bit.
– Nikolai Lvovich
Jan 1 at 8:17
@NikolaiLvovich, Re "working a little bit": Please give specifics.
– agc
Jan 1 at 16:28
Few still getting printed this way: p5-Digest-HMAC-1.03_1 Perl5 interface to HMAC Message
– Nikolai Lvovich
Jan 2 at 12:16
|
show 1 more 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%2f53976504%2fcryptic-sed-command-syntax-confusion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your regex ([^.]*).*
, (
which actually is (
is the start of a capturing group and then [^.]*
captures every character except a literal dot and *
means zero or more, then )
is the mark of closing of group that we started, then .*
captures whatever remains after capturing group1.
Similar will be the explanation for (.*)-.*
regex, where (.*)
will capture everything greedily in capturing group but will stop at last hyphen -
and then will match hyphen and further .*
will match remaining text.
To explain with an example, lets take youtube_dl-2018.12.03
.
Here, ([^.]*)
will capture everything until dot, hence it will capture youtube_dl-2018
and then remaining .*
will capture .12.03
. Then it will be replaced by 1
which means youtube_dl-2018
will be passed to the next regex -e 's/(.*)-.*/1/'
.
Then in your second regex, (.*)-.*
, (.*)
will capture youtube_dl
and put in group1 because after that there is a hyphen and .*
will capture remaining text which is 2018
. And as it is replaced by 1
hence final text will become youtube_dl
.
Seeing your data, I believe, you can also simplify your command to this, as your first regex in sed
command seems redundant. Try this following command and see if it outputs same result?
pkg info | sed -e 's/(.*)-.*/1/'
You can only use this simplified command, as none of your data contains a .
before a -
, otherwise you should use your own command which has two sed
rules.
Also, on another note, if you use -r
, (or -E for OS X), for extended regex, you don't need to escape the parentheses and you can write your regex as,
pkg info | sed -r 's/([^.]*).*/1/' -r 's/(.*)-.*/1/'
Thanks @ agc for formatting and refining my answer :)
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 5:43
1
Understood. Thanks for the explanation :)
– Nikolai Lvovich
Dec 31 '18 at 7:27
@NikolaiLvovich: If my answer helped you, I would appreciate accepting/upvoting my answer. Thank you.
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 7:42
1
Done. sed -E is awesome :)
– Nikolai Lvovich
Dec 31 '18 at 8:21
add a comment |
In your regex ([^.]*).*
, (
which actually is (
is the start of a capturing group and then [^.]*
captures every character except a literal dot and *
means zero or more, then )
is the mark of closing of group that we started, then .*
captures whatever remains after capturing group1.
Similar will be the explanation for (.*)-.*
regex, where (.*)
will capture everything greedily in capturing group but will stop at last hyphen -
and then will match hyphen and further .*
will match remaining text.
To explain with an example, lets take youtube_dl-2018.12.03
.
Here, ([^.]*)
will capture everything until dot, hence it will capture youtube_dl-2018
and then remaining .*
will capture .12.03
. Then it will be replaced by 1
which means youtube_dl-2018
will be passed to the next regex -e 's/(.*)-.*/1/'
.
Then in your second regex, (.*)-.*
, (.*)
will capture youtube_dl
and put in group1 because after that there is a hyphen and .*
will capture remaining text which is 2018
. And as it is replaced by 1
hence final text will become youtube_dl
.
Seeing your data, I believe, you can also simplify your command to this, as your first regex in sed
command seems redundant. Try this following command and see if it outputs same result?
pkg info | sed -e 's/(.*)-.*/1/'
You can only use this simplified command, as none of your data contains a .
before a -
, otherwise you should use your own command which has two sed
rules.
Also, on another note, if you use -r
, (or -E for OS X), for extended regex, you don't need to escape the parentheses and you can write your regex as,
pkg info | sed -r 's/([^.]*).*/1/' -r 's/(.*)-.*/1/'
Thanks @ agc for formatting and refining my answer :)
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 5:43
1
Understood. Thanks for the explanation :)
– Nikolai Lvovich
Dec 31 '18 at 7:27
@NikolaiLvovich: If my answer helped you, I would appreciate accepting/upvoting my answer. Thank you.
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 7:42
1
Done. sed -E is awesome :)
– Nikolai Lvovich
Dec 31 '18 at 8:21
add a comment |
In your regex ([^.]*).*
, (
which actually is (
is the start of a capturing group and then [^.]*
captures every character except a literal dot and *
means zero or more, then )
is the mark of closing of group that we started, then .*
captures whatever remains after capturing group1.
Similar will be the explanation for (.*)-.*
regex, where (.*)
will capture everything greedily in capturing group but will stop at last hyphen -
and then will match hyphen and further .*
will match remaining text.
To explain with an example, lets take youtube_dl-2018.12.03
.
Here, ([^.]*)
will capture everything until dot, hence it will capture youtube_dl-2018
and then remaining .*
will capture .12.03
. Then it will be replaced by 1
which means youtube_dl-2018
will be passed to the next regex -e 's/(.*)-.*/1/'
.
Then in your second regex, (.*)-.*
, (.*)
will capture youtube_dl
and put in group1 because after that there is a hyphen and .*
will capture remaining text which is 2018
. And as it is replaced by 1
hence final text will become youtube_dl
.
Seeing your data, I believe, you can also simplify your command to this, as your first regex in sed
command seems redundant. Try this following command and see if it outputs same result?
pkg info | sed -e 's/(.*)-.*/1/'
You can only use this simplified command, as none of your data contains a .
before a -
, otherwise you should use your own command which has two sed
rules.
Also, on another note, if you use -r
, (or -E for OS X), for extended regex, you don't need to escape the parentheses and you can write your regex as,
pkg info | sed -r 's/([^.]*).*/1/' -r 's/(.*)-.*/1/'
In your regex ([^.]*).*
, (
which actually is (
is the start of a capturing group and then [^.]*
captures every character except a literal dot and *
means zero or more, then )
is the mark of closing of group that we started, then .*
captures whatever remains after capturing group1.
Similar will be the explanation for (.*)-.*
regex, where (.*)
will capture everything greedily in capturing group but will stop at last hyphen -
and then will match hyphen and further .*
will match remaining text.
To explain with an example, lets take youtube_dl-2018.12.03
.
Here, ([^.]*)
will capture everything until dot, hence it will capture youtube_dl-2018
and then remaining .*
will capture .12.03
. Then it will be replaced by 1
which means youtube_dl-2018
will be passed to the next regex -e 's/(.*)-.*/1/'
.
Then in your second regex, (.*)-.*
, (.*)
will capture youtube_dl
and put in group1 because after that there is a hyphen and .*
will capture remaining text which is 2018
. And as it is replaced by 1
hence final text will become youtube_dl
.
Seeing your data, I believe, you can also simplify your command to this, as your first regex in sed
command seems redundant. Try this following command and see if it outputs same result?
pkg info | sed -e 's/(.*)-.*/1/'
You can only use this simplified command, as none of your data contains a .
before a -
, otherwise you should use your own command which has two sed
rules.
Also, on another note, if you use -r
, (or -E for OS X), for extended regex, you don't need to escape the parentheses and you can write your regex as,
pkg info | sed -r 's/([^.]*).*/1/' -r 's/(.*)-.*/1/'
edited Dec 31 '18 at 5:36
agc
4,8021338
4,8021338
answered Dec 30 '18 at 10:35
Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi
6,6192927
6,6192927
Thanks @ agc for formatting and refining my answer :)
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 5:43
1
Understood. Thanks for the explanation :)
– Nikolai Lvovich
Dec 31 '18 at 7:27
@NikolaiLvovich: If my answer helped you, I would appreciate accepting/upvoting my answer. Thank you.
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 7:42
1
Done. sed -E is awesome :)
– Nikolai Lvovich
Dec 31 '18 at 8:21
add a comment |
Thanks @ agc for formatting and refining my answer :)
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 5:43
1
Understood. Thanks for the explanation :)
– Nikolai Lvovich
Dec 31 '18 at 7:27
@NikolaiLvovich: If my answer helped you, I would appreciate accepting/upvoting my answer. Thank you.
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 7:42
1
Done. sed -E is awesome :)
– Nikolai Lvovich
Dec 31 '18 at 8:21
Thanks @ agc for formatting and refining my answer :)
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 5:43
Thanks @ agc for formatting and refining my answer :)
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 5:43
1
1
Understood. Thanks for the explanation :)
– Nikolai Lvovich
Dec 31 '18 at 7:27
Understood. Thanks for the explanation :)
– Nikolai Lvovich
Dec 31 '18 at 7:27
@NikolaiLvovich: If my answer helped you, I would appreciate accepting/upvoting my answer. Thank you.
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 7:42
@NikolaiLvovich: If my answer helped you, I would appreciate accepting/upvoting my answer. Thank you.
– Pushpesh Kumar Rajwanshi
Dec 31 '18 at 7:42
1
1
Done. sed -E is awesome :)
– Nikolai Lvovich
Dec 31 '18 at 8:21
Done. sed -E is awesome :)
– Nikolai Lvovich
Dec 31 '18 at 8:21
add a comment |
It is a difficult way for saying:
Remove all substrings starting with a dot or hypen.
The part before the delimiter is matched and remembered.
Alternatives:
# Incorrect: removes from first, not last hypen:
# pkg info | sed 's/[-.].*//'
# pkg info | cut -d "-" -f1 | cut -d"." -f1
# pkg info | awk -F "-|[.]" '{print $1}'
# The dot is not needed when you remove the substring starting with the last hypen
pkg info | sed 's/-[^-]*$//'
pkg info | rev | cut -d"-" -f2- | rev
pkg info | awk -F "[.]" '{print $1}' | awk -F "[-]" -vOFS='-' 'NF>1 { NF--;print;}'
2
Your logic for substitution needs a little correction due to which it will not work correctly forzathura-pdf-poppler-0.2.9_1
string. Your commands will outputzathura
where as actual output should bezathura-pdf-poppler
. Hence, the correct statement should be Remove all substrings starting with "LAST" occurrence of hyphen. as package names can have hyphen within them and at the same time, hyphen is also the separator for package name and version, which makes the dot as redundant in the regex.
– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 17:30
Tx, I corrected the answer.
– Walter A
Dec 31 '18 at 8:14
add a comment |
It is a difficult way for saying:
Remove all substrings starting with a dot or hypen.
The part before the delimiter is matched and remembered.
Alternatives:
# Incorrect: removes from first, not last hypen:
# pkg info | sed 's/[-.].*//'
# pkg info | cut -d "-" -f1 | cut -d"." -f1
# pkg info | awk -F "-|[.]" '{print $1}'
# The dot is not needed when you remove the substring starting with the last hypen
pkg info | sed 's/-[^-]*$//'
pkg info | rev | cut -d"-" -f2- | rev
pkg info | awk -F "[.]" '{print $1}' | awk -F "[-]" -vOFS='-' 'NF>1 { NF--;print;}'
2
Your logic for substitution needs a little correction due to which it will not work correctly forzathura-pdf-poppler-0.2.9_1
string. Your commands will outputzathura
where as actual output should bezathura-pdf-poppler
. Hence, the correct statement should be Remove all substrings starting with "LAST" occurrence of hyphen. as package names can have hyphen within them and at the same time, hyphen is also the separator for package name and version, which makes the dot as redundant in the regex.
– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 17:30
Tx, I corrected the answer.
– Walter A
Dec 31 '18 at 8:14
add a comment |
It is a difficult way for saying:
Remove all substrings starting with a dot or hypen.
The part before the delimiter is matched and remembered.
Alternatives:
# Incorrect: removes from first, not last hypen:
# pkg info | sed 's/[-.].*//'
# pkg info | cut -d "-" -f1 | cut -d"." -f1
# pkg info | awk -F "-|[.]" '{print $1}'
# The dot is not needed when you remove the substring starting with the last hypen
pkg info | sed 's/-[^-]*$//'
pkg info | rev | cut -d"-" -f2- | rev
pkg info | awk -F "[.]" '{print $1}' | awk -F "[-]" -vOFS='-' 'NF>1 { NF--;print;}'
It is a difficult way for saying:
Remove all substrings starting with a dot or hypen.
The part before the delimiter is matched and remembered.
Alternatives:
# Incorrect: removes from first, not last hypen:
# pkg info | sed 's/[-.].*//'
# pkg info | cut -d "-" -f1 | cut -d"." -f1
# pkg info | awk -F "-|[.]" '{print $1}'
# The dot is not needed when you remove the substring starting with the last hypen
pkg info | sed 's/-[^-]*$//'
pkg info | rev | cut -d"-" -f2- | rev
pkg info | awk -F "[.]" '{print $1}' | awk -F "[-]" -vOFS='-' 'NF>1 { NF--;print;}'
edited Dec 31 '18 at 8:14
answered Dec 30 '18 at 14:45
Walter AWalter A
10.7k21031
10.7k21031
2
Your logic for substitution needs a little correction due to which it will not work correctly forzathura-pdf-poppler-0.2.9_1
string. Your commands will outputzathura
where as actual output should bezathura-pdf-poppler
. Hence, the correct statement should be Remove all substrings starting with "LAST" occurrence of hyphen. as package names can have hyphen within them and at the same time, hyphen is also the separator for package name and version, which makes the dot as redundant in the regex.
– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 17:30
Tx, I corrected the answer.
– Walter A
Dec 31 '18 at 8:14
add a comment |
2
Your logic for substitution needs a little correction due to which it will not work correctly forzathura-pdf-poppler-0.2.9_1
string. Your commands will outputzathura
where as actual output should bezathura-pdf-poppler
. Hence, the correct statement should be Remove all substrings starting with "LAST" occurrence of hyphen. as package names can have hyphen within them and at the same time, hyphen is also the separator for package name and version, which makes the dot as redundant in the regex.
– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 17:30
Tx, I corrected the answer.
– Walter A
Dec 31 '18 at 8:14
2
2
Your logic for substitution needs a little correction due to which it will not work correctly for
zathura-pdf-poppler-0.2.9_1
string. Your commands will output zathura
where as actual output should be zathura-pdf-poppler
. Hence, the correct statement should be Remove all substrings starting with "LAST" occurrence of hyphen. as package names can have hyphen within them and at the same time, hyphen is also the separator for package name and version, which makes the dot as redundant in the regex.– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 17:30
Your logic for substitution needs a little correction due to which it will not work correctly for
zathura-pdf-poppler-0.2.9_1
string. Your commands will output zathura
where as actual output should be zathura-pdf-poppler
. Hence, the correct statement should be Remove all substrings starting with "LAST" occurrence of hyphen. as package names can have hyphen within them and at the same time, hyphen is also the separator for package name and version, which makes the dot as redundant in the regex.– Pushpesh Kumar Rajwanshi
Dec 30 '18 at 17:30
Tx, I corrected the answer.
– Walter A
Dec 31 '18 at 8:14
Tx, I corrected the answer.
– Walter A
Dec 31 '18 at 8:14
add a comment |
Silly invisible-text GNU
grep
method that works on the console,
but which would fail if sent to a file or piped to a filter:
pkg info | GREP_COLORS='ms=30;30;30' grep '-[^-]*s.*$'
How it works:
grep
is used to find the last hyphen before a
space, and everything after that, (i.e. everything we don't want
to see), whichgrep
shows in highlighted colors as defined in the
GREP_COLORS
environmental variable. Since the highlight colors
30;30;30
is a black font, (on a black background), the unwanted
text is invisible.
If the terminal background is already black,
GREP_COLORS='ms=30
would be sufficient.
sed
method based on not printing thegrep
regex:
pkg info | sed 's#(^.*)(-[^-]*[[:space:]].*$)#1#'
...this method can be sent to pipes and filters. Shorter version using GNU
sed
:
pkg info | sed 's#(^.*)(-.*s.*)#1#'
1
No idea why this is not working on FreeBSD. Maybe this is GNU grep only feature?
– Nikolai Lvovich
Jan 1 at 7:26
@NikolaiLvovich, Probably is a GNU feature at that, based on a skim of BSDman sed
.
– agc
Jan 1 at 7:45
Interesting! The sed method is working a little bit.
– Nikolai Lvovich
Jan 1 at 8:17
@NikolaiLvovich, Re "working a little bit": Please give specifics.
– agc
Jan 1 at 16:28
Few still getting printed this way: p5-Digest-HMAC-1.03_1 Perl5 interface to HMAC Message
– Nikolai Lvovich
Jan 2 at 12:16
|
show 1 more comment
Silly invisible-text GNU
grep
method that works on the console,
but which would fail if sent to a file or piped to a filter:
pkg info | GREP_COLORS='ms=30;30;30' grep '-[^-]*s.*$'
How it works:
grep
is used to find the last hyphen before a
space, and everything after that, (i.e. everything we don't want
to see), whichgrep
shows in highlighted colors as defined in the
GREP_COLORS
environmental variable. Since the highlight colors
30;30;30
is a black font, (on a black background), the unwanted
text is invisible.
If the terminal background is already black,
GREP_COLORS='ms=30
would be sufficient.
sed
method based on not printing thegrep
regex:
pkg info | sed 's#(^.*)(-[^-]*[[:space:]].*$)#1#'
...this method can be sent to pipes and filters. Shorter version using GNU
sed
:
pkg info | sed 's#(^.*)(-.*s.*)#1#'
1
No idea why this is not working on FreeBSD. Maybe this is GNU grep only feature?
– Nikolai Lvovich
Jan 1 at 7:26
@NikolaiLvovich, Probably is a GNU feature at that, based on a skim of BSDman sed
.
– agc
Jan 1 at 7:45
Interesting! The sed method is working a little bit.
– Nikolai Lvovich
Jan 1 at 8:17
@NikolaiLvovich, Re "working a little bit": Please give specifics.
– agc
Jan 1 at 16:28
Few still getting printed this way: p5-Digest-HMAC-1.03_1 Perl5 interface to HMAC Message
– Nikolai Lvovich
Jan 2 at 12:16
|
show 1 more comment
Silly invisible-text GNU
grep
method that works on the console,
but which would fail if sent to a file or piped to a filter:
pkg info | GREP_COLORS='ms=30;30;30' grep '-[^-]*s.*$'
How it works:
grep
is used to find the last hyphen before a
space, and everything after that, (i.e. everything we don't want
to see), whichgrep
shows in highlighted colors as defined in the
GREP_COLORS
environmental variable. Since the highlight colors
30;30;30
is a black font, (on a black background), the unwanted
text is invisible.
If the terminal background is already black,
GREP_COLORS='ms=30
would be sufficient.
sed
method based on not printing thegrep
regex:
pkg info | sed 's#(^.*)(-[^-]*[[:space:]].*$)#1#'
...this method can be sent to pipes and filters. Shorter version using GNU
sed
:
pkg info | sed 's#(^.*)(-.*s.*)#1#'
Silly invisible-text GNU
grep
method that works on the console,
but which would fail if sent to a file or piped to a filter:
pkg info | GREP_COLORS='ms=30;30;30' grep '-[^-]*s.*$'
How it works:
grep
is used to find the last hyphen before a
space, and everything after that, (i.e. everything we don't want
to see), whichgrep
shows in highlighted colors as defined in the
GREP_COLORS
environmental variable. Since the highlight colors
30;30;30
is a black font, (on a black background), the unwanted
text is invisible.
If the terminal background is already black,
GREP_COLORS='ms=30
would be sufficient.
sed
method based on not printing thegrep
regex:
pkg info | sed 's#(^.*)(-[^-]*[[:space:]].*$)#1#'
...this method can be sent to pipes and filters. Shorter version using GNU
sed
:
pkg info | sed 's#(^.*)(-.*s.*)#1#'
edited Jan 1 at 16:27
answered Jan 1 at 5:58
agcagc
4,8021338
4,8021338
1
No idea why this is not working on FreeBSD. Maybe this is GNU grep only feature?
– Nikolai Lvovich
Jan 1 at 7:26
@NikolaiLvovich, Probably is a GNU feature at that, based on a skim of BSDman sed
.
– agc
Jan 1 at 7:45
Interesting! The sed method is working a little bit.
– Nikolai Lvovich
Jan 1 at 8:17
@NikolaiLvovich, Re "working a little bit": Please give specifics.
– agc
Jan 1 at 16:28
Few still getting printed this way: p5-Digest-HMAC-1.03_1 Perl5 interface to HMAC Message
– Nikolai Lvovich
Jan 2 at 12:16
|
show 1 more comment
1
No idea why this is not working on FreeBSD. Maybe this is GNU grep only feature?
– Nikolai Lvovich
Jan 1 at 7:26
@NikolaiLvovich, Probably is a GNU feature at that, based on a skim of BSDman sed
.
– agc
Jan 1 at 7:45
Interesting! The sed method is working a little bit.
– Nikolai Lvovich
Jan 1 at 8:17
@NikolaiLvovich, Re "working a little bit": Please give specifics.
– agc
Jan 1 at 16:28
Few still getting printed this way: p5-Digest-HMAC-1.03_1 Perl5 interface to HMAC Message
– Nikolai Lvovich
Jan 2 at 12:16
1
1
No idea why this is not working on FreeBSD. Maybe this is GNU grep only feature?
– Nikolai Lvovich
Jan 1 at 7:26
No idea why this is not working on FreeBSD. Maybe this is GNU grep only feature?
– Nikolai Lvovich
Jan 1 at 7:26
@NikolaiLvovich, Probably is a GNU feature at that, based on a skim of BSD
man sed
.– agc
Jan 1 at 7:45
@NikolaiLvovich, Probably is a GNU feature at that, based on a skim of BSD
man sed
.– agc
Jan 1 at 7:45
Interesting! The sed method is working a little bit.
– Nikolai Lvovich
Jan 1 at 8:17
Interesting! The sed method is working a little bit.
– Nikolai Lvovich
Jan 1 at 8:17
@NikolaiLvovich, Re "working a little bit": Please give specifics.
– agc
Jan 1 at 16:28
@NikolaiLvovich, Re "working a little bit": Please give specifics.
– agc
Jan 1 at 16:28
Few still getting printed this way: p5-Digest-HMAC-1.03_1 Perl5 interface to HMAC Message
– Nikolai Lvovich
Jan 2 at 12:16
Few still getting printed this way: p5-Digest-HMAC-1.03_1 Perl5 interface to HMAC Message
– Nikolai Lvovich
Jan 2 at 12:16
|
show 1 more 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%2f53976504%2fcryptic-sed-command-syntax-confusion%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
The above commands could have been written
sed 's/..*//;s/-[^-]*$//' file
which perhaps makes them clearer.– potong
Dec 30 '18 at 11:44