Operating System - HP-UX
1752800 Members
6036 Online
108789 Solutions
New Discussion юеВ

Re: how to extract a paragraph from a file in shell scripting

 
SOLVED
Go to solution
Mannoj
Occasional Advisor

how to extract a paragraph from a file in shell scripting

hI gURUS,

Am stuck up in a situation wherein a file containing the following strings,,

filename - err.log
-------------------
Dec-31-2008
ASM_DISKGROUP_02
Jan-01-2009
ARCH shutting down
Jan-11-2009
SUCCESS: diskgroup ASM_DISKGROUP_02
Jan-31-2009
Alter database dismounted
Jan-31-2009
ARC0: Archival stopped
Feb-01-2009
ASM_DISKGROUP_02
------------------------------------------
I want to extract all lines from first occurence of Jan to Last occurence of Jan.

DESIRED OUTPUT is -
---------------------------------
Jan-01-2009
ARCH shutting down
Jan-11-2009
SUCCESS: diskgroup ASM_DISKGROUP_02
Jan-31-2009
Alter database dismounted
Jan-31-2009
ARC0: Archival stopped
4 REPLIES 4
Hein van den Heuvel
Honored Contributor
Solution

Re: how to extract a paragraph from a file in shell scripting

AWK, and other tools, offer a RANGE style condition: X,Y
From the time condition X becomes tru, until Y becomes true.

For your taks that could look like as simple as:

# awk '/^Jan/,/^Feb/' tmp.txt

Now you may have to muck with the details like what to do with the first and last lines, or whether to worry about prior year records, or whether there is garantueed to be a start and 'next' record. So you may need:

# awk '/^Jan.*09/,/^Feb.*09/{print prior; prior=$0}' tmp.txt

If you know that each line with date is followed by a single additional line, then I might prefer:

# awk '/^Jan.*09/{ print; getline; print}' tmp.txt

But I suspect this meets your requirements best:

awk '/-2009$/{ ok = ($1 ~ /^Jan/)? 1: 0} ok' tmp.txt

So we look for a line ending in a year.
Set a flag (ok) to true to start printing if the first field in that line started with 'Jan', and false otherwise (Feb or Mar or..), to stop printing.
Then in a whole new program paragraph we test variable ok, and... say nothing else.
This defaults to 'print current line if ok is true'

Enjoy,
Hein.
James R. Ferguson
Acclaimed Contributor

Re: how to extract a paragraph from a file in shell scripting

Hi:

If we assume that there is only one line following each "date" line, then this will work fpr you:

# perl -ne 'print if /^Jan-/..!/^Jan-/' file

Regards!

...JRF...
Mannoj
Occasional Advisor

Re: how to extract a paragraph from a file in shell scripting

Hey Hein your solution

awk '/^Jan/,/^Feb/' tmp.txt

suited me exactly,

Hats off to u,,,,
Dennis Handly
Acclaimed Contributor

Re: how to extract a paragraph from a file in shell scripting

>Hein: we test variable ok ... This defaults to 'print current line if ok is true'

Instead of having to explain this confusing code, it may be better to be explicit:
ok { print $0 }