Capturing data between patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing data between patterns
# 1  
Old 04-23-2010
Capturing data between patterns

Hii, Friends,
I want your help in one of my problem.
My problem is as follows.

I have a flat file as follows (Just a sample)

MHT011
01(DOT)8750707asdfas8609
03(DOT)ASD3453ASD
09(DOT)876JHT87
11(DOT)sfd324ert

TTT077
01(MOB)876786klj897
06(MOB)876JHT87
07(MOB)sfd324ert
08(MOB)876JHT87
09(MOB)654rrr


ILD321
01(MKU)ASD3453ASD
01(MKU) 45HRT53
01(MKU)DFDF56723GSDF


Here I want to capture lines between MHT011 and TTT077 and want to send that data to file MHT011.rprt.
Similarly I want to capture lines between TTT077 and ILD321 and want to send that data to file TTT077.rprt

One important point to be considered is number of lines between MHT011 and TTT077 are not fixed, it can be only 1 or it can be 100.

Can you please help me resolving this problem?

Thank you in advance
Anushree.
# 2  
Old 04-23-2010
anushree.a,

Itīs not too automated, but like a first approach, yo can try this:


Code:
echo "MHT011
01(DOT)8750707asdfas8609
03(DOT)ASD3453ASD
09(DOT)876JHT87
11(DOT)sfd324ert

TTT077
01(MOB)876786klj897
06(MOB)876JHT87
07(MOB)sfd324ert
08(MOB)876JHT87
09(MOB)654rrr" | awk '/MHT011/,/TTT077/'| sed '$d' > MHT011.rprt

# 3  
Old 04-23-2010
Dear Cgkmal,
I tried your suggestion.
However I got following error

syntax error The source line is 1.
The error context is
>>> /MHT011/,/TTT077 <<<
awk: Quitting



Please help.
# 4  
Old 04-23-2010
Let see,

I think was a typo from me in "
/TTT077/'|" a missing space.

Code:
1-) awk '/MHT011/,/TTT077/' (search between 2 patterns)



but to avoid the unneeded last line to appear is required

Code:
2-) sed '$d' (deletes last line)

Thus, taken data from file the script would be:

Code:
awk '/MHT011/,/TTT077/' inputfile | sed '$d' > MHT011.rprt
awk '/TTT077/,/ILD321/' inputfile | sed '$d' > TTT077.rprt
.
.
.
so on...

Hope it helpsSmilie
# 5  
Old 04-23-2010
Wowww... its like a magic...
It worked...
Thank you sooo much...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

Perl : not capturing all the data from excel sheet

Hi folks, I am working on assignment that captures all the records(2 columns one column contains names and other contain date of birth) from excel sheet stored in a directory and checks for current date and month. If it matches current date and month then the matched records are printed as... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

capturing selective data from a vcd file

Hi, This is a vcd file.A vcd file may have 'n' modules. 1) I need to capture the data in bold,i.e. the module names (shown in bold) 2) Also i need to capture the data inside each individual module,say for tst_bench_top ,i need to capture data from line 4 to line 20 ... I just want one... (2 Replies)
Discussion started by: veerabahu
2 Replies

5. Red Hat

capturing data of nethogs

I have installed nethogs to see which process is sending load on the network. i would like to know how can i capture its data in a log file. Or is there is a unix command like script or tee which can help me to capture the data using cronjob (0 Replies)
Discussion started by: anshus1
0 Replies

6. IP Networking

How to capturing packet data so i can see the Version, IHL, TOS, etc?

Helo Folks :) i had read an answer in this thread unixcom/programming/117551-calculate-ip-header-checksum-manually.html i need to know what the software name that was used to capturing packet data in a network, just like the packet's capture output that showed up in the thread. Thank you :)... (5 Replies)
Discussion started by: polutan
5 Replies

7. Shell Programming and Scripting

Capturing Particular Data

Hi everyone! Can any one help me out regarding capturing relevant data from a file. For e.g, if i want to capture the comment written after "Prompt:" for a particular date, like for this case what would be the command to capture the tag written after "Prompt:" for Date: 2009-03-20. A... (7 Replies)
Discussion started by: muhmsida
7 Replies

8. Shell Programming and Scripting

Extarcting data from a file that matches two different patterns

Say I have data like shown below. I want all the data that contains XXXX and where it finds XXXX then I also want the Timestamp and the Error fields. I have tried egrep "XXXX|Timestamp" > test.txt. That pulls out the data correctly but gives me two records for each message and it also pulls the... (4 Replies)
Discussion started by: gugs
4 Replies

9. Shell Programming and Scripting

Capturing Unix Traceroute data

Hi guys, I need a way to capture the host on the next-to-last hop in a traceroute output. The last output is the destination but I need to capture the router just before the last hop. I can do this in perl but I'm not so sure about Shell... I'm on AIX 5.3 using ksh any ideas? ... (3 Replies)
Discussion started by: TivoliGUY
3 Replies

10. UNIX for Dummies Questions & Answers

Capturing some data from a file into a variable

I have a file with some values in a tab delimted format Eg: 'test' contains: a<tab>b<tab>c<tab>Trk_12345678 now i need to capture this value 'Trk_12345678' into a variable say 'x' and append that value of 12345678 to 12345679 and store is back to a new 'test1' file as : 'test1'... (11 Replies)
Discussion started by: shiroh_1982
11 Replies
Login or Register to Ask a Question