0

I have this kind of text message published at a high frequency:

pose: 
  - 
    position: 
      x: 0.0
      y: 0.0
      z: 0.0
    orientation: 
      x: 0.0
      y: 0.0
      z: 0.0
      w: 1.0
  - 
    position: 
      x: 0.350104982508
      y: 0.729534356813
      z: -3.10228873344e-06
    orientation: 
      x: -9.84128404362e-07
      y: -1.996909502e-06
      z: -0.447019362477
      w: 0.894524281149
  - 
    position: 
      x: 0.455011
      y: -1.59228
      z: 0.0
    orientation: 
      x: 0.0
      y: 0.0
      z: 0.0
      w: 1.0

I want to pipe it to grep or any other similar tool in order to show the area starting from 12 lines after "pose:" until 20 lines after "pose:". The basic commands for grep only allow to show n lines before or after "pose:", In my case what I want to display is this submessage:

position: 
  x: 0.350104982508
  y: 0.729534356813
  z: -3.10228873344e-06
orientation: 
  x: -9.84128404362e-07
  y: -1.996909502e-06
  z: -0.447019362477
  w: 0.894524281149

Is there any command line tools that can do that?

2
 awk '/^pose:/{s=NR+11} (NR>s && NR<s+10){print $0}'

When line start with pose:, s is set as NR+11. if NR is greater than s and less than s+10 lines are printed.

1

Answer REPLACED:

Basically it's the matter of substraction of '5 lines after' from 'all 20 lines after'

here's my quick suggestion

diff <(grep 'pose:' filename -A 20) <(grep 'pose:' filename -A 5)

Of course diff output will contain some extra headings lines and '<' sign, which you may want to filter out

that's however no big deal to filter out

  • I don't understand how the -B can be applied here, can you please clarify? – Mehdi Mar 4 '16 at 10:14
  • I am sorry for overlooking the point. I'll update the answer soon – Tagwint Mar 4 '16 at 10:27
  • this command is using a text file though, I have a program continuously outputting text and need to pipe its output. – Mehdi Mar 4 '16 at 10:37
0
(grep -A 20 "pose:" file; echo "--")|grep -B 9 '^--'
  • can you pipe that? – Mehdi Mar 4 '16 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.