![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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... |
|
||||
|
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 |
|
||||
|
Quote:
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.. |
|
||||
|
Quote:
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... |
|
||||
|
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
Code:
xxyy lmno xxyy wxyz fklsdjsdfalgj xy sd;lfks;lkfsd |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|