Home Man
Search
Today's Posts
Register

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

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

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 12-28-2017
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:

Code:
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.

Code:
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.
# 2  
Old 12-28-2017
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
# 3  
Old 12-29-2017
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.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to print out lines that do not fall between range in file cmccabe Shell Programming and Scripting 6 06-02-2017 08:27 AM
Check/print missing number in a consecutive range and remove duplicate numbers newbie_01 Shell Programming and Scripting 3 03-30-2017 02:20 PM
How to print N number of lines before and after the grep? Huvan Shell Programming and Scripting 0 06-18-2015 11:08 AM
Grep range of lines to print a line number on match Kevin Tivoli Shell Programming and Scripting 15 04-10-2013 09:16 PM
Sed print range of lines between line number and pattern RMN Shell Programming and Scripting 4 10-20-2012 03:54 PM
How to print lines that only have number lower than... narachaid Shell Programming and Scripting 3 09-02-2012 03:09 PM
print range of lines matching pattern and previous line siriche Shell Programming and Scripting 1 02-09-2012 08:00 PM
print lines between line number senthil_is Shell Programming and Scripting 3 08-10-2011 07:15 AM
Why? I can not change the number of lines to print Edgar Guevara SCO 2 07-30-2010 04:56 PM
How to print number of lines with awk ? maheshsri Shell Programming and Scripting 1 11-18-2005 01:19 AM


All times are GMT -4. The time now is 03:24 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password