The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
select a portion of a file into a CSV anju Shell Programming and Scripting 8 02-28-2008 01:50 AM
Need Help (Select query) topgear1000cc Shell Programming and Scripting 5 02-12-2008 07:22 AM
How to store the data retrived by a select query into variables? jisha Shell Programming and Scripting 12 01-17-2008 11:45 PM
awk program to select a portion of a line anju Shell Programming and Scripting 3 01-11-2008 06:33 AM
Displaying the data from the select query in a particular format sachin.tendulka Shell Programming and Scripting 15 12-11-2007 08:44 AM

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 10-12-2006
vanand420 vanand420 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 60
Select a portion of file based on query

Hi friends
I am having a small problem and ur help is needed...
I have a long file from which i want to select only some portions after filtering (grep).
My file looks like :

header
xxyy
lmno
xxyy
wxyz
footer

header
abcd
xy
pqrs
footer
.
.

I want to select the block of lines between header and footer based on search given suppose I give 'xy' then the complete block from header to footer where xy exist should be selected..
I was using command : awk '/header/,/footer/' But confused where to insert the search criteria in this command
Please help me
Thanks in advance...
  #2 (permalink)  
Old 10-12-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
Code:
#!/usr/bin/perl -w

$file="test.txt";

print "Enter search string : ";
$str=<STDIN>;
chomp $str;

open(FH,$file) || die "Unable to open $file : $!\n";

my $line;
my $strt_str=0;
my @arr=();
my $fnd = 0;

while ( $line = <FH> )
{
        chomp $line;

        if( $line =~ /^header/  ) {
                print join("\n",@arr)."\n" if $fnd == 1;
                @arr = ();
                $strt_str = 1;
                $fnd = 0;
        }

        push(@arr,$line) if( $strt_str == 1 );

        $fnd = 1 if ( $line =~ /$str/ );

        $strt_str = 0 if ( $line =~ /^footer/ );
}

print  join("\n",@arr)."\n";

Last edited by blowtorch; 10-12-2006 at 06:28 AM.. Reason: add code tags
  #3 (permalink)  
Old 10-12-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
mahendramahendr, please use code tags whenever you post code. It makes things so much easier to read.
  #4 (permalink)  
Old 10-12-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
oh yaa... it looks good and more readable..

though I always use tabs in coding, they are not visible in html... thanks again... will use code.
  #5 (permalink)  
Old 10-13-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by vanand420
Hi friends
I am having a small problem and ur help is needed...
I have a long file from which i want to select only some portions after filtering (grep).
My file looks like :

header
xxyy
lmno
xxyy
wxyz
footer

header
abcd
xy
pqrs
footer
.
.

I want to select the block of lines between header and footer based on search given suppose I give 'xy' then the complete block from header to footer where xy exist should be selected..
I was using command : awk '/header/,/footer/' But confused where to insert the search criteria in this command
Please help me
Thanks in advance...
Code:
awk ' BEGIN { RS = "" ; FS = "\n" }
{ flag=0;
for( i = 1 ; i <= NF; ++i )
{
if(match ( $i , "^xy$" ))
        {  flag=1;break;  }
}
if( flag == 1 ) print
} ' file

Last edited by anbu23; 10-13-2006 at 02:04 PM..
  #6 (permalink)  
Old 10-17-2006
vanand420 vanand420 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 60
Quote:
Originally Posted by anbu23
Code:
awk ' BEGIN { RS = "" ; FS = "\n" }
{ flag=0;
for( i = 1 ; i <= NF; ++i )
{
if(match ( $i , "^xy$" ))
        {  flag=1;break;  }
}
if( flag == 1 ) print
} ' file


Thanks for the reply friend
But where to specify my fixed header and fixed footer in the script
In the file there are several such header and footer...as in log files...
How to select one or more such block based on search query...
  #7 (permalink)  
Old 10-17-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
Sample input:
Code:
header
xxyy
lmno
xxyy
wxyz
footer

header
abcd
pqrs
footer

header
fklsdjsdfalgj
xy
sd;lfks;lkfsd
footer

Python alternative:

Code:
import re
data = open("input.txt").read()
pattern = re.compile(r"header(.*?)footer",re.M|re.DOTALL)
for i in pattern.findall(data):
 	if i.find("xy") != -1:
 		print i
output:
Code:
xxyy
lmno
xxyy
wxyz


fklsdjsdfalgj
xy
sd;lfks;lkfsd
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 08:48 AM.


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