Grep content between specific lines

cat file1

*FileHeader*                                       Partition 0
Total Data Bytes              1416
  Avg Bytes/Record            1416
Others                           1

PRDX22.AUDIT_DATA_INFO                          Partition 4
Total Data Bytes              4615
  Avg Bytes/Record             100
Delete                          11
Insert                          35
Before Images                   11
After Images                    35

PRDX22.AUDIT_TRAIL                              Partition 4
Total Data Bytes             12317
  Avg Bytes/Record             362
Delete                           4
Insert                          30
Before Images                    4
After Images                    30

PRDX22.CONTACT                                  Partition 4
Total Data Bytes             21252
  Avg Bytes/Record             442
Insert                           1
FieldComp                       47
After Images                    48

Write all headers to a file

grep -n 'PRDX22' >headerfile
cat headerfile
26:PRDX22.AUDIT_DATA_INFO                          Partition 4
34:PRDX22.AUDIT_TRAIL                              Partition 4
42:PRDX22.CONTACT                                  Partition 4

I need to write content of each segment (for eg: PRDX22.AUDIT_DATA_INFO line 26 to 33) to one file.

Note: unix commands without awk/sed.

Thanks,
Veera

You never seem to be willing to show us that you have done any work yourself.

You always seem to be unwilling to use the obvious tools that can easily do the job you want done.

Is this a homework assignment? If not, why are you restricting the commands that can be used?

What OS and shell are you using?

OS : 5.10 Sun solaris.

This is not homework

You always seems to be commenting guy rather than proving ideas.

You aren't a teacher and I am not school Kid.

Veera.

Veera- It's very harsh of you, I should say. We are not supposed to teach here as you have mentioned we are not school kid or teacher here. So if you will not tell us how and what you have tried so far how we will help you. This forum is all about learning, we will help each other to learn and before asking for help we will try our best then only at last point we will ask help/guidance to others. Don Cragun is one of the most experience person/admin here(You can see his/admin members profile and come to know about them), so likewise we have a nice, well knowledge, gentle and always helpful team here which will help us always to learn technology. So in spite of blaming people (Which is not supposed to happen by any one at this platform) you can really try hard to learn and share with us what are the difficulties you are facing and we will try our best to help you.

Hope this will help.

Thanks,
R. Singh

We can't bear this type of behavior here. +1 for ban

below idea I have to go with (without using awk ). I am trying make script short and give exact results.

cat -n header > file1

cat file1

1:15:SCHEMA.TABLE1  
2:25:SCHEMA.TABLE2
3:30:SCHEMA.TABLE3
4:46:SCHEMA.TABLE3

MAX=`$( wc -l < file1 )`

cat file1 |while read line

do

SEQ=`echo $line |cut -d':' -f1`
STR=`echo $line |cut -d':' -f2`
TBL=`echo $line |cut -d':' -f3`

if [ SEQ -eq $MAX ];then

sed '$STR,$n,p' content.txt > $TBL.txt

else

SEQ=SEQ+1

END=`grep -"^$SEQ:" file1 |cut -d':' -f2`

END=`expr $END - 1` 

sed '$STR,$END,p' content.txt > $TBL.txt

fi


done

I asked what OS and shell you're using. You have now given us the OS. Are you restricting yourself to only using /bin/sh on Solaris 10? (Your code can be much shorter and more efficient if you're willing to use /bin/ksh , /usr/xpg4/bin/sh , or /usr/xpg6/bin/sh , or /bin/bash instead of /bin/sh .)

I am glad that this is not homework. There are special rules and a special forum where homework assignments are to be submitted. Homework assignments frequently include a small list of utilities that can be used to complete the assignment. So saying that you can't use awk and sed made this look like a homework assignment. Why don't you want to use awk or sed ? You have said you want a short script that will give you exact results. You have shown us about 15 lines of code (using sed which you said we can't use) and you haven't solved the problem yet. (And you made a leap of logic that I can't follow between the file you named headerfile in post #1 and the file you named header in post #6 in this thread???

Why wasn't the line:

*FileHeader*                                       Partition 0

a header in post #1? Where did the SCHEMA.TABLEdigit come from in post #6 and why do you want to the last two segments to the same file ( SCHEMA.TABLE3 ) which will throw away the contents of the 3rd segment?

You are right. I am not a teacher. My father was a teacher. I got my degrees in computer science and computer engineering more than four decades ago, and I am still a student learning new things about my chosen field every day. If you aren't here to learn; you're in the wrong place. If you're here expecting us to serve as your unpaid programming staff; you're in the wrong place.

If you were willing to use awk and I guessed correctly while making a lot a wild assumptions based on your incomplete design requirements, I would suggest you try something like:

#!/bin/sh
/usr/xpg4/bin/awk '
/Partition/ { if(fn++) close(file); file = "SCHEMA.TABLE" fn }
file != "" { print > file }
' file1

which is a total of 5 lines of code and gives exact results. The awk script could be turned into a single line, but I will choose readable code (rather than the minimum number of lines of code) every time. With the data you showed us in file1 in your 1st post, this script produce the four files:
SCHEMA.TABLE1 :

*FileHeader*                                       Partition 0
Total Data Bytes              1416
  Avg Bytes/Record            1416
Others                           1

SCHEMA.TABLE2 :

PRDX22.AUDIT_DATA_INFO                          Partition 4
Total Data Bytes              4615
  Avg Bytes/Record             100
Delete                          11
Insert                          35
Before Images                   11
After Images                    35

SCHEMA.TABLE3 :

PRDX22.AUDIT_TRAIL                              Partition 4
Total Data Bytes             12317
  Avg Bytes/Record             362
Delete                           4
Insert                          30
Before Images                    4
After Images                    30

and SCHEMA.TABLE4 :

PRDX22.CONTACT                                  Partition 4
Total Data Bytes             21252
  Avg Bytes/Record             442
Insert                           1
FieldComp                       47
After Images                    48
2 Likes

I strongly agree that using awk work can be made easier.

  1. shell used: /bin/ksh

  2. only basic functions using awk can be allowed

 eg; awk -F\t '{print $0 }' 

.
(as per coding standards.

  1. sed can be used.

  2. Only lines between segments/tables needs (few initial lines should be avoided) - *FileHeader*

5.why do you want to the last two segments to the same file (its a typo, table names unique).

Hence I was trying to create the script. I shouldn't post actual contents.

In my previous post itself, I tried to explain same and posted things I tried.

Don Cragun replied that its a school work (His assumption).

I never said that your request was school work; I asked if it was school work because the constraints you placed on solutions are highly unusual. (And, even though you later decided that you could use sed , your first post said we were not allowed to use awk or sed .) If I had decided that this was homework, I would have issued an infraction and closed the thread (as I did for two other threads yesterday)!

The following should run several orders of magnitude faster than the fragments you showed us before. Not only does it avoid both awk and sed , it does not use anything but ksh built-ins. For small files, this will probably even run faster than using awk :

#!/bin/ksh
fc=0
fn=/dev/null
while IFS='' read -r line
do      if [ "${line#*PRDX22}" != "$line" ]
        then    fc=$((fc + 1))
                fn=SCHEMA.TABLE$fc
                > $fn
        fi
        printf '%s\n' "$line" >> $fn
done < file1

With the original input you provided in your 1st post, it produces three output files:
SCHEMA.TABLE1 :

PRDX22.AUDIT_DATA_INFO                          Partition 4
Total Data Bytes              4615
  Avg Bytes/Record             100
Delete                          11
Insert                          35
Before Images                   11
After Images                    35

SCHEMA.TABLE2 :

PRDX22.AUDIT_TRAIL                              Partition 4
Total Data Bytes             12317
  Avg Bytes/Record             362
Delete                           4
Insert                          30
Before Images                    4
After Images                    30

and SCHEMA.TABLE3 :

PRDX22.CONTACT                                  Partition 4
Total Data Bytes             21252
  Avg Bytes/Record             442
Insert                           1
FieldComp                       47
After Images                    48

Since you still haven't shown us any sample output, and the data you have shown us in various posts obviously does not come from a single sample input, I have no confidence that this is close to what you want; but it is the best I can do with the data you have provided.