Parsing the output of date with sed
up vote
1
down vote
favorite
I am trying to replace the whitespaces in the output of date with '_' with no success.
$date
Fri Sep 14 14:10:04 EDT 2012
$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012
As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?
shell sed date
add a comment |
up vote
1
down vote
favorite
I am trying to replace the whitespaces in the output of date with '_' with no success.
$date
Fri Sep 14 14:10:04 EDT 2012
$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012
As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?
shell sed date
11
Most versions ofdatelet you specify the format directly instead so you DON'T have to massage it later with other tools.
– jw013
Sep 14 '12 at 18:14
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to replace the whitespaces in the output of date with '_' with no success.
$date
Fri Sep 14 14:10:04 EDT 2012
$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012
As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?
shell sed date
I am trying to replace the whitespaces in the output of date with '_' with no success.
$date
Fri Sep 14 14:10:04 EDT 2012
$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012
As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?
shell sed date
shell sed date
asked Sep 14 '12 at 18:12
Amelio Vazquez-Reina
12.2k52127228
12.2k52127228
11
Most versions ofdatelet you specify the format directly instead so you DON'T have to massage it later with other tools.
– jw013
Sep 14 '12 at 18:14
add a comment |
11
Most versions ofdatelet you specify the format directly instead so you DON'T have to massage it later with other tools.
– jw013
Sep 14 '12 at 18:14
11
11
Most versions of
date let you specify the format directly instead so you DON'T have to massage it later with other tools.– jw013
Sep 14 '12 at 18:14
Most versions of
date let you specify the format directly instead so you DON'T have to massage it later with other tools.– jw013
Sep 14 '12 at 18:14
add a comment |
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
Try:
$ date | tr ' ' '_'
or
$ date | sed 's/ /_/g'
Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.
1
In a case like this,tris faster than a regexp substitution.
– JRFerguson
Sep 14 '12 at 19:36
add a comment |
up vote
6
down vote
Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.
date +%Y-%m-%dT%T
which on some systems you may even write as
date +%FT%T
Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
Try:
$ date | tr ' ' '_'
or
$ date | sed 's/ /_/g'
Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.
1
In a case like this,tris faster than a regexp substitution.
– JRFerguson
Sep 14 '12 at 19:36
add a comment |
up vote
8
down vote
accepted
Try:
$ date | tr ' ' '_'
or
$ date | sed 's/ /_/g'
Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.
1
In a case like this,tris faster than a regexp substitution.
– JRFerguson
Sep 14 '12 at 19:36
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
Try:
$ date | tr ' ' '_'
or
$ date | sed 's/ /_/g'
Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.
Try:
$ date | tr ' ' '_'
or
$ date | sed 's/ /_/g'
Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.
edited Sep 14 '12 at 18:52
answered Sep 14 '12 at 18:14
tojrobinson
59637
59637
1
In a case like this,tris faster than a regexp substitution.
– JRFerguson
Sep 14 '12 at 19:36
add a comment |
1
In a case like this,tris faster than a regexp substitution.
– JRFerguson
Sep 14 '12 at 19:36
1
1
In a case like this,
tr is faster than a regexp substitution.– JRFerguson
Sep 14 '12 at 19:36
In a case like this,
tr is faster than a regexp substitution.– JRFerguson
Sep 14 '12 at 19:36
add a comment |
up vote
6
down vote
Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.
date +%Y-%m-%dT%T
which on some systems you may even write as
date +%FT%T
Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.
add a comment |
up vote
6
down vote
Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.
date +%Y-%m-%dT%T
which on some systems you may even write as
date +%FT%T
Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.
add a comment |
up vote
6
down vote
up vote
6
down vote
Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.
date +%Y-%m-%dT%T
which on some systems you may even write as
date +%FT%T
Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.
Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.
date +%Y-%m-%dT%T
which on some systems you may even write as
date +%FT%T
Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.
edited Nov 26 at 12:30
answered Sep 14 '12 at 20:17
Stéphane Chazelas
296k54559903
296k54559903
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f48209%2fparsing-the-output-of-date-with-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
11
Most versions of
datelet you specify the format directly instead so you DON'T have to massage it later with other tools.– jw013
Sep 14 '12 at 18:14