How to keep colored output using sed
I'm using the sed
command and I want to keep colored output from the previous command. The output of ls
is colored, but the output of sed is not. I'm using OSX.
ls -la | sed -En '/Desktop/q;p'
sed osx pipe ls colors
New contributor
add a comment |
I'm using the sed
command and I want to keep colored output from the previous command. The output of ls
is colored, but the output of sed is not. I'm using OSX.
ls -la | sed -En '/Desktop/q;p'
sed osx pipe ls colors
New contributor
add a comment |
I'm using the sed
command and I want to keep colored output from the previous command. The output of ls
is colored, but the output of sed is not. I'm using OSX.
ls -la | sed -En '/Desktop/q;p'
sed osx pipe ls colors
New contributor
I'm using the sed
command and I want to keep colored output from the previous command. The output of ls
is colored, but the output of sed is not. I'm using OSX.
ls -la | sed -En '/Desktop/q;p'
sed osx pipe ls colors
sed osx pipe ls colors
New contributor
New contributor
edited yesterday
Jeff Schaller
40.6k1056129
40.6k1056129
New contributor
asked yesterday
JohniakJohniak
1411
1411
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
On macOS, the ls
is not GNU ls
and does not accept the --color=always
option that Linux users might expect for this functionality.
In the macOS version of ls
, the colors are controlled by two variables: $CLICOLOR
and $CLICOLOR_FORCE
. If the former is defined, the terminal specified by $TERM
supports color, and the output is to a terminal, then this output will be colored, much like GNU's --color=auto
option. If the latter variable is defined as well, the final condition is dropped, behaving like GNU's --color=always
.
So to have color passed through to sed
, you would need something like the following:
CLICOLOR_FORCE=1 ls -la | sed -En '/Desktop/q;p'
macOS'sls
does have the-G
option to enable color as well, but I'm not sure how it behaves in a pipeline.
– 8bittree
yesterday
2
@8bittree Passing-G
is equivalent to defining$CLICOLOR
(and therefore not sufficient)
– Fox
yesterday
add a comment |
I'll blindly guess that your distribution, like many, has an alias that maps ls
to ls --color=auto
. ls -la --color=always | sed …
should work.
(The auto
setting makes, broadly speaking, ls
check if its output goes to the display or not, and only color for the display. The reason for this is that color is realized by escape sequences, i.e. invisible command characters, so a program might confuse them for part of the filename, things like that.)
3
The blind guess is wrong. OSX is not a Linux distribution and does not use a GNU userland
– Fox
yesterday
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Johniak 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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497413%2fhow-to-keep-colored-output-using-sed%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
On macOS, the ls
is not GNU ls
and does not accept the --color=always
option that Linux users might expect for this functionality.
In the macOS version of ls
, the colors are controlled by two variables: $CLICOLOR
and $CLICOLOR_FORCE
. If the former is defined, the terminal specified by $TERM
supports color, and the output is to a terminal, then this output will be colored, much like GNU's --color=auto
option. If the latter variable is defined as well, the final condition is dropped, behaving like GNU's --color=always
.
So to have color passed through to sed
, you would need something like the following:
CLICOLOR_FORCE=1 ls -la | sed -En '/Desktop/q;p'
macOS'sls
does have the-G
option to enable color as well, but I'm not sure how it behaves in a pipeline.
– 8bittree
yesterday
2
@8bittree Passing-G
is equivalent to defining$CLICOLOR
(and therefore not sufficient)
– Fox
yesterday
add a comment |
On macOS, the ls
is not GNU ls
and does not accept the --color=always
option that Linux users might expect for this functionality.
In the macOS version of ls
, the colors are controlled by two variables: $CLICOLOR
and $CLICOLOR_FORCE
. If the former is defined, the terminal specified by $TERM
supports color, and the output is to a terminal, then this output will be colored, much like GNU's --color=auto
option. If the latter variable is defined as well, the final condition is dropped, behaving like GNU's --color=always
.
So to have color passed through to sed
, you would need something like the following:
CLICOLOR_FORCE=1 ls -la | sed -En '/Desktop/q;p'
macOS'sls
does have the-G
option to enable color as well, but I'm not sure how it behaves in a pipeline.
– 8bittree
yesterday
2
@8bittree Passing-G
is equivalent to defining$CLICOLOR
(and therefore not sufficient)
– Fox
yesterday
add a comment |
On macOS, the ls
is not GNU ls
and does not accept the --color=always
option that Linux users might expect for this functionality.
In the macOS version of ls
, the colors are controlled by two variables: $CLICOLOR
and $CLICOLOR_FORCE
. If the former is defined, the terminal specified by $TERM
supports color, and the output is to a terminal, then this output will be colored, much like GNU's --color=auto
option. If the latter variable is defined as well, the final condition is dropped, behaving like GNU's --color=always
.
So to have color passed through to sed
, you would need something like the following:
CLICOLOR_FORCE=1 ls -la | sed -En '/Desktop/q;p'
On macOS, the ls
is not GNU ls
and does not accept the --color=always
option that Linux users might expect for this functionality.
In the macOS version of ls
, the colors are controlled by two variables: $CLICOLOR
and $CLICOLOR_FORCE
. If the former is defined, the terminal specified by $TERM
supports color, and the output is to a terminal, then this output will be colored, much like GNU's --color=auto
option. If the latter variable is defined as well, the final condition is dropped, behaving like GNU's --color=always
.
So to have color passed through to sed
, you would need something like the following:
CLICOLOR_FORCE=1 ls -la | sed -En '/Desktop/q;p'
edited yesterday
terdon♦
130k32254432
130k32254432
answered yesterday
FoxFox
5,44911233
5,44911233
macOS'sls
does have the-G
option to enable color as well, but I'm not sure how it behaves in a pipeline.
– 8bittree
yesterday
2
@8bittree Passing-G
is equivalent to defining$CLICOLOR
(and therefore not sufficient)
– Fox
yesterday
add a comment |
macOS'sls
does have the-G
option to enable color as well, but I'm not sure how it behaves in a pipeline.
– 8bittree
yesterday
2
@8bittree Passing-G
is equivalent to defining$CLICOLOR
(and therefore not sufficient)
– Fox
yesterday
macOS's
ls
does have the -G
option to enable color as well, but I'm not sure how it behaves in a pipeline.– 8bittree
yesterday
macOS's
ls
does have the -G
option to enable color as well, but I'm not sure how it behaves in a pipeline.– 8bittree
yesterday
2
2
@8bittree Passing
-G
is equivalent to defining $CLICOLOR
(and therefore not sufficient)– Fox
yesterday
@8bittree Passing
-G
is equivalent to defining $CLICOLOR
(and therefore not sufficient)– Fox
yesterday
add a comment |
I'll blindly guess that your distribution, like many, has an alias that maps ls
to ls --color=auto
. ls -la --color=always | sed …
should work.
(The auto
setting makes, broadly speaking, ls
check if its output goes to the display or not, and only color for the display. The reason for this is that color is realized by escape sequences, i.e. invisible command characters, so a program might confuse them for part of the filename, things like that.)
3
The blind guess is wrong. OSX is not a Linux distribution and does not use a GNU userland
– Fox
yesterday
add a comment |
I'll blindly guess that your distribution, like many, has an alias that maps ls
to ls --color=auto
. ls -la --color=always | sed …
should work.
(The auto
setting makes, broadly speaking, ls
check if its output goes to the display or not, and only color for the display. The reason for this is that color is realized by escape sequences, i.e. invisible command characters, so a program might confuse them for part of the filename, things like that.)
3
The blind guess is wrong. OSX is not a Linux distribution and does not use a GNU userland
– Fox
yesterday
add a comment |
I'll blindly guess that your distribution, like many, has an alias that maps ls
to ls --color=auto
. ls -la --color=always | sed …
should work.
(The auto
setting makes, broadly speaking, ls
check if its output goes to the display or not, and only color for the display. The reason for this is that color is realized by escape sequences, i.e. invisible command characters, so a program might confuse them for part of the filename, things like that.)
I'll blindly guess that your distribution, like many, has an alias that maps ls
to ls --color=auto
. ls -la --color=always | sed …
should work.
(The auto
setting makes, broadly speaking, ls
check if its output goes to the display or not, and only color for the display. The reason for this is that color is realized by escape sequences, i.e. invisible command characters, so a program might confuse them for part of the filename, things like that.)
answered yesterday
Ulrich SchwarzUlrich Schwarz
9,74312946
9,74312946
3
The blind guess is wrong. OSX is not a Linux distribution and does not use a GNU userland
– Fox
yesterday
add a comment |
3
The blind guess is wrong. OSX is not a Linux distribution and does not use a GNU userland
– Fox
yesterday
3
3
The blind guess is wrong. OSX is not a Linux distribution and does not use a GNU userland
– Fox
yesterday
The blind guess is wrong. OSX is not a Linux distribution and does not use a GNU userland
– Fox
yesterday
add a comment |
Johniak is a new contributor. Be nice, and check out our Code of Conduct.
Johniak is a new contributor. Be nice, and check out our Code of Conduct.
Johniak is a new contributor. Be nice, and check out our Code of Conduct.
Johniak is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f497413%2fhow-to-keep-colored-output-using-sed%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