0

The problem

The output of grep -A 4 davidson file.txt gives me:

davidson 0 27  |r|= 0.00244  e= [-0.04073962  0.42777949  0.44157777  0.45997235  0.45997235  0.51613738 
  0.55005554  0.5538866   0.5538866   0.57108874  0.63182267  0.68741162
  0.68741186  0.69608     0.78013465  0.78013465  0.78042162  0.78688599
  0.78688599  0.79381315  0.79381317  0.80049466  0.80049466  0.92062423
  0.93453524  1.01356402  1.01942272]  max|de|= 1.02  lindep= 0.809
--
davidson 1 54  |r|= 3.26  e= [-0.04073964  0.42777928  0.44157749  0.45997213  0.45997213  0.51613724
  0.55005542  0.55388652  0.55388652  0.57108822  0.63182247  0.68741026
  0.68741031  0.69607963  0.78013252  0.78013252  0.78042159  0.78688546
  0.78688546  0.793812    0.793812    0.80049354  0.80049354  0.92062408
  0.93453361  0.97904591  1.01356325  1.01945088]  max|de|= -0.0404  lindep= 0.694
--
davidson 2 66  |r|= 0.523  e= [-0.04073964  0.02576398  0.42777928  0.44157749  0.45997212  0.45997212
  0.51613724  0.55005542  0.55388652  0.55388652  0.57108822  0.63182247
  0.68741006  0.68741006  0.69607962  0.78013218  0.78013218  0.78042159
  0.78688546  0.78688546  0.79381179  0.79381179  0.80049292  0.80049292
  0.92062408  0.93453351  1.01356275  1.01942294]  max|de|= -0.953  lindep= 0.278

What if for each of the above three groups of output, I only want the first and last line? Specifically, rather than getting lines 1-5 for each instance of the string davidson, I want to get only lines 1 and 5.

Prior research I've attempted

I have searched:

"grep -A" but with some lines removed

using Google and the search bar here in unix.se, and didn't get many results (especially the latter search, which only gave me this). These were some of the results I found from the Google search, and reasons why they don't seem to solve my problem:

When typing this question, the following question was suggested to me:

But neither of these solve my problem either.

4
  • 1
    edit your question to include concise, testable sample input (contents of file.txt rather than the output of running grep -A on that file) and expected output so we can help you. The right solution won't involve grep so just seeing the grep output isn't adequate to figure out what that solution might be.
    – Ed Morton
    Feb 21, 2023 at 10:41
  • 1
    Your original file appears to be structured in some way. It would be interesting to see more of it to give you a way of working with that original file directly instead of with a pre-processed version of it. If you can additionally let us know what produced the data, someone may know of a parser for that specific format.
    – Kusalananda
    Feb 22, 2023 at 10:27
  • 1
    (cont.) Also, the first and last line of each group in the output seems to contain parts of an array. If you let us know exactly what you want to get (like the |r| value and the max|de| value or something), it may be easier for us to give you an answer that does not require further post-processing steps on your end.
    – Kusalananda
    Feb 22, 2023 at 10:28
  • @Kusalananda please give me some time to add those details okay? Feb 26, 2023 at 0:23

1 Answer 1

3

Unfortunately you didn't give us a sample of the original data, but playing with your output above this may do what you want?

Rather than grep -A4 ... try this:

awk '/davidson/{h=NR;line=$0} NR==h+4{print line;print $0}' file.txt
2
  • 1
    +1 but could be shortened a bit to awk '/davidson/{h=NR; print}; NR==h+4 && h'. The && h avoids printing the 4th input line when NR=4 and h hasn't been set yet (h won't be true until it is non-zero, which it won't be until an input line matches /davidson/).
    – cas
    Feb 21, 2023 at 12:47
  • 1
    '/davidson/{h=NR+4; print} NR==h' is a bit more concise and avoids printing line number 4 if/when davidson doesn't occur in the first 4 lines of input.
    – Ed Morton
    Feb 21, 2023 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .