Select a portion of file based on query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select a portion of file based on query
# 1  
Old 10-12-2006
Select a portion of file based on query

Hi friends Smilie
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  
Old 10-12-2006
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 07:28 AM.. Reason: add code tags
# 3  
Old 10-12-2006
mahendramahendr, please use code tags whenever you post code. It makes things so much easier to read.
# 4  
Old 10-12-2006
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  
Old 10-13-2006
Quote:
Originally Posted by vanand420
Hi friends Smilie
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 03:04 PM..
# 6  
Old 10-17-2006
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 Smilie
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  
Old 10-17-2006
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Select last update data based on file name

Hi All, I need to remove all files except the most update data based on date on filename Input data_AIDS_20150312.txt data_AIDS_20150311.txt data_AIDS_20150411.txt data_AIDS_20140312.txt the most updated data is data_AIDS_20150411.txt, so I'll remove other files. My expected output... (3 Replies)
Discussion started by: radius
3 Replies

2. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

3. Shell Programming and Scripting

Rename portion of file based on another file

Hello, I've been searching and reading, but I can't figure out how to solve this problem with my newbie skills. In my directory, I have a list of files (see dirlist.txt attachment) that I need to merge and rename. I have part of the code of the code figured out (see below). However, I... (3 Replies)
Discussion started by: anjulka
3 Replies

4. Shell Programming and Scripting

redirecting oracle sqlplus select query into file

So, I would like to run differen select queries on multiple databases.. I made a script wich I thought to be called something like.. ./script.sh sql_file_name out.log or to enter select statement in a command line.. (aix) and I did created some shell script wich is not working.. it... (6 Replies)
Discussion started by: bongo
6 Replies

5. UNIX for Advanced & Expert Users

need to get a portion of entries in file based on a criteria --- Help please

All, Below is the file, what i need to do is take the text in between the /*-- and --*/ , i mean the jobs. Then i have grep for system name . If the job is there in system 1 i have to print to a file. Basically i want to take all the jobs that are in system1 to another file . because... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

6. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi Yogesh, Lucky that i caught you online. Yeah i read about DBI and the WriteExcel module. But the server is not supporting these modules. It said..."Cannot locate DBI"..."Cannot locate Spreadsheet::WriteExcel" I tried creating a simple text file to get the query output, but the... (1 Reply)
Discussion started by: dolphin123
1 Replies

7. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi , I just found you while surfing for the string 'Redirecting sql select query output from within a shell script to txt file/excel file' Could you find time sending me the code for the above question? It'll be great help for me. I have a perl file that calls the sql file... (1 Reply)
Discussion started by: dolphin123
1 Replies

8. UNIX for Dummies Questions & Answers

Reading from a file and passing the value to a select query

Hi all, Here is my problem. I want to read data from a file and pass the variable to a select query. I tried but it doesn't seem to work. Please advise. Example below. FileName='filekey.txt' while read LINE do var=$LINE print "For File key $var" ${ORACLE_HOME}/bin/sqlplus -s... (1 Reply)
Discussion started by: er_ashu
1 Replies

9. Shell Programming and Scripting

select a portion of a file into a CSV

How will i convert a file <LDATE>10-12-07</LDATE><LTIME>13:47:48.553</LTIME><LTEXT>name:anju;city:blore;ph:123</LTEXT> <LDATE>10-12-07</LDATE><LTIME>13:47:48.553</LTIME><LTEXT>name:anju;city:blore;ph:123</LTEXT>... (8 Replies)
Discussion started by: anju
8 Replies

10. Shell Programming and Scripting

awk program to select a portion of a line

Hi all, I am new to awk programs.I have a file like this vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd vjfavhjlaf<LTEXT>aabcdfffvvbbxbcddjbv</LTEXT>fAFdfdADfd... (3 Replies)
Discussion started by: anju
3 Replies
Login or Register to Ask a Question