extract block in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract block in file
# 1  
Old 10-24-2001
extract block in file

I need to extract a particular block from a file whose locations are not known but the only identity is a word. For example

in a file I have

ABC

asdklf
asdfk

FGH

dfdfg

asdlfk
asdfl

...

JHK

ppoi
lkjkj


FGH
sdjsd
sdf
sd



-eof-

I have to extract the block whichever begins with FGH like

FGH

dfdfg

asdlfk
asdfl

FGH
sdjsd
sdf
sd

Here the significance is in between two blocks (ex FGH and JHK) i have double spacing.

How can I do this ?
sskb
# 2  
Old 10-25-2001
This is always a fun programming problem. There are three approaches I can think of. I am sure there are many more. I suggest you do what you feel comfortable with or maybe get some ideas from what I suggest:

1. Using awk

I can't even come up with an example for you. You'll have to use a BEGIN an END section, probably with FS="<text you are looking for>". For RS, I am not sure what to use if they are separated by double blank lines. I am sure there is a way to specify this but I don't know what that would be. You may even have to do a sed first to replace some stuff to get awk to work. Eitherway, it probably isn't going to be pretty.

2. Using Perl Smilie

Text parsing is definitely where perl shines. There is a range regular expession you can use that will probably do what you want in less then 5 lines of code. You may even be able to do it in 1. I would check with perlmonks.com for advice on the complete script but the regular experssion would be similar to (from memory, syntax is probably wrong):

/FGH/ .. /\n\n

This will find the block between and including FGH and 2 blank lines.

3. c

You could alway write a c program to pump the data into an array and parse it that way.

#2 would be my choice, but I thought you would like to know more options incase you don't know perl (if you don't, you should check it out, it is pretty sweet)

TioTony
# 3  
Old 10-25-2001
Code:
#! /usr/bin/ksh
read next
printing=0
while this="$next"; read this ; do
     [[ $this = "FGH" ]] && printing=1
     ((printing)) && print - "$this"
     [[ $this = "" && $next = "" ]] && printing=0
done
((printing)) && print - "$next"
exit 0

# 4  
Old 10-25-2001
thanks Tony for your kind and nice information.
Perdarbo,
thanks for your code, but I am sorry I could not know where to specify the input filename in that script.
could you please tell me how to use this.

thanks
sskb
# 5  
Old 10-25-2001
1) Use an editor to create a file and type in the code. I will call it "printblock".

2) Make the script executable with "chmod +x printblock".

3) Run the script and redirect input from your file. "./printblock < datafile". You may want to save the output to a second file. You can do that with "./printblock < datafile > secondfile".

Now I don't want to sound unkind, but if you're struggling with concepts like running a script and redirecting input, then you shouldn't be trying to write complex scripts yet. You need to walk before you run. I strongly recommend that you spend some time on unix basics via a book or class.
# 6  
Old 10-25-2001
thanks

Thanks Perderabo for your kind advice and help.
That code is working fine. I had totally forgotten the redirection concept in the whole tension.
I am sorry for having run as I have hardly started walking. I am still going through the unix basics and I know it is never gonna be complete to master UNIX, at least for me. Thanks for your help.
Smilie
sskb
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract XML block when value is matched (Shell script)

Hi everyone, So i'm struggling with an xml (log file) where we get information about some devices, so the logfile is filled with multiple "blocks" like that. Based on the <devId> i want to extract this part of the xml file. If possible I want it to have an script for this, cause we'll use... (5 Replies)
Discussion started by: Pouky
5 Replies

2. Shell Programming and Scripting

How can I extract XML block around matching search string?

I want to extract XML block surrounding search string Ex: print XML block for string "myapp1-ear" surrounded by "<application> .. </application>" Input XML: <?xml version="1.0" encoding="UTF-8"?> <deployment-request> <requestor> <first-name>kchinnam</first-name> ... (16 Replies)
Discussion started by: kchinnam
16 Replies

3. Shell Programming and Scripting

Extract a block of text

Hello all, I am working on a script which should parse a large file called input.txt which contains table definitions, index definitions and comments like these ones: ------------------------------------------------ -- DDL Statements for table "CMWSYS"."CMWD_TEC_SUIVI_TRT"... (12 Replies)
Discussion started by: kiki_riki_miki
12 Replies

4. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

5. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

6. Shell Programming and Scripting

How to extract block from a file?

I have siebel log file as following EventContext ....... 123 ....... SELECT ... .. EventConext <---- Question 1 , I should get this line 345 ...... SELECT <----- Question 2 , print this line Test..... <----- Question 2 , print this line .... <----- Question 2 , print... (5 Replies)
Discussion started by: ran123
5 Replies

7. Shell Programming and Scripting

[Awk] Extract block of with a particular pattern

Hi, I have some CVS log files, which are divided into blocks. Each block has many fields of information and I want to extract those blocks with a pattern. Here is the sample input. RCS file: /cvsroot/eclipse/org.eclipse.debug.core/core/org/eclipse/debug/core/DebugPlugin.java,v head: 1.174... (7 Replies)
Discussion started by: sandeepk1611
7 Replies

8. Shell Programming and Scripting

Extract a block of text??

Hello all, I have a large output file from which I would like to extract a single block of text. An example block of text is shown below: ***** EQUILIBRIUM GEOMETRY LOCATED ***** COORDINATES OF ALL ATOMS ARE (ANGS) ATOM CHARGE X Y Z ... (10 Replies)
Discussion started by: marcozd
10 Replies

9. Shell Programming and Scripting

Extract selective block from XML file

Hi, There's an xml file produced from a front-end tool as shown below: <INPUT DATABASE ="ORACLE" DBNAME ="UNIX" NAME ="FACT_TABLE" OWNERNAME ="DIPS"> <INPUTFIELD DATATYPE ="double" DEFAULTVALUE ="" DESCRIPTION ="" NAME ="STORE_KEY" PICTURETEXT ="" PORTTYPE ="INPUT" PRECISION ="15" SCALE... (6 Replies)
Discussion started by: dips_ag
6 Replies

10. Shell Programming and Scripting

Extract block of data and the error reason too. So so urgent

Hi , this is my first enty in our forum. Problem scenario: Using informatica tool am loding records from source DB to target DB. While loading some records getting rejected due to some reason. Informatica will capture those rejected records in session log file.now the session log ll be... (2 Replies)
Discussion started by: Gopal_Engg
2 Replies
Login or Register to Ask a Question