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
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#.
Wow... fast respond, thanks.. hope i can be more clear of my request
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 Hope you guys can help me out.
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:
#!/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";
}
}
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".