How to get the n-th line after a grepped one?SSH client and Command Prompt replacements Windows look-and-feelCreate one file per line via shellosx bash grep - finding search terms in a large file with one single linegrep: output specific line after matchKeep grep output on one line?How to pass parameter in linux command at the end of the lineHow can I enrich my bash one-liner to get a default valuebash: how to print multiple arrays valuesUse grep for a long line to get the part of the lineHow to substitute the Output file created in round one back into the command in a FOR loop?
Difference between shutdown options
What (if any) is the reason to buy in small local stores?
Proving an identity involving cross products and coplanar vectors
Why the "ls" command is showing the permissions of files in a FAT32 partition?
Slur or Tie when they are mixed?
How to preserve electronics (computers, iPads and phones) for hundreds of years
Possible Eco thriller, man invents a device to remove rain from glass
Why does the frost depth increase when the surface temperature warms up?
Extracting patterns from a text
How to make a list of partial sums using forEach
Should a narrator ever describe things based on a character's view instead of facts?
Remove all of the duplicate numbers in an array of numbers - Javascript
How to Disable and Drop all Temporal Tables from a database
Consistent Linux device enumeration
Given this phrasing in the lease, when should I pay my rent?
Why didn’t Eve recognize the little cockroach as a living organism?
How do you justify more code being written by following clean code practices?
PTIJ: does fasting on Ta'anis Esther give us reward as if we celebrated 2 Purims? (similar to Yom Kippur)
How to write Quadratic equation with negative coefficient
Can I say "fingers" when referring to toes?
How much do grades matter for a future academia position?
How would a solely written language work mechanically
Do I have to take mana from my deck or hand when tapping a dual land?
Why didn't Voldemort know what Grindelwald looked like?
How to get the n-th line after a grepped one?
SSH client and Command Prompt replacements Windows look-and-feelCreate one file per line via shellosx bash grep - finding search terms in a large file with one single linegrep: output specific line after matchKeep grep output on one line?How to pass parameter in linux command at the end of the lineHow can I enrich my bash one-liner to get a default valuebash: how to print multiple arrays valuesUse grep for a long line to get the part of the lineHow to substitute the Output file created in round one back into the command in a FOR loop?
Consider the following text file
one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8
I would like to access to the second line after the one which matched four
. This would be the line
six 6
The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2
).
Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)
EDIT: in my specific case the occurrence of four
(to take that example) is guaranteed unique. But the answers show interesting extended cases when it is not.
linux command-line bash grep
add a comment |
Consider the following text file
one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8
I would like to access to the second line after the one which matched four
. This would be the line
six 6
The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2
).
Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)
EDIT: in my specific case the occurrence of four
(to take that example) is guaranteed unique. But the answers show interesting extended cases when it is not.
linux command-line bash grep
3
Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?
– Nasir Riley
Mar 16 at 22:36
3
Use awk? But if you're more familiar with Python that would be quicker.
– MZB
Mar 16 at 23:25
@NasirRiley: good point - I edited my question (four
will be unique)
– WoJ
2 days ago
add a comment |
Consider the following text file
one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8
I would like to access to the second line after the one which matched four
. This would be the line
six 6
The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2
).
Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)
EDIT: in my specific case the occurrence of four
(to take that example) is guaranteed unique. But the answers show interesting extended cases when it is not.
linux command-line bash grep
Consider the following text file
one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8
I would like to access to the second line after the one which matched four
. This would be the line
six 6
The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2
).
Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)
EDIT: in my specific case the occurrence of four
(to take that example) is guaranteed unique. But the answers show interesting extended cases when it is not.
linux command-line bash grep
linux command-line bash grep
edited 2 days ago
WoJ
asked Mar 16 at 22:07
WoJWoJ
85432238
85432238
3
Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?
– Nasir Riley
Mar 16 at 22:36
3
Use awk? But if you're more familiar with Python that would be quicker.
– MZB
Mar 16 at 23:25
@NasirRiley: good point - I edited my question (four
will be unique)
– WoJ
2 days ago
add a comment |
3
Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?
– Nasir Riley
Mar 16 at 22:36
3
Use awk? But if you're more familiar with Python that would be quicker.
– MZB
Mar 16 at 23:25
@NasirRiley: good point - I edited my question (four
will be unique)
– WoJ
2 days ago
3
3
Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?
– Nasir Riley
Mar 16 at 22:36
Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?
– Nasir Riley
Mar 16 at 22:36
3
3
Use awk? But if you're more familiar with Python that would be quicker.
– MZB
Mar 16 at 23:25
Use awk? But if you're more familiar with Python that would be quicker.
– MZB
Mar 16 at 23:25
@NasirRiley: good point - I edited my question (
four
will be unique)– WoJ
2 days ago
@NasirRiley: good point - I edited my question (
four
will be unique)– WoJ
2 days ago
add a comment |
6 Answers
6
active
oldest
votes
There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed
call:
sed -n "/four/ n; n; p " SourceData.txt
Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.
To match the first instance only:
sed -n "/four/ n; n; p; q " SourceData.txt
(Note that this answer is as efficient as possible, by ending the scan as soon as the match is found.)
I add this solution because it is worth getting to know sed
and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.
"the other solutions don't handle this case in the expected manner either" – No longer true. :)
– Kamil Maciorowski
Mar 17 at 2:08
@KamilMaciorowski - I didn't think ofawk
: obviously another tool worth learning in detail. And there's a Grymoire tutorial, too!
– AFH
Mar 17 at 14:32
@KamilMaciorowski - I've just seen the edit. I thought of adding a note about the efficiency of addingq
, but at the time I didn't know whether it answered the problem, so I didn't elaborate. The other answers (especially your own) should stand, since others with related problems may find them better solutions for their particular circumstances.
– AFH
2 days ago
add a comment |
Note: this answer was originally written before the OP clearly stated the pattern appears just once. It is designed not to miss any occurrence (unless near the end, so there's no "n-th line after") and I'm going to leave it this way. If you're sure there's only one occurrence or if you want only the first one to be found, you may consider some other solution that stops immediately and doesn't parse the whole input stream/file in vain.
This solution prints the current line iff there was a match two lines ago. It is slightly different from few other answers because it won't miss another match even if it occurs soon after the previous match.
awk -v delay=2 'for (i=delay; i>=0; i--) t[i]=t[i-1] /four/ t[0]="m" if (t[delay]) print'
Whenever there's a match, the information is stored in t[0]
. With each line the t
array is shifted (including shifting t[-1]
to t[0]
to reset the value of t[0]
). The line is printed iff the array indicates there was a match two lines ago.
You can easily set a different delay (e.g. delay=7
) or use another pattern (e.g. /sda[[:digit:]]/
)
This is the best solution, as it handles all occurrences of the pattern. However, it is quite long, might as well write a Python solution :D
– justhalf
Mar 17 at 12:25
2
@justhalf One aspect though:awk
is a standard POSIX tool,python
is not.
– Kamil Maciorowski
Mar 17 at 14:10
add a comment |
You can use this expression (input.txt
):
grep "four" -A 2 input.txt | tail -n 1
Output is:
six 6
The grep
option "-A 2" states that two lines after the matched line are outputted.
And the tail
option "-n 1" states that only the last 1
lines of this result are returned.
3
Note that this only works if there's just a single match, or you're only interested in the last match.
– Barmar
Mar 17 at 7:53
add a comment |
For multiple occurences, and assumming that no lines start with --
:
( grep -A 2 pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'
In slo-mo:
( grep -A 2 pattern data.txt; echo '--' )
prints the pattern and the next two lines, and inserts a--
line between the groups.echo '--'
makes sure that the last group is also followed by--
.grep -E -B1 '^--'
print the separators and the lines just before (whoch are the ones we are looking for)grep -Ev '^--'
drops the separators, leaving only the lines we are looking for.
if you use the-A
,-B
or-C
option thengrep
automatically use--
to separate matches, no need to print it yourself
– phuclv
Mar 17 at 0:47
Doesn't add a '--' after the last., and it there is none, the rest of the pipeline won't get the last target (unless I add a tail -1 to every stage).
– xenoid
Mar 17 at 10:16
2
I suggest searching for '^--$' as a way to reduce your failure mode still further.
– Slartibartfast
2 days ago
add a comment |
Looks like a good use case for ex
, the POSIX-specified scriptable file editor.
Unlike sed and awk, ex
is actually designed for file editing, not stream editing, and is capable of going backwards and forwards in a file. It's actually the non-visual form of the vi
editor.
But the important aspect here is that ex
is capable of chaining addresses. So referring to the line that's two lines after a particular text pattern is trivial.
Here is a command which prints all the lines that come two lines after lines containing four
:
printf '%sn' 'g/four/+2p' | ex file.txt
I've written a lot of answers using ex
on the Unix & Linux Stack Exchange; this one in particular has some additional explanations that may help.
add a comment |
You already got very good answers, which are easily the true go-to solutions for one-liners, but just in case you'd like or need to do it pure-bash only you might start from the following:
while read letters _ && [ "$letters" != four ] ; do :; done ; read && read _ number _ && echo $number ; < data.txt
It only yields the first occurrence of "four".
Here I used _
as the name for a placeholder variable, i.e. a variable that receives values to be discarded, and as an "added bonus" you have the | cut -f2
result already built into the script.
Also please note that above script is just a proof-of-concept: it only works with the exampled input data.
It could be enhanced by:
- reading the entire (unsplit) line to match it against a regex via the
[[
command (in place of[
) along with its=~
operator, like in:while read line && ! [[ "$line" =~ four ]]
but beware the then additional complexity given by bash’s own escaping rules if the regex gets more complex than a simple "four"; also note the!
preceding the[[
to negate the test - joining the
[
(or[[
) command with the tworead
and the&& echo ...
together inside the while loop, to not stop after first match, like in:
while read letters _ ; do [ "$letters" = four ] && read && read _ number _ && echo $number ; done ; < data.txt
Note also the change of the comparison operator. However beware of missing possibly adjacent matching lines. In order to address this possibility you’d need some kind of look-ahead or back-caching, for which you’d need some more advanced bash constructs.
Lastly, of course you can rather output the entire unsplit line via read line && echo $line
in place of read _ number _ && echo $number
New contributor
So you’re assuming that the actual data is exactly like the sample data — the pattern to be searched for is a simple word that appears as a whole word, by itself, as the first word on a line. But the question doesn’t say that; the sample data should be considered as just an example. What if the line matchingfour
actually says “I have fourteen cats.”? What if the user actually wants to do this with a non-trivial regular expression (e.g.,fox*ur
)? … (Cont’d)
– Scott
yesterday
(Cont’d) … What’s worse, the question clearly says that it wants to get the n-th (second) line after the line that matched — not the second word from that line. Inasmuch as| cut -d' ' -f2
was also given as an example, baking it into your answer doesn’t earn you any points. … … … … … … … … … … … … … … Aside from that, this is a valid contribution to the discussion. When you fix the above problems, consider showing two versions: one that finds the first match and stops, and another that finds all the matches.
– Scott
yesterday
@Scott: Well, my "script" is an example too, as much as the question’s information is an example itself - after all I did say "you could start with the following", and I suppose the script does look like just the proof-of-concept it indeed is. But I should make that clearer, agreed. Then, should I provide that much more functionality than explicitly requested ? In order to implement those additional features the script might easily become way more complex than it already is. Wouldn’t that risk to be an overkill ? I think I’d rather wait for explicit inquiries, if and when any (cont'd)
– LL3
yesterday
(cont'd) Wrt the “doing away the cut -f2 part”, it was certainly not for gaining points (nor is my entire contribution as a whole, considering that it is in general quite inferior (ie more complicated) to the other solutions), rather it just cost nothing to do it rather than not, considering that the question mentioned it as a likely subsequent processing of the input data. I contribute with a HTH mindset, I can do without an upvote and am happy with just a “nice, thank you”.
– LL3
yesterday
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "3"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f1414661%2fhow-to-get-the-n-th-line-after-a-grepped-one%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed
call:
sed -n "/four/ n; n; p " SourceData.txt
Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.
To match the first instance only:
sed -n "/four/ n; n; p; q " SourceData.txt
(Note that this answer is as efficient as possible, by ending the scan as soon as the match is found.)
I add this solution because it is worth getting to know sed
and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.
"the other solutions don't handle this case in the expected manner either" – No longer true. :)
– Kamil Maciorowski
Mar 17 at 2:08
@KamilMaciorowski - I didn't think ofawk
: obviously another tool worth learning in detail. And there's a Grymoire tutorial, too!
– AFH
Mar 17 at 14:32
@KamilMaciorowski - I've just seen the edit. I thought of adding a note about the efficiency of addingq
, but at the time I didn't know whether it answered the problem, so I didn't elaborate. The other answers (especially your own) should stand, since others with related problems may find them better solutions for their particular circumstances.
– AFH
2 days ago
add a comment |
There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed
call:
sed -n "/four/ n; n; p " SourceData.txt
Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.
To match the first instance only:
sed -n "/four/ n; n; p; q " SourceData.txt
(Note that this answer is as efficient as possible, by ending the scan as soon as the match is found.)
I add this solution because it is worth getting to know sed
and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.
"the other solutions don't handle this case in the expected manner either" – No longer true. :)
– Kamil Maciorowski
Mar 17 at 2:08
@KamilMaciorowski - I didn't think ofawk
: obviously another tool worth learning in detail. And there's a Grymoire tutorial, too!
– AFH
Mar 17 at 14:32
@KamilMaciorowski - I've just seen the edit. I thought of adding a note about the efficiency of addingq
, but at the time I didn't know whether it answered the problem, so I didn't elaborate. The other answers (especially your own) should stand, since others with related problems may find them better solutions for their particular circumstances.
– AFH
2 days ago
add a comment |
There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed
call:
sed -n "/four/ n; n; p " SourceData.txt
Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.
To match the first instance only:
sed -n "/four/ n; n; p; q " SourceData.txt
(Note that this answer is as efficient as possible, by ending the scan as soon as the match is found.)
I add this solution because it is worth getting to know sed
and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.
There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed
call:
sed -n "/four/ n; n; p " SourceData.txt
Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.
To match the first instance only:
sed -n "/four/ n; n; p; q " SourceData.txt
(Note that this answer is as efficient as possible, by ending the scan as soon as the match is found.)
I add this solution because it is worth getting to know sed
and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.
edited yesterday
answered Mar 17 at 0:14
AFHAFH
14.6k31939
14.6k31939
"the other solutions don't handle this case in the expected manner either" – No longer true. :)
– Kamil Maciorowski
Mar 17 at 2:08
@KamilMaciorowski - I didn't think ofawk
: obviously another tool worth learning in detail. And there's a Grymoire tutorial, too!
– AFH
Mar 17 at 14:32
@KamilMaciorowski - I've just seen the edit. I thought of adding a note about the efficiency of addingq
, but at the time I didn't know whether it answered the problem, so I didn't elaborate. The other answers (especially your own) should stand, since others with related problems may find them better solutions for their particular circumstances.
– AFH
2 days ago
add a comment |
"the other solutions don't handle this case in the expected manner either" – No longer true. :)
– Kamil Maciorowski
Mar 17 at 2:08
@KamilMaciorowski - I didn't think ofawk
: obviously another tool worth learning in detail. And there's a Grymoire tutorial, too!
– AFH
Mar 17 at 14:32
@KamilMaciorowski - I've just seen the edit. I thought of adding a note about the efficiency of addingq
, but at the time I didn't know whether it answered the problem, so I didn't elaborate. The other answers (especially your own) should stand, since others with related problems may find them better solutions for their particular circumstances.
– AFH
2 days ago
"the other solutions don't handle this case in the expected manner either" – No longer true. :)
– Kamil Maciorowski
Mar 17 at 2:08
"the other solutions don't handle this case in the expected manner either" – No longer true. :)
– Kamil Maciorowski
Mar 17 at 2:08
@KamilMaciorowski - I didn't think of
awk
: obviously another tool worth learning in detail. And there's a Grymoire tutorial, too!– AFH
Mar 17 at 14:32
@KamilMaciorowski - I didn't think of
awk
: obviously another tool worth learning in detail. And there's a Grymoire tutorial, too!– AFH
Mar 17 at 14:32
@KamilMaciorowski - I've just seen the edit. I thought of adding a note about the efficiency of adding
q
, but at the time I didn't know whether it answered the problem, so I didn't elaborate. The other answers (especially your own) should stand, since others with related problems may find them better solutions for their particular circumstances.– AFH
2 days ago
@KamilMaciorowski - I've just seen the edit. I thought of adding a note about the efficiency of adding
q
, but at the time I didn't know whether it answered the problem, so I didn't elaborate. The other answers (especially your own) should stand, since others with related problems may find them better solutions for their particular circumstances.– AFH
2 days ago
add a comment |
Note: this answer was originally written before the OP clearly stated the pattern appears just once. It is designed not to miss any occurrence (unless near the end, so there's no "n-th line after") and I'm going to leave it this way. If you're sure there's only one occurrence or if you want only the first one to be found, you may consider some other solution that stops immediately and doesn't parse the whole input stream/file in vain.
This solution prints the current line iff there was a match two lines ago. It is slightly different from few other answers because it won't miss another match even if it occurs soon after the previous match.
awk -v delay=2 'for (i=delay; i>=0; i--) t[i]=t[i-1] /four/ t[0]="m" if (t[delay]) print'
Whenever there's a match, the information is stored in t[0]
. With each line the t
array is shifted (including shifting t[-1]
to t[0]
to reset the value of t[0]
). The line is printed iff the array indicates there was a match two lines ago.
You can easily set a different delay (e.g. delay=7
) or use another pattern (e.g. /sda[[:digit:]]/
)
This is the best solution, as it handles all occurrences of the pattern. However, it is quite long, might as well write a Python solution :D
– justhalf
Mar 17 at 12:25
2
@justhalf One aspect though:awk
is a standard POSIX tool,python
is not.
– Kamil Maciorowski
Mar 17 at 14:10
add a comment |
Note: this answer was originally written before the OP clearly stated the pattern appears just once. It is designed not to miss any occurrence (unless near the end, so there's no "n-th line after") and I'm going to leave it this way. If you're sure there's only one occurrence or if you want only the first one to be found, you may consider some other solution that stops immediately and doesn't parse the whole input stream/file in vain.
This solution prints the current line iff there was a match two lines ago. It is slightly different from few other answers because it won't miss another match even if it occurs soon after the previous match.
awk -v delay=2 'for (i=delay; i>=0; i--) t[i]=t[i-1] /four/ t[0]="m" if (t[delay]) print'
Whenever there's a match, the information is stored in t[0]
. With each line the t
array is shifted (including shifting t[-1]
to t[0]
to reset the value of t[0]
). The line is printed iff the array indicates there was a match two lines ago.
You can easily set a different delay (e.g. delay=7
) or use another pattern (e.g. /sda[[:digit:]]/
)
This is the best solution, as it handles all occurrences of the pattern. However, it is quite long, might as well write a Python solution :D
– justhalf
Mar 17 at 12:25
2
@justhalf One aspect though:awk
is a standard POSIX tool,python
is not.
– Kamil Maciorowski
Mar 17 at 14:10
add a comment |
Note: this answer was originally written before the OP clearly stated the pattern appears just once. It is designed not to miss any occurrence (unless near the end, so there's no "n-th line after") and I'm going to leave it this way. If you're sure there's only one occurrence or if you want only the first one to be found, you may consider some other solution that stops immediately and doesn't parse the whole input stream/file in vain.
This solution prints the current line iff there was a match two lines ago. It is slightly different from few other answers because it won't miss another match even if it occurs soon after the previous match.
awk -v delay=2 'for (i=delay; i>=0; i--) t[i]=t[i-1] /four/ t[0]="m" if (t[delay]) print'
Whenever there's a match, the information is stored in t[0]
. With each line the t
array is shifted (including shifting t[-1]
to t[0]
to reset the value of t[0]
). The line is printed iff the array indicates there was a match two lines ago.
You can easily set a different delay (e.g. delay=7
) or use another pattern (e.g. /sda[[:digit:]]/
)
Note: this answer was originally written before the OP clearly stated the pattern appears just once. It is designed not to miss any occurrence (unless near the end, so there's no "n-th line after") and I'm going to leave it this way. If you're sure there's only one occurrence or if you want only the first one to be found, you may consider some other solution that stops immediately and doesn't parse the whole input stream/file in vain.
This solution prints the current line iff there was a match two lines ago. It is slightly different from few other answers because it won't miss another match even if it occurs soon after the previous match.
awk -v delay=2 'for (i=delay; i>=0; i--) t[i]=t[i-1] /four/ t[0]="m" if (t[delay]) print'
Whenever there's a match, the information is stored in t[0]
. With each line the t
array is shifted (including shifting t[-1]
to t[0]
to reset the value of t[0]
). The line is printed iff the array indicates there was a match two lines ago.
You can easily set a different delay (e.g. delay=7
) or use another pattern (e.g. /sda[[:digit:]]/
)
edited 2 days ago
answered Mar 17 at 2:04
Kamil MaciorowskiKamil Maciorowski
28.5k156187
28.5k156187
This is the best solution, as it handles all occurrences of the pattern. However, it is quite long, might as well write a Python solution :D
– justhalf
Mar 17 at 12:25
2
@justhalf One aspect though:awk
is a standard POSIX tool,python
is not.
– Kamil Maciorowski
Mar 17 at 14:10
add a comment |
This is the best solution, as it handles all occurrences of the pattern. However, it is quite long, might as well write a Python solution :D
– justhalf
Mar 17 at 12:25
2
@justhalf One aspect though:awk
is a standard POSIX tool,python
is not.
– Kamil Maciorowski
Mar 17 at 14:10
This is the best solution, as it handles all occurrences of the pattern. However, it is quite long, might as well write a Python solution :D
– justhalf
Mar 17 at 12:25
This is the best solution, as it handles all occurrences of the pattern. However, it is quite long, might as well write a Python solution :D
– justhalf
Mar 17 at 12:25
2
2
@justhalf One aspect though:
awk
is a standard POSIX tool, python
is not.– Kamil Maciorowski
Mar 17 at 14:10
@justhalf One aspect though:
awk
is a standard POSIX tool, python
is not.– Kamil Maciorowski
Mar 17 at 14:10
add a comment |
You can use this expression (input.txt
):
grep "four" -A 2 input.txt | tail -n 1
Output is:
six 6
The grep
option "-A 2" states that two lines after the matched line are outputted.
And the tail
option "-n 1" states that only the last 1
lines of this result are returned.
3
Note that this only works if there's just a single match, or you're only interested in the last match.
– Barmar
Mar 17 at 7:53
add a comment |
You can use this expression (input.txt
):
grep "four" -A 2 input.txt | tail -n 1
Output is:
six 6
The grep
option "-A 2" states that two lines after the matched line are outputted.
And the tail
option "-n 1" states that only the last 1
lines of this result are returned.
3
Note that this only works if there's just a single match, or you're only interested in the last match.
– Barmar
Mar 17 at 7:53
add a comment |
You can use this expression (input.txt
):
grep "four" -A 2 input.txt | tail -n 1
Output is:
six 6
The grep
option "-A 2" states that two lines after the matched line are outputted.
And the tail
option "-n 1" states that only the last 1
lines of this result are returned.
You can use this expression (input.txt
):
grep "four" -A 2 input.txt | tail -n 1
Output is:
six 6
The grep
option "-A 2" states that two lines after the matched line are outputted.
And the tail
option "-n 1" states that only the last 1
lines of this result are returned.
answered Mar 16 at 22:16
zx485zx485
1,0991913
1,0991913
3
Note that this only works if there's just a single match, or you're only interested in the last match.
– Barmar
Mar 17 at 7:53
add a comment |
3
Note that this only works if there's just a single match, or you're only interested in the last match.
– Barmar
Mar 17 at 7:53
3
3
Note that this only works if there's just a single match, or you're only interested in the last match.
– Barmar
Mar 17 at 7:53
Note that this only works if there's just a single match, or you're only interested in the last match.
– Barmar
Mar 17 at 7:53
add a comment |
For multiple occurences, and assumming that no lines start with --
:
( grep -A 2 pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'
In slo-mo:
( grep -A 2 pattern data.txt; echo '--' )
prints the pattern and the next two lines, and inserts a--
line between the groups.echo '--'
makes sure that the last group is also followed by--
.grep -E -B1 '^--'
print the separators and the lines just before (whoch are the ones we are looking for)grep -Ev '^--'
drops the separators, leaving only the lines we are looking for.
if you use the-A
,-B
or-C
option thengrep
automatically use--
to separate matches, no need to print it yourself
– phuclv
Mar 17 at 0:47
Doesn't add a '--' after the last., and it there is none, the rest of the pipeline won't get the last target (unless I add a tail -1 to every stage).
– xenoid
Mar 17 at 10:16
2
I suggest searching for '^--$' as a way to reduce your failure mode still further.
– Slartibartfast
2 days ago
add a comment |
For multiple occurences, and assumming that no lines start with --
:
( grep -A 2 pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'
In slo-mo:
( grep -A 2 pattern data.txt; echo '--' )
prints the pattern and the next two lines, and inserts a--
line between the groups.echo '--'
makes sure that the last group is also followed by--
.grep -E -B1 '^--'
print the separators and the lines just before (whoch are the ones we are looking for)grep -Ev '^--'
drops the separators, leaving only the lines we are looking for.
if you use the-A
,-B
or-C
option thengrep
automatically use--
to separate matches, no need to print it yourself
– phuclv
Mar 17 at 0:47
Doesn't add a '--' after the last., and it there is none, the rest of the pipeline won't get the last target (unless I add a tail -1 to every stage).
– xenoid
Mar 17 at 10:16
2
I suggest searching for '^--$' as a way to reduce your failure mode still further.
– Slartibartfast
2 days ago
add a comment |
For multiple occurences, and assumming that no lines start with --
:
( grep -A 2 pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'
In slo-mo:
( grep -A 2 pattern data.txt; echo '--' )
prints the pattern and the next two lines, and inserts a--
line between the groups.echo '--'
makes sure that the last group is also followed by--
.grep -E -B1 '^--'
print the separators and the lines just before (whoch are the ones we are looking for)grep -Ev '^--'
drops the separators, leaving only the lines we are looking for.
For multiple occurences, and assumming that no lines start with --
:
( grep -A 2 pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'
In slo-mo:
( grep -A 2 pattern data.txt; echo '--' )
prints the pattern and the next two lines, and inserts a--
line between the groups.echo '--'
makes sure that the last group is also followed by--
.grep -E -B1 '^--'
print the separators and the lines just before (whoch are the ones we are looking for)grep -Ev '^--'
drops the separators, leaving only the lines we are looking for.
answered Mar 16 at 23:07
xenoidxenoid
3,8893719
3,8893719
if you use the-A
,-B
or-C
option thengrep
automatically use--
to separate matches, no need to print it yourself
– phuclv
Mar 17 at 0:47
Doesn't add a '--' after the last., and it there is none, the rest of the pipeline won't get the last target (unless I add a tail -1 to every stage).
– xenoid
Mar 17 at 10:16
2
I suggest searching for '^--$' as a way to reduce your failure mode still further.
– Slartibartfast
2 days ago
add a comment |
if you use the-A
,-B
or-C
option thengrep
automatically use--
to separate matches, no need to print it yourself
– phuclv
Mar 17 at 0:47
Doesn't add a '--' after the last., and it there is none, the rest of the pipeline won't get the last target (unless I add a tail -1 to every stage).
– xenoid
Mar 17 at 10:16
2
I suggest searching for '^--$' as a way to reduce your failure mode still further.
– Slartibartfast
2 days ago
if you use the
-A
, -B
or -C
option then grep
automatically use --
to separate matches, no need to print it yourself– phuclv
Mar 17 at 0:47
if you use the
-A
, -B
or -C
option then grep
automatically use --
to separate matches, no need to print it yourself– phuclv
Mar 17 at 0:47
Doesn't add a '--' after the last., and it there is none, the rest of the pipeline won't get the last target (unless I add a tail -1 to every stage).
– xenoid
Mar 17 at 10:16
Doesn't add a '--' after the last., and it there is none, the rest of the pipeline won't get the last target (unless I add a tail -1 to every stage).
– xenoid
Mar 17 at 10:16
2
2
I suggest searching for '^--$' as a way to reduce your failure mode still further.
– Slartibartfast
2 days ago
I suggest searching for '^--$' as a way to reduce your failure mode still further.
– Slartibartfast
2 days ago
add a comment |
Looks like a good use case for ex
, the POSIX-specified scriptable file editor.
Unlike sed and awk, ex
is actually designed for file editing, not stream editing, and is capable of going backwards and forwards in a file. It's actually the non-visual form of the vi
editor.
But the important aspect here is that ex
is capable of chaining addresses. So referring to the line that's two lines after a particular text pattern is trivial.
Here is a command which prints all the lines that come two lines after lines containing four
:
printf '%sn' 'g/four/+2p' | ex file.txt
I've written a lot of answers using ex
on the Unix & Linux Stack Exchange; this one in particular has some additional explanations that may help.
add a comment |
Looks like a good use case for ex
, the POSIX-specified scriptable file editor.
Unlike sed and awk, ex
is actually designed for file editing, not stream editing, and is capable of going backwards and forwards in a file. It's actually the non-visual form of the vi
editor.
But the important aspect here is that ex
is capable of chaining addresses. So referring to the line that's two lines after a particular text pattern is trivial.
Here is a command which prints all the lines that come two lines after lines containing four
:
printf '%sn' 'g/four/+2p' | ex file.txt
I've written a lot of answers using ex
on the Unix & Linux Stack Exchange; this one in particular has some additional explanations that may help.
add a comment |
Looks like a good use case for ex
, the POSIX-specified scriptable file editor.
Unlike sed and awk, ex
is actually designed for file editing, not stream editing, and is capable of going backwards and forwards in a file. It's actually the non-visual form of the vi
editor.
But the important aspect here is that ex
is capable of chaining addresses. So referring to the line that's two lines after a particular text pattern is trivial.
Here is a command which prints all the lines that come two lines after lines containing four
:
printf '%sn' 'g/four/+2p' | ex file.txt
I've written a lot of answers using ex
on the Unix & Linux Stack Exchange; this one in particular has some additional explanations that may help.
Looks like a good use case for ex
, the POSIX-specified scriptable file editor.
Unlike sed and awk, ex
is actually designed for file editing, not stream editing, and is capable of going backwards and forwards in a file. It's actually the non-visual form of the vi
editor.
But the important aspect here is that ex
is capable of chaining addresses. So referring to the line that's two lines after a particular text pattern is trivial.
Here is a command which prints all the lines that come two lines after lines containing four
:
printf '%sn' 'g/four/+2p' | ex file.txt
I've written a lot of answers using ex
on the Unix & Linux Stack Exchange; this one in particular has some additional explanations that may help.
answered 2 days ago
WildcardWildcard
313517
313517
add a comment |
add a comment |
You already got very good answers, which are easily the true go-to solutions for one-liners, but just in case you'd like or need to do it pure-bash only you might start from the following:
while read letters _ && [ "$letters" != four ] ; do :; done ; read && read _ number _ && echo $number ; < data.txt
It only yields the first occurrence of "four".
Here I used _
as the name for a placeholder variable, i.e. a variable that receives values to be discarded, and as an "added bonus" you have the | cut -f2
result already built into the script.
Also please note that above script is just a proof-of-concept: it only works with the exampled input data.
It could be enhanced by:
- reading the entire (unsplit) line to match it against a regex via the
[[
command (in place of[
) along with its=~
operator, like in:while read line && ! [[ "$line" =~ four ]]
but beware the then additional complexity given by bash’s own escaping rules if the regex gets more complex than a simple "four"; also note the!
preceding the[[
to negate the test - joining the
[
(or[[
) command with the tworead
and the&& echo ...
together inside the while loop, to not stop after first match, like in:
while read letters _ ; do [ "$letters" = four ] && read && read _ number _ && echo $number ; done ; < data.txt
Note also the change of the comparison operator. However beware of missing possibly adjacent matching lines. In order to address this possibility you’d need some kind of look-ahead or back-caching, for which you’d need some more advanced bash constructs.
Lastly, of course you can rather output the entire unsplit line via read line && echo $line
in place of read _ number _ && echo $number
New contributor
So you’re assuming that the actual data is exactly like the sample data — the pattern to be searched for is a simple word that appears as a whole word, by itself, as the first word on a line. But the question doesn’t say that; the sample data should be considered as just an example. What if the line matchingfour
actually says “I have fourteen cats.”? What if the user actually wants to do this with a non-trivial regular expression (e.g.,fox*ur
)? … (Cont’d)
– Scott
yesterday
(Cont’d) … What’s worse, the question clearly says that it wants to get the n-th (second) line after the line that matched — not the second word from that line. Inasmuch as| cut -d' ' -f2
was also given as an example, baking it into your answer doesn’t earn you any points. … … … … … … … … … … … … … … Aside from that, this is a valid contribution to the discussion. When you fix the above problems, consider showing two versions: one that finds the first match and stops, and another that finds all the matches.
– Scott
yesterday
@Scott: Well, my "script" is an example too, as much as the question’s information is an example itself - after all I did say "you could start with the following", and I suppose the script does look like just the proof-of-concept it indeed is. But I should make that clearer, agreed. Then, should I provide that much more functionality than explicitly requested ? In order to implement those additional features the script might easily become way more complex than it already is. Wouldn’t that risk to be an overkill ? I think I’d rather wait for explicit inquiries, if and when any (cont'd)
– LL3
yesterday
(cont'd) Wrt the “doing away the cut -f2 part”, it was certainly not for gaining points (nor is my entire contribution as a whole, considering that it is in general quite inferior (ie more complicated) to the other solutions), rather it just cost nothing to do it rather than not, considering that the question mentioned it as a likely subsequent processing of the input data. I contribute with a HTH mindset, I can do without an upvote and am happy with just a “nice, thank you”.
– LL3
yesterday
add a comment |
You already got very good answers, which are easily the true go-to solutions for one-liners, but just in case you'd like or need to do it pure-bash only you might start from the following:
while read letters _ && [ "$letters" != four ] ; do :; done ; read && read _ number _ && echo $number ; < data.txt
It only yields the first occurrence of "four".
Here I used _
as the name for a placeholder variable, i.e. a variable that receives values to be discarded, and as an "added bonus" you have the | cut -f2
result already built into the script.
Also please note that above script is just a proof-of-concept: it only works with the exampled input data.
It could be enhanced by:
- reading the entire (unsplit) line to match it against a regex via the
[[
command (in place of[
) along with its=~
operator, like in:while read line && ! [[ "$line" =~ four ]]
but beware the then additional complexity given by bash’s own escaping rules if the regex gets more complex than a simple "four"; also note the!
preceding the[[
to negate the test - joining the
[
(or[[
) command with the tworead
and the&& echo ...
together inside the while loop, to not stop after first match, like in:
while read letters _ ; do [ "$letters" = four ] && read && read _ number _ && echo $number ; done ; < data.txt
Note also the change of the comparison operator. However beware of missing possibly adjacent matching lines. In order to address this possibility you’d need some kind of look-ahead or back-caching, for which you’d need some more advanced bash constructs.
Lastly, of course you can rather output the entire unsplit line via read line && echo $line
in place of read _ number _ && echo $number
New contributor
So you’re assuming that the actual data is exactly like the sample data — the pattern to be searched for is a simple word that appears as a whole word, by itself, as the first word on a line. But the question doesn’t say that; the sample data should be considered as just an example. What if the line matchingfour
actually says “I have fourteen cats.”? What if the user actually wants to do this with a non-trivial regular expression (e.g.,fox*ur
)? … (Cont’d)
– Scott
yesterday
(Cont’d) … What’s worse, the question clearly says that it wants to get the n-th (second) line after the line that matched — not the second word from that line. Inasmuch as| cut -d' ' -f2
was also given as an example, baking it into your answer doesn’t earn you any points. … … … … … … … … … … … … … … Aside from that, this is a valid contribution to the discussion. When you fix the above problems, consider showing two versions: one that finds the first match and stops, and another that finds all the matches.
– Scott
yesterday
@Scott: Well, my "script" is an example too, as much as the question’s information is an example itself - after all I did say "you could start with the following", and I suppose the script does look like just the proof-of-concept it indeed is. But I should make that clearer, agreed. Then, should I provide that much more functionality than explicitly requested ? In order to implement those additional features the script might easily become way more complex than it already is. Wouldn’t that risk to be an overkill ? I think I’d rather wait for explicit inquiries, if and when any (cont'd)
– LL3
yesterday
(cont'd) Wrt the “doing away the cut -f2 part”, it was certainly not for gaining points (nor is my entire contribution as a whole, considering that it is in general quite inferior (ie more complicated) to the other solutions), rather it just cost nothing to do it rather than not, considering that the question mentioned it as a likely subsequent processing of the input data. I contribute with a HTH mindset, I can do without an upvote and am happy with just a “nice, thank you”.
– LL3
yesterday
add a comment |
You already got very good answers, which are easily the true go-to solutions for one-liners, but just in case you'd like or need to do it pure-bash only you might start from the following:
while read letters _ && [ "$letters" != four ] ; do :; done ; read && read _ number _ && echo $number ; < data.txt
It only yields the first occurrence of "four".
Here I used _
as the name for a placeholder variable, i.e. a variable that receives values to be discarded, and as an "added bonus" you have the | cut -f2
result already built into the script.
Also please note that above script is just a proof-of-concept: it only works with the exampled input data.
It could be enhanced by:
- reading the entire (unsplit) line to match it against a regex via the
[[
command (in place of[
) along with its=~
operator, like in:while read line && ! [[ "$line" =~ four ]]
but beware the then additional complexity given by bash’s own escaping rules if the regex gets more complex than a simple "four"; also note the!
preceding the[[
to negate the test - joining the
[
(or[[
) command with the tworead
and the&& echo ...
together inside the while loop, to not stop after first match, like in:
while read letters _ ; do [ "$letters" = four ] && read && read _ number _ && echo $number ; done ; < data.txt
Note also the change of the comparison operator. However beware of missing possibly adjacent matching lines. In order to address this possibility you’d need some kind of look-ahead or back-caching, for which you’d need some more advanced bash constructs.
Lastly, of course you can rather output the entire unsplit line via read line && echo $line
in place of read _ number _ && echo $number
New contributor
You already got very good answers, which are easily the true go-to solutions for one-liners, but just in case you'd like or need to do it pure-bash only you might start from the following:
while read letters _ && [ "$letters" != four ] ; do :; done ; read && read _ number _ && echo $number ; < data.txt
It only yields the first occurrence of "four".
Here I used _
as the name for a placeholder variable, i.e. a variable that receives values to be discarded, and as an "added bonus" you have the | cut -f2
result already built into the script.
Also please note that above script is just a proof-of-concept: it only works with the exampled input data.
It could be enhanced by:
- reading the entire (unsplit) line to match it against a regex via the
[[
command (in place of[
) along with its=~
operator, like in:while read line && ! [[ "$line" =~ four ]]
but beware the then additional complexity given by bash’s own escaping rules if the regex gets more complex than a simple "four"; also note the!
preceding the[[
to negate the test - joining the
[
(or[[
) command with the tworead
and the&& echo ...
together inside the while loop, to not stop after first match, like in:
while read letters _ ; do [ "$letters" = four ] && read && read _ number _ && echo $number ; done ; < data.txt
Note also the change of the comparison operator. However beware of missing possibly adjacent matching lines. In order to address this possibility you’d need some kind of look-ahead or back-caching, for which you’d need some more advanced bash constructs.
Lastly, of course you can rather output the entire unsplit line via read line && echo $line
in place of read _ number _ && echo $number
New contributor
edited yesterday
New contributor
answered 2 days ago
LL3LL3
162
162
New contributor
New contributor
So you’re assuming that the actual data is exactly like the sample data — the pattern to be searched for is a simple word that appears as a whole word, by itself, as the first word on a line. But the question doesn’t say that; the sample data should be considered as just an example. What if the line matchingfour
actually says “I have fourteen cats.”? What if the user actually wants to do this with a non-trivial regular expression (e.g.,fox*ur
)? … (Cont’d)
– Scott
yesterday
(Cont’d) … What’s worse, the question clearly says that it wants to get the n-th (second) line after the line that matched — not the second word from that line. Inasmuch as| cut -d' ' -f2
was also given as an example, baking it into your answer doesn’t earn you any points. … … … … … … … … … … … … … … Aside from that, this is a valid contribution to the discussion. When you fix the above problems, consider showing two versions: one that finds the first match and stops, and another that finds all the matches.
– Scott
yesterday
@Scott: Well, my "script" is an example too, as much as the question’s information is an example itself - after all I did say "you could start with the following", and I suppose the script does look like just the proof-of-concept it indeed is. But I should make that clearer, agreed. Then, should I provide that much more functionality than explicitly requested ? In order to implement those additional features the script might easily become way more complex than it already is. Wouldn’t that risk to be an overkill ? I think I’d rather wait for explicit inquiries, if and when any (cont'd)
– LL3
yesterday
(cont'd) Wrt the “doing away the cut -f2 part”, it was certainly not for gaining points (nor is my entire contribution as a whole, considering that it is in general quite inferior (ie more complicated) to the other solutions), rather it just cost nothing to do it rather than not, considering that the question mentioned it as a likely subsequent processing of the input data. I contribute with a HTH mindset, I can do without an upvote and am happy with just a “nice, thank you”.
– LL3
yesterday
add a comment |
So you’re assuming that the actual data is exactly like the sample data — the pattern to be searched for is a simple word that appears as a whole word, by itself, as the first word on a line. But the question doesn’t say that; the sample data should be considered as just an example. What if the line matchingfour
actually says “I have fourteen cats.”? What if the user actually wants to do this with a non-trivial regular expression (e.g.,fox*ur
)? … (Cont’d)
– Scott
yesterday
(Cont’d) … What’s worse, the question clearly says that it wants to get the n-th (second) line after the line that matched — not the second word from that line. Inasmuch as| cut -d' ' -f2
was also given as an example, baking it into your answer doesn’t earn you any points. … … … … … … … … … … … … … … Aside from that, this is a valid contribution to the discussion. When you fix the above problems, consider showing two versions: one that finds the first match and stops, and another that finds all the matches.
– Scott
yesterday
@Scott: Well, my "script" is an example too, as much as the question’s information is an example itself - after all I did say "you could start with the following", and I suppose the script does look like just the proof-of-concept it indeed is. But I should make that clearer, agreed. Then, should I provide that much more functionality than explicitly requested ? In order to implement those additional features the script might easily become way more complex than it already is. Wouldn’t that risk to be an overkill ? I think I’d rather wait for explicit inquiries, if and when any (cont'd)
– LL3
yesterday
(cont'd) Wrt the “doing away the cut -f2 part”, it was certainly not for gaining points (nor is my entire contribution as a whole, considering that it is in general quite inferior (ie more complicated) to the other solutions), rather it just cost nothing to do it rather than not, considering that the question mentioned it as a likely subsequent processing of the input data. I contribute with a HTH mindset, I can do without an upvote and am happy with just a “nice, thank you”.
– LL3
yesterday
So you’re assuming that the actual data is exactly like the sample data — the pattern to be searched for is a simple word that appears as a whole word, by itself, as the first word on a line. But the question doesn’t say that; the sample data should be considered as just an example. What if the line matching
four
actually says “I have fourteen cats.”? What if the user actually wants to do this with a non-trivial regular expression (e.g., fox*ur
)? … (Cont’d)– Scott
yesterday
So you’re assuming that the actual data is exactly like the sample data — the pattern to be searched for is a simple word that appears as a whole word, by itself, as the first word on a line. But the question doesn’t say that; the sample data should be considered as just an example. What if the line matching
four
actually says “I have fourteen cats.”? What if the user actually wants to do this with a non-trivial regular expression (e.g., fox*ur
)? … (Cont’d)– Scott
yesterday
(Cont’d) … What’s worse, the question clearly says that it wants to get the n-th (second) line after the line that matched — not the second word from that line. Inasmuch as
| cut -d' ' -f2
was also given as an example, baking it into your answer doesn’t earn you any points. … … … … … … … … … … … … … … Aside from that, this is a valid contribution to the discussion. When you fix the above problems, consider showing two versions: one that finds the first match and stops, and another that finds all the matches.– Scott
yesterday
(Cont’d) … What’s worse, the question clearly says that it wants to get the n-th (second) line after the line that matched — not the second word from that line. Inasmuch as
| cut -d' ' -f2
was also given as an example, baking it into your answer doesn’t earn you any points. … … … … … … … … … … … … … … Aside from that, this is a valid contribution to the discussion. When you fix the above problems, consider showing two versions: one that finds the first match and stops, and another that finds all the matches.– Scott
yesterday
@Scott: Well, my "script" is an example too, as much as the question’s information is an example itself - after all I did say "you could start with the following", and I suppose the script does look like just the proof-of-concept it indeed is. But I should make that clearer, agreed. Then, should I provide that much more functionality than explicitly requested ? In order to implement those additional features the script might easily become way more complex than it already is. Wouldn’t that risk to be an overkill ? I think I’d rather wait for explicit inquiries, if and when any (cont'd)
– LL3
yesterday
@Scott: Well, my "script" is an example too, as much as the question’s information is an example itself - after all I did say "you could start with the following", and I suppose the script does look like just the proof-of-concept it indeed is. But I should make that clearer, agreed. Then, should I provide that much more functionality than explicitly requested ? In order to implement those additional features the script might easily become way more complex than it already is. Wouldn’t that risk to be an overkill ? I think I’d rather wait for explicit inquiries, if and when any (cont'd)
– LL3
yesterday
(cont'd) Wrt the “doing away the cut -f2 part”, it was certainly not for gaining points (nor is my entire contribution as a whole, considering that it is in general quite inferior (ie more complicated) to the other solutions), rather it just cost nothing to do it rather than not, considering that the question mentioned it as a likely subsequent processing of the input data. I contribute with a HTH mindset, I can do without an upvote and am happy with just a “nice, thank you”.
– LL3
yesterday
(cont'd) Wrt the “doing away the cut -f2 part”, it was certainly not for gaining points (nor is my entire contribution as a whole, considering that it is in general quite inferior (ie more complicated) to the other solutions), rather it just cost nothing to do it rather than not, considering that the question mentioned it as a likely subsequent processing of the input data. I contribute with a HTH mindset, I can do without an upvote and am happy with just a “nice, thank you”.
– LL3
yesterday
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1414661%2fhow-to-get-the-n-th-line-after-a-grepped-one%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
3
Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?
– Nasir Riley
Mar 16 at 22:36
3
Use awk? But if you're more familiar with Python that would be quicker.
– MZB
Mar 16 at 23:25
@NasirRiley: good point - I edited my question (
four
will be unique)– WoJ
2 days ago