Sponsored Content
Top Forums Shell Programming and Scripting [Help] PERL Script - grep multiple lines Post 302268072 by summer_cherry on Monday 15th of December 2008 02:05:09 AM
Old 12-15-2008
Code:
#! /usr/bin/perl 
print "From year\n";
$f_year=<>;
print "From month\n";
$f_mon=<>;
print "To year\n";
$t_year=<>;
print "To month\n";
$t_mon=<>;
$f=sprintf("%s %s",$f_year,$f_mon);
$t=sprintf("%s %s",$t_year,$t_mon);
%hash=('Jan',1,'Feb',2,'Mar',3,'Apr',4,'May',5,'Jun',6,'Jul',7,'Aug',8,'Sep',9,'Oct',10,'Nov',11,'Dec',12);
$/="kb.\n";
open FH,"<a.txt";
$com="2001 Feb";
while(<FH>){
		my @tmp=split("\n",$_);
		my @arr1=split(" ",$tmp[0]);
		$id=$arr1[1];
		my @arr2=split(" ",$tmp[1]);
		$day=sprintf("%s %s",$arr2[$#arr2],$arr2[$#arr2-3]);
		print $id,"\n" if (_compare($day,$f)==1 && _compare($day,$t)==-1);
}
sub _compare{
	my($a,$b)=(@_);
	my @arr1=split(" ",$a);
	my @arr2=split(" ",$b);
	if ($arr1[0]>$arr2[0]){return 1;}
	if ($arr1[0]<$arr2[0]){return -1;}
	if($arr1[0]==$arr2[0]){
		if ($hash{$arr1[1]}>$hash{$arr2[1]}){return 1;}
		if ($hash{$arr1[1]}<$hash{$arr2[1]}) {return -1;}
		if ($hash{$arr1[1]}==$hash{$arr2[1]}) {return 0;}
	}
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep on multiple lines

I 'm trying to grep 2 fieldds on 2 differnt lines. Like this: psit > file egrep -e '(NS|ES)' $file. Not working. If this succeeds then run next cmd else exit. Pls Help Gundu (13 Replies)
Discussion started by: gundu
13 Replies

2. Shell Programming and Scripting

grep multiple lines

Hey guys: I've been meaning to post this question for awhile...it is regarding grep. Let's say for example that the following entry is in logxx: Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6 Call Answered - DN:8753101 CLID:5164665761 PI:83 If I do a grep 91030... (27 Replies)
Discussion started by: cdunavent
27 Replies

3. Shell Programming and Scripting

grep multiple lines

Hi. I have this format on a textfile: VG Name /dev/vg00 PV Name /dev/dsk/c16t0d0 PV Name /dev/dsk/c18t0d0 PV Name /dev/dsk/c16t4d0 VG Name /dev/vg01 PV Name ... (6 Replies)
Discussion started by: jOOc
6 Replies

4. Shell Programming and Scripting

shell script: grep multiple lines after pattern match

I have sql file containing lot of queries on different database table. I have to filter specific table queries. Let say i need all queries of test1,test2,test3 along with four lines above it and sql queries can be multi lines or in single line. Input file contains. set INSERT_ID=1; set... (1 Reply)
Discussion started by: mirfan
1 Replies

5. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

6. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

7. UNIX for Dummies Questions & Answers

grep in multiple lines

hi i have kind of below text in a file. I want to get a complete paragraph starting with START and ending with before another START) which has a particular string say XYZ or ABC START XYZ hshjghkjh 45 ljkfd fldjlj d jldf START 3493u ABC 454 4545454 4545454 45454 4545454 START ...... (3 Replies)
Discussion started by: reldb
3 Replies

8. UNIX for Advanced & Expert Users

grep across multiple lines

How do you grep 'select * from table_name' string from a script if the select * and from table_name are on 2 different lines ? like select * from table_name Any help would be greatly appreciated !!! Thanks RDR (4 Replies)
Discussion started by: RDR
4 Replies

9. UNIX for Dummies Questions & Answers

Grep multiple lines

I want to grep multiple lines from a text file. I want to grep all lines containing X,Y and NA in a single command. How do I go about doing that? This is what my text files look like: rs1983866 0.0983 10 100016313 rs1983865 0.5994 X 100016339 rs1983864 0.3272 11 100017453 rs7077266... (2 Replies)
Discussion started by: evelibertine
2 Replies

10. Shell Programming and Scripting

Perl script: matching multiple lines error

Dear Perl users, Could somebody help me how to fix my code so I can get my desired output. Here is the data: Pattern Gabriel halo1 halo2 end Pattern Andreas halo1 halo2 endI want to grep multiple lines between the pattern /Pattern Gabriel / and /end/. Then I will store the output into... (6 Replies)
Discussion started by: askari
6 Replies
All times are GMT -4. The time now is 05:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy