Perl : Get Maxdate from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : Get Maxdate from the file
# 1  
Old 04-07-2010
Perl : Get Maxdate from the file

Hi All,

I have a input file like below :
Code:
-------------------------------------------------
949890;01-4477138;20101208;7003907933;0
1352305;01-32175;20101225;7005373440;0
0771586;03-975357;20091206;7004353176;0
1868963;06-26112;20101001;7004927161;0
1877828;08-300381;20101008;7004591372;0
0158942;08-371741;20100512;7011021111;0
1889407;03-282266;20101114;7025777256;0
1426078;08-53175593;20100204;7004814000;0
1805938;03-274032;20110823;7006722399;0
0993741;02-55239;20090516;7004863481;0
1981600;40-302572;20110301;7031323637;0
---------------------------------------------------

The third field in this file is date in YYYYMMDD format.

I need to get the max date(20110823) from this file and store it in a variable for doing dome calculataions.
How can i get the max date from this file using perl script ??

Thanks in advance
# 2  
Old 04-07-2010
Can be done easliy in awk , i did not work in perl much

An alternative to Perl:
Code:
 
awk -F";" -v max=0 '$3 > max { max = $3 } END {print max}' input_file

# 3  
Old 04-07-2010
using PERL:-

Code:
perl -lanF';' -e 'BEGIN{$max=0}{$max = $F[2] if $F[2]>$max}END{print $max}' infile.txt

SmilieSmilieSmilie
# 4  
Old 04-07-2010
Hi ,

If i am executing this command from my unix prompt it is working fine and giving me the correct result. But it is not working inside my perl script . it is giving error.

Code:
#!/bin/perl

use strict;
use pcmif;

my $max;

$max = perl -lanF';' -e 'BEGIN{$max=0}{$max = $F[2] if $F[2]>$max}END{print $max}' internal_baseline_data.txt;

----------------

error : Bad name after lanF' at ./test.pl line 8.


Can you please tell me what went worng here...


Thanks in advance
# 5  
Old 04-07-2010
Quote:
Originally Posted by subin_bala
Hi ,

If i am executing this command from my unix prompt it is working fine and giving me the correct result. But it is not working inside my perl script . it is giving error.

#!/bin/perl

use strict;
use pcmif;

my $max;

$max = perl -lanF';' -e 'BEGIN{$max=0}{$max = $F[2] if $F[2]>$max}END{print $max}' internal_baseline_data.txt;

----------------

error : Bad name after lanF' at ./test.pl line 8.


Can you please tell me what went worng here...


Thanks in advance
Code:
$max = `perl -lanF';' -e 'BEGIN{$max=0}{$max = $F[2] if $F[2]>$max}END{print $max}' internal_baseline_data.txt; `

# 6  
Old 04-07-2010
Code:
cut -d';' -f3 inputfile | sort | tail -n1

Works perfectly by me with your sample.
To get it in a variable
Code:
VAR=$(cut -d';' -f3 inputfile | sort | tail -n1)

# 7  
Old 04-07-2010
Hi ,

Still it is not working...


Code:
#!/bin/perl

use strict;
use pcmif;

my $max;

$max = `perl -lanF';' -e 'BEGIN{$max=0}{$max = $F[2] if $F[2]>$max}END{print $max}' internal_baseline_data.txt;`

print "max date is : $max";


getting error like this :

Global symbol "@F" requires explicit package name at ./test.pl line 8.
Global symbol "@F" requires explicit package name at ./test.pl line 8.
syntax error at ./test.pl line 10, near "print"
Execution of ./test.pl aborted due to compilation errors.
----------

sorry for the trouble again ...

Thanks in advance ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. UNIX for Advanced & Expert Users

Convert CSV file to nested XML file using UNIX/PERL?

we have a CSV which i need to convert to XML using Perl or Unix shell scripting. I was able to build this XML in oracle database. However, SQL/XML query is running for long time. Hence, I'm considering to write a Perl or shell script to generate this XML file. Basically need to build this XML... (3 Replies)
Discussion started by: laknar
3 Replies

4. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

5. Programming

Perl- How to catch the file in perl code?

Hi, plz see the below code , i have catch the file "Orders.20110714.out "file as a Orders*.out. but it giving me an error .it does not open the file. if the same thing i have done by code code-> ls Orders*.out then it gives me the output Orders.20110714.out i am trying apply the... (1 Reply)
Discussion started by: pspriyanka
1 Replies

6. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

7. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

8. Shell Programming and Scripting

Perl : Process a file in perl

I have a file with following data : Qspace: COLOR: Queue: doColor Qspace: COLOR: Queue order: fifo Qspace: COLOR: Out of order: none Qspace: COLOR: Retries: 5 Qspace: COLOR: Queue: setColor Qspace: COLOR: Queue order: fifo Qspace: COLOR: Out of order: none Qspace: COLOR: Retries: 5... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

9. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

10. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies
Login or Register to Ask a Question