Select lines with two occurences of same pattern
I have several lines of output, some lines contain information on jobs waiting
example
I want to select lines that have more than one occurrence of the pattern
jobs waiting: 0
example:
abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah
would work if the command only returns line 3 and line 5
I would also like to be able to select lines that are opposite to this
Unfortunately I have no idea how to do this, with sed? awk? grep?
awk sed grep
New contributor
add a comment |
I have several lines of output, some lines contain information on jobs waiting
example
I want to select lines that have more than one occurrence of the pattern
jobs waiting: 0
example:
abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah
would work if the command only returns line 3 and line 5
I would also like to be able to select lines that are opposite to this
Unfortunately I have no idea how to do this, with sed? awk? grep?
awk sed grep
New contributor
add a comment |
I have several lines of output, some lines contain information on jobs waiting
example
I want to select lines that have more than one occurrence of the pattern
jobs waiting: 0
example:
abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah
would work if the command only returns line 3 and line 5
I would also like to be able to select lines that are opposite to this
Unfortunately I have no idea how to do this, with sed? awk? grep?
awk sed grep
New contributor
I have several lines of output, some lines contain information on jobs waiting
example
I want to select lines that have more than one occurrence of the pattern
jobs waiting: 0
example:
abcdef rglk,jobs waiting: 2,blah,blah,jobs waiting:0,jobs running: 1,blah,blah
lbf(kjn fk)kkj,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
gdjhgvdjh,jobs waiting: 0,blah,blah,jobs running: 1,blah,blah,jobs waiting: 0
g gg,jobs waiting: 2,blah,jobs waiting: 0,jobs running: 1,blah,blah
kjn dikfc,jobs waiting: 0,blah,jobs waiting: 0,jobs running: 1,bl ah,blah
d1d,jobs waiting: 2,blah,blah,jobs running: 1,blah,blah
kjfdk nrf(lkj rgf),jobs waiting: 2,blah,blah,jobs waiting: 0,bl ah,blah
would work if the command only returns line 3 and line 5
I would also like to be able to select lines that are opposite to this
Unfortunately I have no idea how to do this, with sed? awk? grep?
awk sed grep
awk sed grep
New contributor
New contributor
edited 5 mins ago
Sparhawk
9,94464296
9,94464296
New contributor
asked 17 mins ago
David JenkinsDavid Jenkins
61
61
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
KISS grep
approach:
grep 'jobs waiting: *0.*jobs waiting: *0' file
Invert by adding the -v
command line switch.
Alternate with sed
- attempt to replace the second instance, and print the line if it succeeds:
sed -n 's/jobs waiting: *0/&/2p' file
Inverse as
sed -n 's/jobs waiting: *0/&/2; t; p' file
(In both cases, *0
allows for zero or more space characters before the 0
, consistent with your example.)
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
});
}
});
David Jenkins 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%2f500974%2fselect-lines-with-two-occurences-of-same-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
KISS grep
approach:
grep 'jobs waiting: *0.*jobs waiting: *0' file
Invert by adding the -v
command line switch.
Alternate with sed
- attempt to replace the second instance, and print the line if it succeeds:
sed -n 's/jobs waiting: *0/&/2p' file
Inverse as
sed -n 's/jobs waiting: *0/&/2; t; p' file
(In both cases, *0
allows for zero or more space characters before the 0
, consistent with your example.)
add a comment |
KISS grep
approach:
grep 'jobs waiting: *0.*jobs waiting: *0' file
Invert by adding the -v
command line switch.
Alternate with sed
- attempt to replace the second instance, and print the line if it succeeds:
sed -n 's/jobs waiting: *0/&/2p' file
Inverse as
sed -n 's/jobs waiting: *0/&/2; t; p' file
(In both cases, *0
allows for zero or more space characters before the 0
, consistent with your example.)
add a comment |
KISS grep
approach:
grep 'jobs waiting: *0.*jobs waiting: *0' file
Invert by adding the -v
command line switch.
Alternate with sed
- attempt to replace the second instance, and print the line if it succeeds:
sed -n 's/jobs waiting: *0/&/2p' file
Inverse as
sed -n 's/jobs waiting: *0/&/2; t; p' file
(In both cases, *0
allows for zero or more space characters before the 0
, consistent with your example.)
KISS grep
approach:
grep 'jobs waiting: *0.*jobs waiting: *0' file
Invert by adding the -v
command line switch.
Alternate with sed
- attempt to replace the second instance, and print the line if it succeeds:
sed -n 's/jobs waiting: *0/&/2p' file
Inverse as
sed -n 's/jobs waiting: *0/&/2; t; p' file
(In both cases, *0
allows for zero or more space characters before the 0
, consistent with your example.)
answered 3 mins ago
steeldriversteeldriver
36.1k35286
36.1k35286
add a comment |
add a comment |
David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
David Jenkins is a new contributor. Be nice, and check out our Code of Conduct.
David Jenkins 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%2f500974%2fselect-lines-with-two-occurences-of-same-pattern%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