UNIX for Beginners Questions & Answers

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 Display Modes
Old 12-28-2017
newbie_01 newbie_01 is offline
Registered User
Join Date: May 2009
Last Activity: 22 May 2018, 8:32 PM EDT
Posts: 236
Thanks: 45
Thanked 1 Time in 1 Post
Advise on how to print range of lines above and below a number?


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.
Attached Files
File Type: txt testfile.txt (334.4 KB, 73 views)
Sponsored Links
Old 12-28-2017
rbatte1 rbatte1 is offline Forum Staff  
Root armed
Join Date: Jun 2007
Last Activity: 17 July 2018, 11:37 AM EDT
Location: Lancashire, UK
Posts: 3,586
Thanks: 1,594
Thanked 706 Times in 633 Posts
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,
Sponsored Links
Old 12-29-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 18 July 2018, 7:28 AM EDT
Location: San Jose, CA, USA
Posts: 11,407
Thanks: 649
Thanked 3,970 Times in 3,393 Posts
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.
Sponsored Links
👤 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
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
print range of lines matching pattern and previous line siriche Shell Programming and Scripting 1 02-09-2012 08:00 PM

All times are GMT -4. The time now is 08:30 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
Show Password

Not a Forum Member?
Forgot Password?