Only print output after finding pattern
There's a script (let's call it echoer
) that prints to screen a bunch of information. I'd like to be able to only see lines after a pattern is found.
I imagine the usage of a solution to look something like
echoer | solution_command <pattern>
Ideally pattern
would be a regular expression, but hard value strings would be enough for me.
echo printf
New contributor
add a comment |
There's a script (let's call it echoer
) that prints to screen a bunch of information. I'd like to be able to only see lines after a pattern is found.
I imagine the usage of a solution to look something like
echoer | solution_command <pattern>
Ideally pattern
would be a regular expression, but hard value strings would be enough for me.
echo printf
New contributor
So pattern can be multiple strings?
– Inian
1 hour ago
A glob? Do you mean a regular expression? Globs only make sense for file name expansions.
– terdon♦
55 mins ago
@Inian I'm not sure I understand the question. Do you mean to ask if I want this to work with disjunctions?
– user23146
39 mins ago
add a comment |
There's a script (let's call it echoer
) that prints to screen a bunch of information. I'd like to be able to only see lines after a pattern is found.
I imagine the usage of a solution to look something like
echoer | solution_command <pattern>
Ideally pattern
would be a regular expression, but hard value strings would be enough for me.
echo printf
New contributor
There's a script (let's call it echoer
) that prints to screen a bunch of information. I'd like to be able to only see lines after a pattern is found.
I imagine the usage of a solution to look something like
echoer | solution_command <pattern>
Ideally pattern
would be a regular expression, but hard value strings would be enough for me.
echo printf
echo printf
New contributor
New contributor
edited 40 mins ago
user23146
New contributor
asked 1 hour ago
user23146user23146
83
83
New contributor
New contributor
So pattern can be multiple strings?
– Inian
1 hour ago
A glob? Do you mean a regular expression? Globs only make sense for file name expansions.
– terdon♦
55 mins ago
@Inian I'm not sure I understand the question. Do you mean to ask if I want this to work with disjunctions?
– user23146
39 mins ago
add a comment |
So pattern can be multiple strings?
– Inian
1 hour ago
A glob? Do you mean a regular expression? Globs only make sense for file name expansions.
– terdon♦
55 mins ago
@Inian I'm not sure I understand the question. Do you mean to ask if I want this to work with disjunctions?
– user23146
39 mins ago
So pattern can be multiple strings?
– Inian
1 hour ago
So pattern can be multiple strings?
– Inian
1 hour ago
A glob? Do you mean a regular expression? Globs only make sense for file name expansions.
– terdon♦
55 mins ago
A glob? Do you mean a regular expression? Globs only make sense for file name expansions.
– terdon♦
55 mins ago
@Inian I'm not sure I understand the question. Do you mean to ask if I want this to work with disjunctions?
– user23146
39 mins ago
@Inian I'm not sure I understand the question. Do you mean to ask if I want this to work with disjunctions?
– user23146
39 mins ago
add a comment |
1 Answer
1
active
oldest
votes
AWK can do this with pattern ranges, which allows the use of any regular expression:
echoer | awk '/pattern/,0'
will print echoer
’s output starting with the first line matching pattern
.
AWK is pattern-based, and is typically used with a “if this pattern matches, do this” type of approach. “This pattern” can be a range of patterns, defined as “when this pattern matches, start going this, until this other pattern matches”; this is specified by writing two patterns separated by a comma, as above. Patterns can be text matches, as in /pattern/
, where the current line is checked against the pattern, interpreted as a regular expression; they can also be general expressions, evaluated for every line, and considered to match if their result is non-zero or non-empty.
In AWK, the default action is to print the current line.
Putting all this together, awk '/pattern/,0'
looks for lines matching pattern
, and once it finds one, applies the default action to all lines until the 0
condition matches (is non-zero). awk '/pattern/,""'
would work too.
The Gawk manual goes into much more detail.
Thanks. This works. Any chance you can breakdown the syntax for those who don't know AWK? I know AWK operates line by line and it is triggered by matching the input pattern, but that's about it.
– user23146
36 mins ago
1
I was not aware of the range semantics with zero as the end of the range. Thanks!
– Kusalananda♦
36 mins ago
@user23146 I’ve tried to explain a bit better.
– Stephen Kitt
15 mins ago
@StephenKitt This is great! What if I wanted to print until it matched0
? Would it be/pattern/,/0/
? What would the answer look like explicitly writing out the default action?
– user23146
10 mins ago
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
});
}
});
user23146 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%2f509173%2fonly-print-output-after-finding-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
AWK can do this with pattern ranges, which allows the use of any regular expression:
echoer | awk '/pattern/,0'
will print echoer
’s output starting with the first line matching pattern
.
AWK is pattern-based, and is typically used with a “if this pattern matches, do this” type of approach. “This pattern” can be a range of patterns, defined as “when this pattern matches, start going this, until this other pattern matches”; this is specified by writing two patterns separated by a comma, as above. Patterns can be text matches, as in /pattern/
, where the current line is checked against the pattern, interpreted as a regular expression; they can also be general expressions, evaluated for every line, and considered to match if their result is non-zero or non-empty.
In AWK, the default action is to print the current line.
Putting all this together, awk '/pattern/,0'
looks for lines matching pattern
, and once it finds one, applies the default action to all lines until the 0
condition matches (is non-zero). awk '/pattern/,""'
would work too.
The Gawk manual goes into much more detail.
Thanks. This works. Any chance you can breakdown the syntax for those who don't know AWK? I know AWK operates line by line and it is triggered by matching the input pattern, but that's about it.
– user23146
36 mins ago
1
I was not aware of the range semantics with zero as the end of the range. Thanks!
– Kusalananda♦
36 mins ago
@user23146 I’ve tried to explain a bit better.
– Stephen Kitt
15 mins ago
@StephenKitt This is great! What if I wanted to print until it matched0
? Would it be/pattern/,/0/
? What would the answer look like explicitly writing out the default action?
– user23146
10 mins ago
add a comment |
AWK can do this with pattern ranges, which allows the use of any regular expression:
echoer | awk '/pattern/,0'
will print echoer
’s output starting with the first line matching pattern
.
AWK is pattern-based, and is typically used with a “if this pattern matches, do this” type of approach. “This pattern” can be a range of patterns, defined as “when this pattern matches, start going this, until this other pattern matches”; this is specified by writing two patterns separated by a comma, as above. Patterns can be text matches, as in /pattern/
, where the current line is checked against the pattern, interpreted as a regular expression; they can also be general expressions, evaluated for every line, and considered to match if their result is non-zero or non-empty.
In AWK, the default action is to print the current line.
Putting all this together, awk '/pattern/,0'
looks for lines matching pattern
, and once it finds one, applies the default action to all lines until the 0
condition matches (is non-zero). awk '/pattern/,""'
would work too.
The Gawk manual goes into much more detail.
Thanks. This works. Any chance you can breakdown the syntax for those who don't know AWK? I know AWK operates line by line and it is triggered by matching the input pattern, but that's about it.
– user23146
36 mins ago
1
I was not aware of the range semantics with zero as the end of the range. Thanks!
– Kusalananda♦
36 mins ago
@user23146 I’ve tried to explain a bit better.
– Stephen Kitt
15 mins ago
@StephenKitt This is great! What if I wanted to print until it matched0
? Would it be/pattern/,/0/
? What would the answer look like explicitly writing out the default action?
– user23146
10 mins ago
add a comment |
AWK can do this with pattern ranges, which allows the use of any regular expression:
echoer | awk '/pattern/,0'
will print echoer
’s output starting with the first line matching pattern
.
AWK is pattern-based, and is typically used with a “if this pattern matches, do this” type of approach. “This pattern” can be a range of patterns, defined as “when this pattern matches, start going this, until this other pattern matches”; this is specified by writing two patterns separated by a comma, as above. Patterns can be text matches, as in /pattern/
, where the current line is checked against the pattern, interpreted as a regular expression; they can also be general expressions, evaluated for every line, and considered to match if their result is non-zero or non-empty.
In AWK, the default action is to print the current line.
Putting all this together, awk '/pattern/,0'
looks for lines matching pattern
, and once it finds one, applies the default action to all lines until the 0
condition matches (is non-zero). awk '/pattern/,""'
would work too.
The Gawk manual goes into much more detail.
AWK can do this with pattern ranges, which allows the use of any regular expression:
echoer | awk '/pattern/,0'
will print echoer
’s output starting with the first line matching pattern
.
AWK is pattern-based, and is typically used with a “if this pattern matches, do this” type of approach. “This pattern” can be a range of patterns, defined as “when this pattern matches, start going this, until this other pattern matches”; this is specified by writing two patterns separated by a comma, as above. Patterns can be text matches, as in /pattern/
, where the current line is checked against the pattern, interpreted as a regular expression; they can also be general expressions, evaluated for every line, and considered to match if their result is non-zero or non-empty.
In AWK, the default action is to print the current line.
Putting all this together, awk '/pattern/,0'
looks for lines matching pattern
, and once it finds one, applies the default action to all lines until the 0
condition matches (is non-zero). awk '/pattern/,""'
would work too.
The Gawk manual goes into much more detail.
edited 15 mins ago
answered 56 mins ago
Stephen KittStephen Kitt
178k24405482
178k24405482
Thanks. This works. Any chance you can breakdown the syntax for those who don't know AWK? I know AWK operates line by line and it is triggered by matching the input pattern, but that's about it.
– user23146
36 mins ago
1
I was not aware of the range semantics with zero as the end of the range. Thanks!
– Kusalananda♦
36 mins ago
@user23146 I’ve tried to explain a bit better.
– Stephen Kitt
15 mins ago
@StephenKitt This is great! What if I wanted to print until it matched0
? Would it be/pattern/,/0/
? What would the answer look like explicitly writing out the default action?
– user23146
10 mins ago
add a comment |
Thanks. This works. Any chance you can breakdown the syntax for those who don't know AWK? I know AWK operates line by line and it is triggered by matching the input pattern, but that's about it.
– user23146
36 mins ago
1
I was not aware of the range semantics with zero as the end of the range. Thanks!
– Kusalananda♦
36 mins ago
@user23146 I’ve tried to explain a bit better.
– Stephen Kitt
15 mins ago
@StephenKitt This is great! What if I wanted to print until it matched0
? Would it be/pattern/,/0/
? What would the answer look like explicitly writing out the default action?
– user23146
10 mins ago
Thanks. This works. Any chance you can breakdown the syntax for those who don't know AWK? I know AWK operates line by line and it is triggered by matching the input pattern, but that's about it.
– user23146
36 mins ago
Thanks. This works. Any chance you can breakdown the syntax for those who don't know AWK? I know AWK operates line by line and it is triggered by matching the input pattern, but that's about it.
– user23146
36 mins ago
1
1
I was not aware of the range semantics with zero as the end of the range. Thanks!
– Kusalananda♦
36 mins ago
I was not aware of the range semantics with zero as the end of the range. Thanks!
– Kusalananda♦
36 mins ago
@user23146 I’ve tried to explain a bit better.
– Stephen Kitt
15 mins ago
@user23146 I’ve tried to explain a bit better.
– Stephen Kitt
15 mins ago
@StephenKitt This is great! What if I wanted to print until it matched
0
? Would it be /pattern/,/0/
? What would the answer look like explicitly writing out the default action?– user23146
10 mins ago
@StephenKitt This is great! What if I wanted to print until it matched
0
? Would it be /pattern/,/0/
? What would the answer look like explicitly writing out the default action?– user23146
10 mins ago
add a comment |
user23146 is a new contributor. Be nice, and check out our Code of Conduct.
user23146 is a new contributor. Be nice, and check out our Code of Conduct.
user23146 is a new contributor. Be nice, and check out our Code of Conduct.
user23146 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%2f509173%2fonly-print-output-after-finding-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
So pattern can be multiple strings?
– Inian
1 hour ago
A glob? Do you mean a regular expression? Globs only make sense for file name expansions.
– terdon♦
55 mins ago
@Inian I'm not sure I understand the question. Do you mean to ask if I want this to work with disjunctions?
– user23146
39 mins ago