LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-25-2009, 03:20 AM   #1
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Rep: Reputation: 15
Awk script to select range from file


I have a file generated in every event occurred. In this file i have a entries like bellow

<without blank line after Summary>
Summary:
Adapter at hardware path 0/6/1/0 : Received an interrupt indicating that a
primitive was transmitted

and sometimes like bellow

<with blank line after Summary>
Summary:

Adapter at hardware path 0/6/1/0 : Received an interrupt indicating that a
primitive was transmitted


What I need is to extract the Summary from the file. I have the following awk script. But it only works if there is no blank line after Summary.


cat server.event.txt|awk '/^Sum/,/^$/'

But I need up to second blank lines after Summary.

Can anyone correct the script?
 
Old 11-25-2009, 05:27 AM   #2
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
I propose
Code:
sed -n '/^Summary/!p
/^Summary/ {N
s/\n$//
p}' eventfilename |sed -n '/^Summary/,/^$/p'
You don't need awk for that.
The first sed suppresses a blank line after a line beginning with Summary.
The second sed extracts the lines between Summary and the next blank line.

Last edited by berbae; 11-25-2009 at 05:39 AM.
 
Old 11-25-2009, 05:53 AM   #3
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
Output error

#sed -n '/^Summary/!p /^Summary/ {N s/\n$// p}' bldhkilm.event.txt |sed -n '/^Summary/,/^$/p'
sed: Function /^Summary/!p /^Summary/ {N s/\n$// p} cannot be parsed.
 
Old 11-25-2009, 06:02 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681Reputation: 681
Could you post an actual sample containing both cases. It is hard to test an answer without test data. Sometimes a description of the problem isn't enough. I'm not certain whether there is more than one line of textafter "Summary"; whether there is always a blank line before "Summary"; or whether there are blank lines elsewhere. Be sure to post the last summary record. Whether there is a blank line after the last summary could determine whether a "/Summary:/,/^$/" range will match for the last summary.

You shouldn't have joined the lines in the example barbea posted.
Code:
cat summaryexample
Summary:
This is a non blank line right after the Summary heading.
This is a part of the summary.

Summary:

There is a blank line after the summary.
The last line of the file is blank.
Code:
sed -n '/^Summary/!p
        /^Summary/ {N
                    s/\n$//
                    p}' summaryexample |
sed -n '/^Summary/,/^$/p'
Summary:
This is a non blank line right after the Summary heading.
This is a part of the summary.

Summary:
There is a blank line after the summary.
The last line of the file is blank.
It is important to use proper indentation so that the structure of the sed program stands out. That way you can see easily which commands are executed only in a range or subrange.

Last edited by jschiwal; 11-25-2009 at 06:28 AM.
 
Old 11-25-2009, 06:05 AM   #5
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
I have posted it my initial post.
 
Old 11-25-2009, 11:40 AM   #6
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
For the command to work, it should have been launched as written in my post, or in jschiwal's one with a better identation (the line structure has to be kept).
Can you confirm that it solves your problem ?
 
Old 11-25-2009, 09:52 PM   #7
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
Using Jschiwal's code my output is like bellow

$ sed -n '/^Summary/!p
> /^Summary/ {N
> s/\n$//
> p}' bldhkilm.event.txt |
> sed -n '/^Summary/,/^$/p'
sed: Function p} cannot be parsed.

BTW, my OS is HP-UX
 
Old 11-26-2009, 05:21 AM   #8
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
The command works in Linux.
But it seems that in HP-UX each sed command should be on its own line so try :
Code:
sed -n '/^Summary/!p
        /^Summary/ {N
                    s/\n$//
                    p
                    }' bldhkilm.event.txt |
sed -n '/^Summary/,/^$/p'
And Jschiwal's code is the same code I've written in post #2 with a different presentation.
The "sed: Function p} cannot be parsed." should have given you a clue that the HP-UX sed command doesn't like the } just after the command p.
I hope that it will be the good answer now.
 
Old 11-26-2009, 05:46 AM   #9
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
Thanks a lot barbae, finally it works

Very much appreciate for your contribution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need a php script to format and display a select amount of text from a file steve51184 Linux - Software 27 01-27-2009 03:29 PM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 09:17 PM
How to select TCP_Ports Range sachinsharma10 Linux - Networking 4 09-06-2007 06:14 AM
Perl script/mysql select query from a file mcdrr Programming 12 06-05-2007 01:00 AM
command or shell script to print line range from file minil Programming 3 12-28-2005 09:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration