- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to extract a paragraph from a file in shel...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2009 06:18 AM
тАО01-31-2009 06:18 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2009 06:52 AM
тАО01-31-2009 06:52 AM
SolutionFrom 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2009 06:57 AM
тАО01-31-2009 06:57 AM
Re: how to extract a paragraph from a file in shell scripting
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2009 08:16 AM
тАО01-31-2009 08:16 AM
Re: how to extract a paragraph from a file in shell scripting
awk '/^Jan/,/^Feb/' tmp.txt
suited me exactly,
Hats off to u,,,,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2009 05:41 PM
тАО01-31-2009 05:41 PM
Re: how to extract a paragraph from a file in shell scripting
Instead of having to explain this confusing code, it may be better to be explicit:
ok { print $0 }