The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to sort paragraphs by date within a file nabmufti Shell Programming and Scripting 1 02-13-2008 05:33 PM
how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!! nabmufti Shell Programming and Scripting 6 02-09-2008 08:32 PM
filter the string from a file ?? varungupta Shell Programming and Scripting 11 09-17-2007 11:11 PM
File filter Dastard Shell Programming and Scripting 3 09-06-2007 02:50 PM
filter out certain column from a file CamTu Shell Programming and Scripting 4 04-04-2005 07:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-07-2006
cnlhap cnlhap is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 1
Cool how to filter out some paragraphs in a file

Hi,

I am trying to filter out those paragraphs that contains 'CONNECT', 'alter system switch logfile'. That means say the input file is :

-------------------------------------------------------
Wed Jun 7 00:32:31 2006
ACTION : 'CONNECT'
CLIENT USER: prdadm
CLIENT TERMINAL:

Wed Jun 7 00:32:31 2006
ACTION : 'alter system switch logfile'
CLIENT USER: prdadm
CLIENT TERMINAL:


Wed Jun 7 00:32:31 2006
ACTION : 'CONNECT'
CLIENT USER: prdadm
CLIENT TERMINAL:

Wed Jun 7 00:32:42 2006
ACTION : 'ALTER DATABASE CLOSE NORMAL'
CLIENT USER: prdadm
CLIENT TERMINAL:
----------------------------------------------------------------

I would like the output file to be :

----------------------------------------------------------------
Wed Jun 7 00:32:42 2006
ACTION : 'ALTER DATABASE CLOSE NORMAL'
CLIENT USER: prdadm
CLIENT TERMINAL:
----------------------------------------------------------------

Do you have any script to perform this, thank you very much .
  #2 (permalink)  
Old 06-08-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,960
I'm assuming the horizontal lines aren't actually in the file, and are just used instead of
Code:
code tags
Code tags are better because they preserve spacing.

Here's a script:
Code:
#!/bin/sh

SHOULDPRINT=1

function do_flush
{
        if [[ ${SHOULDPRINT} -eq 1 && ! -z ${OUT} ]]
        then
                echo "${OUT}"
        fi
}

while read LINE
do
        if [[ -z ${LINE} ]]
        then
                do_flush
                OUT=""
                SHOULDPRINT=1
        elif    echo "${LINE}" | egrep -q "CONNECT|alter system switch logfile"
        then
                SHOULDPRINT=0
        else
                if [[ -z ${OUT} ]]
                then
                        OUT="
${LINE}"
                else
                        OUT="${OUT}
${LINE}"
                fi
        fi
done
do_flush
  #3 (permalink)  
Old 06-08-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
An awk example of the same thing:
Code:
 nawk '
    $0 !~ /ACTION : / {
        last_line=$0
    }

    $0 ~ /^ACTION : / && $0 !~ /CONNECT|alter system/ {
        print last_line
        print $0
        getline ; print
        getline ; print
    }
' your_log_file
  #4 (permalink)  
Old 06-08-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
nawk -f cn.awk your_log_file

cn.awk:
Code:
BEGIN {
  RS=FS=""
  PATexclude="(CONNECT)|(alter system switch logfile)"
}
$0 !~ PATexclude
  #5 (permalink)  
Old 06-08-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Cool

Quote:
Originally Posted by vgersh99
nawk -f cn.awk your_log_file

cn.awk:
Code:
BEGIN {
  RS=FS=""
  PATexclude="(CONNECT)|(alter system switch logfile)"
}
$0 !~ PATexclude
Nice! This works as long as the paragraphs have whitespace between them.
  #6 (permalink)  
Old 06-08-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by tmarikle
Nice! This works as long as the paragraphs have whitespace between them.
yes, that was the assumption - 'paragraphs are separated by blank lines'!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0