Advise on how to print range of lines above and below a number?

Hi,

I have attached an output file which is some kind of database file mapping. It is basically like an allocation mapping of a tablespace and its datafile/s.

The output is generated by the SQL script that I found from 401 Authorization Required

Excerpts of the file are as below:

Tablespace        File    Block NbBlocks Segment
---------------- ----- -------- -------- ---------------------------------------------------------------------
ABC_XYZXYZ_TXT      18        1      127
                            128      128 TEST.ABCD_MARTIAN_IDX
                            256      128 TEST.ABCD_MARTIAN_IDX
                            384      128
                            512     1024
                           1536      128
                           1664      128
...
...
...
                        3555712    16512
                        3572224     1024
                        3573248    24576
                        3597824     1024
                        3598848   149120

                    43        1      127
                            128      128 TEST.HEY_ARNOLD_FANS
                            256      128 TEST.ABC_BATMAN_ROBIN_N1
                            384      128
                            512        8
                            520        8
                            528        8
...
...
...

I have modified and rename the content but just mainly the segment name, I have put some segment name as an example.

In this example, the tablespace name is ABC_XYZXYZ_TXT and it is composed of two datafiles, file 18 and file 14. I am guessing Block 1 is the file header of each datafile hence the segment column is empty for where it is Block 1.

I'll try my best to explain what I am wanting to do. Before I go write a long script for it, I want to know first if maybe I can 'simply' use awk inside the script to search and print a range of lines above and below a given number.

For example, if I do x.ksh 3000, the script should print out something like below based on what's on testfile.txt.

ABC_XYZXYZ_TXT      18        1      127
                           2432      128
                           2560      128
                           2688      128
                           2816      128
                           2944      128
                           3072      128
                           3200      128
                           3328      128
                           3456      128
                           3584      128
                           3712      128
                    43        1      127
                           2560      128
                           2688      128
                           2816      128
                           2944      128
                           3072      128
                           3200      128
                           3328      128
                           3456      128
                           3584      128
                           3712      128

At the moment, I am thinking of reading the file one at a time and comparing the third field to the given argument that is 3000. While it seem easy to print the next 5 records > 3000, I can't think of a way of saving the last 5 that are less than 3000?

I am hoping I can get some advice on how to achieve what I am wanting to do.

Any reply much appreciated. Thanks in advance.

Hello newbie_01,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

I'm not too clear what you intention is, but if you can numerically sort the data (or it already is) and pass it through grep, the -A and/or -B flags may do what you want.

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

Kind regards,
Robin

Hi newbie_01,
Until you answer Robin's question about homework, you won't get any further help on this topic. And, with over 200 posts and more than 8 years in this forum, we shouldn't have to repeat the questions that Robin asked. We are happy to help you learn how to use UNIX utilities to do what you need to do, but we are not your unpaid programming staff. Other than needing to use line 2 in your input file to figure out where field boundaries are located, this is can be done with a pretty simple awk script.

So, answer the homework question.

Show us what you've tried to solve this on your own.

Show us where you're stuck.