[Help] PERL Script - grep multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Help] PERL Script - grep multiple lines
# 1  
Old 12-12-2008
[Help] PERL Script - grep multiple lines

Hi Gurus,

I need some help with the "grep" command or whatever command that you think suitable for me. I'm about to write a perl script to extract a report from the system and submit it to the end users. The input for the script will consist of 3 element.

1) Generation ID
2) Month
3) Year

I have a text file that contain the following data :


*Its actually quite a long data (from 2001 to 2008) but the format are the same Smilie*

I need to get those 3 element for the input in my perl script. The main element is Generation <ID#>. Let say from the sample data above, i want the data with the date of Jan 2001 to let say Dec 2001, in order to get the Generation ID#, i need to search for all the JAN to DEC 2001's Generation ID#.

So the question is, how can i do that ? Smilie

Thanks,

Last edited by miskin; 06-16-2009 at 10:23 AM..
# 2  
Old 12-12-2008
Code:
#!/usr/bin/perl -w
my @data;
while (<>) { 
  push @data, $1 if /^\s*Generation (\d+)/;
  push @data, ($1, $2) if /imported at \w+ (\w+) .* (\d{4})\s*$/;
}

Now your data exists in the @data array. Every 3rd element is a new record. To extract, you can use, well, lots of things:
Code:
while ($#data >= 0) { 
   printf "%s\t%s\t%s\n", splice(@data,0,3);
}

There's about n + 1 other ways of doing such a thing.

Last edited by otheus; 12-12-2008 at 02:16 PM.. Reason: typo; code error
# 3  
Old 12-12-2008
If you really want to output this into another perl script, rather than just doing it all in perl, you can do this:
[code]
Code:
#!/usr/bin/perl -w
while (<>) { 
  print $1 
     if /^\s*Generation (\d+)/;
  print " ",$1, " ",$2,"\n"
     if /imported at \w+ (\w+) .* (\d{4})\s*$/;
}

Then you just pipe that script's output to the other perl script.
# 4  
Old 12-12-2008
Wow... fast respond, thanks.. hope i can be more clear of my request Smilie

Well.. i have a data file contain the data as per my first post, the file called data.txt.. the script will execute and ask an input to search through data.txt.. example input required by the script.

1) From Month :
2) From Year :
3) To Month :
4) To Year :

and let say the input will be like this

1) From Month : Jan
2) From Year : 2001
3) To Month : Dec
4) To Year : 2007

so from the input, the script will then search for the Generation ID# for the period Jan 2001 to Dec 2007. Once get the Generation ID# and dump the Generation ID# to another plain text file (genid.txt), the script then run the remaining command to complete the process.

Firstly, i must say that i'm still a newbie in this area .. and so sorry about that and so sorry if i trouble you guys Smilie Hope you guys can help me out.

Thanks in advance. Smilie
# 5  
Old 12-12-2008
Sounds pretty simple. But I'm not sure if there should be MULTIPLE generation IDs or not. Let's assume so. So let's say you pass the variables on the command line to your new script:
Code:
$ your-script.pl $from_month $from_year $to_month $to_year

So now you want a script to read these four arguments, run through the named file, and save it to another file, right? So:
Code:
$ your-script.pl $from_month $from_year $to_month $to_year <data.txt >genid.txt

Now comes the script:
Code:
#!/usr/bin/perl -w

# Create lookup table for Month specification so we can 
# determine which month-name is before/after another month name
%Month = ( Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6, Jul => 7, 
 Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12 );

# Get arguments from command line
($from_month, $from_year, $to_month, $to_year) = @ARGV;

# loop through each input line
while (<>) { 
  # "remember" gen_id for next line of input. 
  $gen_id = $1
     if /^\s*Generation (\d+)/;

  # if the shoe fits...
  if (/imported at \w+ (\w+) .* (\d{4})\s*$/ 
    && $2 >= $from_year && $2 <= to_year
    && $Month{$1} >= $Month{$from_month}
    && $Month{$1} <= $Month{$to_month}
  ) { 
     print $gen_id,"\n";
  }
}

# 6  
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;}
	}
}

# 7  
Old 12-15-2008
SummerCherry,

I haven't tried your script, but I imagine that the line that loads the id ($id=$arr[1];) will load on every line, when in fact, it should only load on lines marked "Generation".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question