Grep content between specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep content between specific lines
# 1  
Old 09-22-2014
Grep content between specific lines

Code:
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
Code:
grep -n 'PRDX22' >headerfile

Code:
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
# 2  
Old 09-22-2014
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?
# 3  
Old 09-22-2014
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.
# 4  
Old 09-22-2014
Quote:
Originally Posted by Veera_V
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

Last edited by RavinderSingh13; 09-22-2014 at 09:49 AM..
# 5  
Old 09-22-2014
We can't bear this type of behavior here. +1 for ban
# 6  
Old 09-22-2014
below idea I have to go with (without using awk ). I am trying make script short and give exact results.

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


Last edited by Veera_V; 09-22-2014 at 04:54 PM..
# 7  
Old 09-22-2014
Quote:
Originally Posted by Veera_V
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.
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:
Code:
*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:
Code:
#!/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:
Code:
*FileHeader*                                       Partition 0
Total Data Bytes              1416
  Avg Bytes/Record            1416
Others                           1

SCHEMA.TABLE2:
Code:
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:
Code:
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:
Code:
PRDX22.CONTACT                                  Partition 4
Total Data Bytes             21252
  Avg Bytes/Record             442
Insert                           1
FieldComp                       47
After Images                    48

These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep specific lines

Hello I have a file with nearly 90000 lines in x,y,z format but have some lines that I do not need to show. Is there anyway to delete those 3 lines after every 288 lines. Eg I keep the first 288 lines delete (289, 290 291); keep the next 288 lines after those and so on... Thanks (6 Replies)
Discussion started by: Madiouma Ndiaye
6 Replies

2. Shell Programming and Scripting

Display specific lines content from the file

Hell, I want to grep certain word from file and display above 2 lines and after two lines. Here is the content of sample file. Mar 14, 2013 12:56:59 AM Agent.Agent SendTo INFO: Connection to server:7041 - Credential Transmit Successesful Mar 14, 2013 8:54:21 AM cgent SendTo WARNING:... (7 Replies)
Discussion started by: balareddy
7 Replies

3. Shell Programming and Scripting

Grep all lines for a specific date in log-files

I need to grep all lines for "yesterday" in /var/log/messages. Dates are in the format "YYYY-MM-DD". (5 Replies)
Discussion started by: Padmanabhan
5 Replies

4. Shell Programming and Scripting

grep the output between specific lines

Symmetrix ID : 00000001234 Host Name : myown Identifiers Found : 5000000000000000 5000000000000001 Device Cap(MB) Attr Dir:P ------ ------- ---- ---- 1234 25886 (M) 8D:1, 9D:1 0123 25886 (M) 8D:1, 9D:1 1345 25886 (M) ... (5 Replies)
Discussion started by: maddy.san
5 Replies

5. Shell Programming and Scripting

Grep only specific lines ordered by column/date

Hi everybody, I'd like to think I've been through the search tool not only on this site, but also on google too, but I haven't been able to find what I was looking for. If I might've missed something on this forum, please slap me in the face with a link that you consider useful for my query :D ... (4 Replies)
Discussion started by: dilibau
4 Replies

6. Shell Programming and Scripting

Problems to print specific lines with awk and grep...HELP!

Hi all I have data like this: model: 1, misfit value: 0.74987 1 1.182 1.735 2.056 1.867 2 0.503 1.843 2.018 1.888 3 2.706 2.952 2.979 1.882 4 8.015 3.414 3.675 1.874 ... (1 Reply)
Discussion started by: fedora2011
1 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. UNIX Desktop Questions & Answers

grep lines with two specific characters somewhere in the line

I'm having trouble with extracting certain lines from a file based on whether they have all the required fields. Original file: snt:594:Sam N This bpt:2342:Bob P That lr:123 wrp:23:Whoever Person cor:794 Desired output: snt:594:Sam N This bpt:2342:Bob P That wrp:23:Whoever Person ... (3 Replies)
Discussion started by: Chthonic
3 Replies

9. Shell Programming and Scripting

retrieving specific lines from a file - can I use grep ?

Hi there, if i had a file that looked like this my_server1 red green blue yellow blue my_server2 blue blue yellow green blue my_server3 yellow (9 Replies)
Discussion started by: hcclnoodles
9 Replies

10. Shell Programming and Scripting

Retreive content between specific lines ina file

Hi I have a text file which has two sets of lines repeating for "n" number of times.Some data is printed between the two lines.I want to retrieve all the data thats there in between those two set of lines.I have the string value of those two set of lines. To be much more clearer ... (4 Replies)
Discussion started by: chennaitomcruis
4 Replies
Login or Register to Ask a Question