Getting current time as output from “date” in awk script
Clash Royale CLAN TAG#URR8PPP
In an awk
, script I am using a command
system(date)
to print the current date in a file, but after this command is executed, the next line is also added implicitly. Is there any way in awk
to print the current date without the new line being added so that whatever I print next comes in the same line?
awk date string
add a comment |
In an awk
, script I am using a command
system(date)
to print the current date in a file, but after this command is executed, the next line is also added implicitly. Is there any way in awk
to print the current date without the new line being added so that whatever I print next comes in the same line?
awk date string
add a comment |
In an awk
, script I am using a command
system(date)
to print the current date in a file, but after this command is executed, the next line is also added implicitly. Is there any way in awk
to print the current date without the new line being added so that whatever I print next comes in the same line?
awk date string
In an awk
, script I am using a command
system(date)
to print the current date in a file, but after this command is executed, the next line is also added implicitly. Is there any way in awk
to print the current date without the new line being added so that whatever I print next comes in the same line?
awk date string
awk date string
edited Jan 28 at 10:28
Kusalananda
130k17246406
130k17246406
asked May 6 '11 at 18:36
nishannishan
7117
7117
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The date
command adds a newline at the end of its output. You can read the output into awk and make it print without appending a newline.
"date" | getline date; printf "%s", date
Unless your script is long-running, you could alternatively obtain the date when you start your script.
awk -v date="$(date)" '
… printf "%s", date …
'
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:-
– nishan
May 6 '11 at 19:36
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date)
– nishan
May 6 '11 at 19:44
1
@nishan: The pipe executes its left-hand side as a command. So you're executing the result ofsystem(…)
as a shell command. The right syntax is in my answer.
– Gilles
May 6 '11 at 20:00
add a comment |
The GNU awk
and @ThomasDickey's mawk
implementations of awk
has some extensions relating to time and date functionality. With these, you could use
strftime("%+", systime())
to get a string representation in the same format as the date
utility would produce.
Or, for a slightly different format, but more portable (works on non-BSD systems),
strftime("%c", systime())
Testing:
$ date
Mon Jan 28 11:22:22 CET 2019
$ gawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:27 CET 2019
$ mawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:33 CET 2019
If the %+
format is not supported on your system (it's not in glibc2
), then %c
would probably generate a close approximation to this. The exact format will depend on the implementation of strftime()
in the C library, and on the current datetime-related locale settings. In the C
/POSIX
locale, you would get the same output as above, but with no time-zone abbreviation.
BSD awk
implementation may not have these functions by default.
See also:
- The manuals for
awk
andstrftime
(for time/date-stamp formatting) on your system.
That would be @ThomasDickey'smawk
(since 20121129). Note that the one that comes with Debian and derivatives doesn't have those extensions.%+
support is also probably system dependant.
– Stéphane Chazelas
Jan 28 at 11:29
@StéphaneChazelas Some of that has now been addressed. Thanks.
– Kusalananda
Jan 28 at 12:12
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
);
);
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%2f12765%2fgetting-current-time-as-output-from-date-in-awk-script%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
The date
command adds a newline at the end of its output. You can read the output into awk and make it print without appending a newline.
"date" | getline date; printf "%s", date
Unless your script is long-running, you could alternatively obtain the date when you start your script.
awk -v date="$(date)" '
… printf "%s", date …
'
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:-
– nishan
May 6 '11 at 19:36
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date)
– nishan
May 6 '11 at 19:44
1
@nishan: The pipe executes its left-hand side as a command. So you're executing the result ofsystem(…)
as a shell command. The right syntax is in my answer.
– Gilles
May 6 '11 at 20:00
add a comment |
The date
command adds a newline at the end of its output. You can read the output into awk and make it print without appending a newline.
"date" | getline date; printf "%s", date
Unless your script is long-running, you could alternatively obtain the date when you start your script.
awk -v date="$(date)" '
… printf "%s", date …
'
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:-
– nishan
May 6 '11 at 19:36
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date)
– nishan
May 6 '11 at 19:44
1
@nishan: The pipe executes its left-hand side as a command. So you're executing the result ofsystem(…)
as a shell command. The right syntax is in my answer.
– Gilles
May 6 '11 at 20:00
add a comment |
The date
command adds a newline at the end of its output. You can read the output into awk and make it print without appending a newline.
"date" | getline date; printf "%s", date
Unless your script is long-running, you could alternatively obtain the date when you start your script.
awk -v date="$(date)" '
… printf "%s", date …
'
The date
command adds a newline at the end of its output. You can read the output into awk and make it print without appending a newline.
"date" | getline date; printf "%s", date
Unless your script is long-running, you could alternatively obtain the date when you start your script.
awk -v date="$(date)" '
… printf "%s", date …
'
answered May 6 '11 at 18:59
GillesGilles
537k12810871604
537k12810871604
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:-
– nishan
May 6 '11 at 19:36
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date)
– nishan
May 6 '11 at 19:44
1
@nishan: The pipe executes its left-hand side as a command. So you're executing the result ofsystem(…)
as a shell command. The right syntax is in my answer.
– Gilles
May 6 '11 at 20:00
add a comment |
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:-
– nishan
May 6 '11 at 19:36
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date)
– nishan
May 6 '11 at 19:44
1
@nishan: The pipe executes its left-hand side as a command. So you're executing the result ofsystem(…)
as a shell command. The right syntax is in my answer.
– Gilles
May 6 '11 at 20:00
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:-
– nishan
May 6 '11 at 19:36
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:-
– nishan
May 6 '11 at 19:36
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date)
– nishan
May 6 '11 at 19:44
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date)
– nishan
May 6 '11 at 19:44
1
1
@nishan: The pipe executes its left-hand side as a command. So you're executing the result of
system(…)
as a shell command. The right syntax is in my answer.– Gilles
May 6 '11 at 20:00
@nishan: The pipe executes its left-hand side as a command. So you're executing the result of
system(…)
as a shell command. The right syntax is in my answer.– Gilles
May 6 '11 at 20:00
add a comment |
The GNU awk
and @ThomasDickey's mawk
implementations of awk
has some extensions relating to time and date functionality. With these, you could use
strftime("%+", systime())
to get a string representation in the same format as the date
utility would produce.
Or, for a slightly different format, but more portable (works on non-BSD systems),
strftime("%c", systime())
Testing:
$ date
Mon Jan 28 11:22:22 CET 2019
$ gawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:27 CET 2019
$ mawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:33 CET 2019
If the %+
format is not supported on your system (it's not in glibc2
), then %c
would probably generate a close approximation to this. The exact format will depend on the implementation of strftime()
in the C library, and on the current datetime-related locale settings. In the C
/POSIX
locale, you would get the same output as above, but with no time-zone abbreviation.
BSD awk
implementation may not have these functions by default.
See also:
- The manuals for
awk
andstrftime
(for time/date-stamp formatting) on your system.
That would be @ThomasDickey'smawk
(since 20121129). Note that the one that comes with Debian and derivatives doesn't have those extensions.%+
support is also probably system dependant.
– Stéphane Chazelas
Jan 28 at 11:29
@StéphaneChazelas Some of that has now been addressed. Thanks.
– Kusalananda
Jan 28 at 12:12
add a comment |
The GNU awk
and @ThomasDickey's mawk
implementations of awk
has some extensions relating to time and date functionality. With these, you could use
strftime("%+", systime())
to get a string representation in the same format as the date
utility would produce.
Or, for a slightly different format, but more portable (works on non-BSD systems),
strftime("%c", systime())
Testing:
$ date
Mon Jan 28 11:22:22 CET 2019
$ gawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:27 CET 2019
$ mawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:33 CET 2019
If the %+
format is not supported on your system (it's not in glibc2
), then %c
would probably generate a close approximation to this. The exact format will depend on the implementation of strftime()
in the C library, and on the current datetime-related locale settings. In the C
/POSIX
locale, you would get the same output as above, but with no time-zone abbreviation.
BSD awk
implementation may not have these functions by default.
See also:
- The manuals for
awk
andstrftime
(for time/date-stamp formatting) on your system.
That would be @ThomasDickey'smawk
(since 20121129). Note that the one that comes with Debian and derivatives doesn't have those extensions.%+
support is also probably system dependant.
– Stéphane Chazelas
Jan 28 at 11:29
@StéphaneChazelas Some of that has now been addressed. Thanks.
– Kusalananda
Jan 28 at 12:12
add a comment |
The GNU awk
and @ThomasDickey's mawk
implementations of awk
has some extensions relating to time and date functionality. With these, you could use
strftime("%+", systime())
to get a string representation in the same format as the date
utility would produce.
Or, for a slightly different format, but more portable (works on non-BSD systems),
strftime("%c", systime())
Testing:
$ date
Mon Jan 28 11:22:22 CET 2019
$ gawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:27 CET 2019
$ mawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:33 CET 2019
If the %+
format is not supported on your system (it's not in glibc2
), then %c
would probably generate a close approximation to this. The exact format will depend on the implementation of strftime()
in the C library, and on the current datetime-related locale settings. In the C
/POSIX
locale, you would get the same output as above, but with no time-zone abbreviation.
BSD awk
implementation may not have these functions by default.
See also:
- The manuals for
awk
andstrftime
(for time/date-stamp formatting) on your system.
The GNU awk
and @ThomasDickey's mawk
implementations of awk
has some extensions relating to time and date functionality. With these, you could use
strftime("%+", systime())
to get a string representation in the same format as the date
utility would produce.
Or, for a slightly different format, but more portable (works on non-BSD systems),
strftime("%c", systime())
Testing:
$ date
Mon Jan 28 11:22:22 CET 2019
$ gawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:27 CET 2019
$ mawk 'BEGIN print strftime("%+", systime()) '
Mon Jan 28 11:22:33 CET 2019
If the %+
format is not supported on your system (it's not in glibc2
), then %c
would probably generate a close approximation to this. The exact format will depend on the implementation of strftime()
in the C library, and on the current datetime-related locale settings. In the C
/POSIX
locale, you would get the same output as above, but with no time-zone abbreviation.
BSD awk
implementation may not have these functions by default.
See also:
- The manuals for
awk
andstrftime
(for time/date-stamp formatting) on your system.
edited Jan 28 at 12:58
answered Jan 28 at 10:23
KusalanandaKusalananda
130k17246406
130k17246406
That would be @ThomasDickey'smawk
(since 20121129). Note that the one that comes with Debian and derivatives doesn't have those extensions.%+
support is also probably system dependant.
– Stéphane Chazelas
Jan 28 at 11:29
@StéphaneChazelas Some of that has now been addressed. Thanks.
– Kusalananda
Jan 28 at 12:12
add a comment |
That would be @ThomasDickey'smawk
(since 20121129). Note that the one that comes with Debian and derivatives doesn't have those extensions.%+
support is also probably system dependant.
– Stéphane Chazelas
Jan 28 at 11:29
@StéphaneChazelas Some of that has now been addressed. Thanks.
– Kusalananda
Jan 28 at 12:12
That would be @ThomasDickey's
mawk
(since 20121129). Note that the one that comes with Debian and derivatives doesn't have those extensions. %+
support is also probably system dependant.– Stéphane Chazelas
Jan 28 at 11:29
That would be @ThomasDickey's
mawk
(since 20121129). Note that the one that comes with Debian and derivatives doesn't have those extensions. %+
support is also probably system dependant.– Stéphane Chazelas
Jan 28 at 11:29
@StéphaneChazelas Some of that has now been addressed. Thanks.
– Kusalananda
Jan 28 at 12:12
@StéphaneChazelas Some of that has now been addressed. Thanks.
– Kusalananda
Jan 28 at 12:12
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.
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%2f12765%2fgetting-current-time-as-output-from-date-in-awk-script%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