1

I am trying to extract lines containing a certain string also 4 lines before it (just the fourth line not the ones in between). Right now I can get the line itself;

grep -n "my string" myfile.out > output.txt

I can also get the lines before it, but not that specific one, all the four preceding lines;

grep "my string" -B 4 myfile.out > output.txt

Let's take this example:

 ! CREEK                                                              
 FLUX 6739, 6670, 6640, 6672, 6582, 6583, 6548, 6576, 6570 &      
 656565                   
 ***
 ***  THE SUM OF SPECIFIED:   -1.192451    
 ***  THE AVERAGE:  -6.2760599E-02
 ***  THE MAXIMUM:    0.000000    
 ***  THE MINIMUM:  -0.4294980    
 ***  THE SUM OF POSITIVE:    0.000000    
 ***  THE SUM OF NEGATIVE:   -1.192451   

I want this output:

 ! CREEK
 ***  THE SUM OF SPECIFIED:   -1.192451

I would appreciate windows-batch-file solution or PowerShell as well.

M--
  • 25,431
  • 8
  • 61
  • 93

3 Answers3

2

Poweshell solution :

# verbose version 
Select-String "THE SUM OF SPECIFIED" -Path "C:\temp\test.txt" -Context 4,0 | foreach { $_.Context.PreContext[0]; $_.Line}

# or short version (with alias)
sls "THE SUM OF SPECIFIED" "C:\temp\test.txt" -Co 4,0 | % { $_.Context.PreContext[0]; $_.Line}
Esperento57
  • 16,521
  • 3
  • 39
  • 45
1

Here is tac + awk solution:

tac file |
awk '/THE SUM OF SPECIFIED/{p=NR; print} p && NR==p+4{print; p=0}' |
tac

! CREEK
***  THE SUM OF SPECIFIED:   -1.192451

Alternatively here is grep + awk solution:

grep "THE SUM OF SPECIFIED" -B 4 file | awk 'NR==1 || NR ==5'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    I should accept your answer and I will. But a follow up question that I don't wanna post another question for it. What if I want to find the first line before `"The sum of ..."` that start with **`!`**. Cause I realized sometimes it is third line before that. – M-- Jun 08 '17 at 14:53
  • 1
    For that you can do: `tac file | awk '/THE SUM OF SPECIFIED/{p=NR; print} p && /^!/{print; p=0}' | tac` – anubhava Jun 08 '17 at 14:56
0

If your file is named myfile.out this should do what you want to:

grep "THE SUM OF SPECIFIED" -B 4  myfile.out | sed -n "1p;5p"
xiawi
  • 1,772
  • 4
  • 19
  • 21