0

I have a file that contains information like the following:

header info line 1 
header info line 2
header info line 3
   ....
process: 1
   info 1
   info 2
   info 3

process: 2
   info 1
   info 2
   info 3

process: 3
   info 1
   info 2
   info 3

What I want to do is grep for one of the process lines (e.x "process: 2") then delete the other process while keeping the header information. What I know is the number of lines after the "process: #" (Lets just use 3 for this example). What I don't know is how many process numbers there are. What I was trying was:

 grep "process: 2" -A 3 file.txt

However I lose the header information. I want to keep the header info but get rid of all the other process info. I feel like I can do this with an egrep but I'm not sure how.

My desired output is the following:

header info line 1
header info line 2
header info line 3
   ....
process: 2
   info 1
   info 2
   info 3
fedorqui
  • 275,237
  • 103
  • 548
  • 598
pdm
  • 145
  • 1
  • 2
  • 9

3 Answers3

2

Better use awk for this:

$ awk -v N=3 -v header=4 '/process: 2/{c=N+1} NR<=header || c&&c--;' file
header info line 1 
header info line 2
header info line 3
   ....
process: 2
   info 1
   info 2
   info 3

This uses printing with sed or awk a line following a matching pattern together with a check on the lines on the header.

Explanation

  • -v N=3 -v header=4 provide the amount of lines the header has (header) and how many lines after the match should be printed (N).
  • /process: 2/{c=N+1} when process: 2 line is seen, set the variable c (from counter).
  • c&&c-- evaluate c. If its value is bigger than 0, it evaluates as True, so that the line is printed. Also, decrement the value so that just N lines are printed.
  • NR<=header if the line number is equal or lower than the given value header, it evaluates to True and the line is printed.
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • @pdm see my update with a proper Explanation. It is basically a way to keep track how many lines after the match have been printed so far. – fedorqui Jul 07 '15 at 14:44
  • 1
    Thank you very much for the explanation. This clears up everything. – pdm Jul 07 '15 at 14:45
1

sed's nice too

sed -n '1,4p; /^process: 2$/ {N;N;N;p;q}' file.txt

That will print the first 4 lines, and when we see the desired pattern, read the next 3 lines, print and quit.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

head -1 file; grep something file

Juan Medina
  • 134
  • 1
  • 4