Get one thing from a line and concatenate it with one thing from another line
I have the following:
2018-11-10 23:57:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00
Average 9 6 0 84 16.00
2018-11-10 23:58:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00
Average 10 6 0 84 16.00
I need to get the time with the average value of %idle
:
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
text-processing awk sed perl
add a comment |
I have the following:
2018-11-10 23:57:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00
Average 9 6 0 84 16.00
2018-11-10 23:58:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00
Average 10 6 0 84 16.00
I need to get the time with the average value of %idle
:
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
text-processing awk sed perl
actually i am getting this file from another server and it is only one file
– ammar
Nov 11 '18 at 13:12
1
This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 '18 at 8:30
add a comment |
I have the following:
2018-11-10 23:57:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00
Average 9 6 0 84 16.00
2018-11-10 23:58:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00
Average 10 6 0 84 16.00
I need to get the time with the average value of %idle
:
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
text-processing awk sed perl
I have the following:
2018-11-10 23:57:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:57:21 %usr %sys %wio %idle physc
23:57:31 10 7 0 83 16.00
23:57:41 9 6 0 85 16.00
23:57:51 9 6 0 85 16.00
23:58:01 9 7 0 84 16.00
23:58:11 10 6 0 84 16.00
Average 9 6 0 84 16.00
2018-11-10 23:58:21 [COMMAND]: sar -u 10 5
AIX host 1 7 11/10/18
System configuration: lcpu=64 mode=Capped
23:58:21 %usr %sys %wio %idle physc
23:58:31 10 8 0 82 15.99
23:58:41 9 6 0 85 16.00
23:58:51 9 6 0 85 16.00
23:59:01 9 6 0 84 16.00
23:59:11 10 6 0 83 16.00
Average 10 6 0 84 16.00
I need to get the time with the average value of %idle
:
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
text-processing awk sed perl
text-processing awk sed perl
edited Nov 12 '18 at 9:07
Peter Mortensen
90158
90158
asked Nov 11 '18 at 13:02
ammarammar
357
357
actually i am getting this file from another server and it is only one file
– ammar
Nov 11 '18 at 13:12
1
This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 '18 at 8:30
add a comment |
actually i am getting this file from another server and it is only one file
– ammar
Nov 11 '18 at 13:12
1
This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 '18 at 8:30
actually i am getting this file from another server and it is only one file
– ammar
Nov 11 '18 at 13:12
actually i am getting this file from another server and it is only one file
– ammar
Nov 11 '18 at 13:12
1
1
This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 '18 at 8:30
This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 '18 at 8:30
add a comment |
3 Answers
3
active
oldest
votes
Going by your input file as-is, a simple awk
command as simple as below should suffice.
awk '/sar/ time=$1" "$2; next /Average/ print time"' file
add a comment |
A couple of choices:
awk
$ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84perl
$ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
add a comment |
This Perl one-liner should do the trick:
perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'
(I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name
at the beginning or the end, or a pipe will do. The workings are these:
- Go to a line with date and time at the beginning and memorise it.
- Go to a line with
Average
at beginning and once there, print the memorised date and time plus the|
sign and the average taken from the fifth column assuming whitespace column separation. - Point 1. repeats.
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%2f481082%2fget-one-thing-from-a-line-and-concatenate-it-with-one-thing-from-another-line%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
Going by your input file as-is, a simple awk
command as simple as below should suffice.
awk '/sar/ time=$1" "$2; next /Average/ print time"' file
add a comment |
Going by your input file as-is, a simple awk
command as simple as below should suffice.
awk '/sar/ time=$1" "$2; next /Average/ print time"' file
add a comment |
Going by your input file as-is, a simple awk
command as simple as below should suffice.
awk '/sar/ time=$1" "$2; next /Average/ print time"' file
Going by your input file as-is, a simple awk
command as simple as below should suffice.
awk '/sar/ time=$1" "$2; next /Average/ print time"' file
answered Nov 11 '18 at 13:16
InianInian
4,110824
4,110824
add a comment |
add a comment |
A couple of choices:
awk
$ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84perl
$ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
add a comment |
A couple of choices:
awk
$ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84perl
$ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
add a comment |
A couple of choices:
awk
$ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84perl
$ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
A couple of choices:
awk
$ awk '/COMMAND/printf "%s %s", $1,$2/Average/"$5' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84perl
$ perl -ale '$k="$F[0] $F[1]" if /COMMAND/; print "$k|$F[4]" if /Average/' file
2018-11-10 23:57:21|84
2018-11-10 23:58:21|84
edited Nov 11 '18 at 16:19
answered Nov 11 '18 at 14:48
terdon♦terdon
130k32255433
130k32255433
add a comment |
add a comment |
This Perl one-liner should do the trick:
perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'
(I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name
at the beginning or the end, or a pipe will do. The workings are these:
- Go to a line with date and time at the beginning and memorise it.
- Go to a line with
Average
at beginning and once there, print the memorised date and time plus the|
sign and the average taken from the fifth column assuming whitespace column separation. - Point 1. repeats.
add a comment |
This Perl one-liner should do the trick:
perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'
(I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name
at the beginning or the end, or a pipe will do. The workings are these:
- Go to a line with date and time at the beginning and memorise it.
- Go to a line with
Average
at beginning and once there, print the memorised date and time plus the|
sign and the average taken from the fifth column assuming whitespace column separation. - Point 1. repeats.
add a comment |
This Perl one-liner should do the trick:
perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'
(I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name
at the beginning or the end, or a pipe will do. The workings are these:
- Go to a line with date and time at the beginning and memorise it.
- Go to a line with
Average
at beginning and once there, print the memorised date and time plus the|
sign and the average taken from the fifth column assuming whitespace column separation. - Point 1. repeats.
This Perl one-liner should do the trick:
perl -lne'$d=$1 if /^(d4-d2-d2s+d2:d2:d2)/;
if(/^Averages+S+s+S+s+S+s+(S+)/) $1";'
(I made it two lines, so it's more readable.) You need to feed it with input though. The file name at the end, or <file_name
at the beginning or the end, or a pipe will do. The workings are these:
- Go to a line with date and time at the beginning and memorise it.
- Go to a line with
Average
at beginning and once there, print the memorised date and time plus the|
sign and the average taken from the fifth column assuming whitespace column separation. - Point 1. repeats.
edited Nov 11 '18 at 13:23
answered Nov 11 '18 at 13:15
TomaszTomasz
9,53652965
9,53652965
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.
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%2f481082%2fget-one-thing-from-a-line-and-concatenate-it-with-one-thing-from-another-line%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
actually i am getting this file from another server and it is only one file
– ammar
Nov 11 '18 at 13:12
1
This is using this site as a script-writing service.
– Peter Mortensen
Nov 12 '18 at 8:30