need to modify code for catching last date from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to modify code for catching last date from file
# 1  
Old 11-02-2011
need to modify code for catching last date from file

Hi All,

I am having one abc.txt file contains below data.


abc.txt contains below data

Code:
jjhj "TransactTime":"2011-09-26 12:09:15"
ggjk "TransactTime":"2011-10-31 12:09:15"
jlajsla "TransactTime":"2011-11-01 12:09:15"

in below code using
Code:
 " $runDate = $1 if $str =~ m/,"TransactTime":"([^"]{10})/; "

i can catch the date 2011-09-26 as a run date but my aim is to catch the last date of file (at the end file date) which is 2011-11-01.

so please help me to modify this code.


Code:
$ARGV[1]||='/home/user01/exercise/abc.txt

Code:
open EX, $ARGV[1];
local $/;
while(<EX>){$str = $_;};
$runDate = $1 if $str =~ m/,"TransactTime":"([^"]{10})/; # get transaction date as run date


Thanks

Last edited by vbe; 11-02-2011 at 10:42 AM..
# 2  
Old 11-02-2011
Hope this helps:

Code:
#!/usr/bin/perl

use strict;

my $runDate;
my $str;
my $file = "file.txt";
my @file;

open(FILE, "<$file") or die "Unable to open file: $!\n";
@file = reverse <FILE>;  # reverse the contents of the file
close(FILE);

$str = shift(@file);  # remove the first element, last line of file
chomp($str);
$runDate = $1 if $str =~ m/.*?"TransactTime":"([^"]{10})/;
print "$runDate\n";

2011-11-01

Last edited by pludi; 11-02-2011 at 11:23 AM..
# 3  
Old 11-02-2011
Hi aish11,

Other solution:
Code:
$ cat abc.txt
jjhj "TransactTime":"2011-09-26 12:09:15"
ggjk "TransactTime":"2011-10-31 12:09:15"
jlajsla "TransactTime":"2011-11-01 12:09:15"
$ cat script.pl
use warnings;
use strict;

@ARGV == 1 or die qq[Usage: perl $0 input-file\n];
                                                                                                                                                                                                                                             
my $run_date;                                                                                                                                                                                                                                
while ( <> ) {                                                                                                                                                                                                                               
        $run_date = m/(?i:transactTime)":"(\S+)/ ? $1 : $run_date;                                                                                                                                                                           
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
printf qq[%s\n], $run_date;                                                                                                                                                                                                                  
$ perl script.pl abc.txt                                                                                                                                                                                                 
2011-11-01

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ed to modify a file --- or not?

Running Oracle Linux 6 (derivative of RHEL 6) Given this snippet of code in a shell script: #-- reset oratab to use 11.2 home for dwdev #-- normally we'd just use sed to do this sort of thing, but that would #-- require permissions that we don't have in the /etc/ directory, so we #-- ... (3 Replies)
Discussion started by: edstevens
3 Replies

2. Shell Programming and Scripting

Question on sync 2 folders with checking files' modify date

Hi Linux Community I would like to ask about how to compare files in deferent server with date. Those A and B servers has the same folder, I have write a sample script to "ls" both folders and "diff" them, and then "rsync" the missing files. It was running well, both A and B are sync, until... (2 Replies)
Discussion started by: lunaticdawn
2 Replies

3. Solaris

OpenBSM not catching all file writes

I have a custom auditing class configured for these events but it doesn't seem to be catching it when I do a "echo hey > test.txt" Any ideas on why that is? ---------- Post updated at 06:04 PM ---------- Previous update was at 05:47 PM ---------- Scratch this topic, it looks like it's because... (0 Replies)
Discussion started by: thmnetwork
0 Replies

4. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

5. Shell Programming and Scripting

Please modify my code...

Hi, Ths is the code I wrote: #!/bin/bash tr "nameofTheCITY" $1 lynx -dump "http://www.theweathernetwork.com/index.php?product=search&pagecontent=results&prodtype=city&prodname=nameofTheCITY"|grep $1|head -1000>>temp.txt the program in supposed to change the last part of the website to... (1 Reply)
Discussion started by: andrew1400
1 Replies

6. Programming

How to modify this c code to get full GUI interface for Unix Linux ?

Hi, the following code comes from Unix Linux dialog utility project. Unfortunately, this project is no more actively maintained. Unix Linux dialog utility is made of widget boxes. One of them is mixedgauge.c In the following example I would like to get rid of flickering when run in... (0 Replies)
Discussion started by: jack2
0 Replies

7. Shell Programming and Scripting

Modify a file

Hi all Can anyone suggest me a good solution ? My requirement is as follows I have a plain text file similar to this... sending data to 0003345234 here is the output... ,.......... ........... ....... sending data to 00033452ab here is the output... ,.......... ........... .... (5 Replies)
Discussion started by: ./hari.sh
5 Replies

8. Shell Programming and Scripting

Can I modify the .bashrc file instead of .profile file to customize my login?

Hello, I got this question which tells me to customize my login script. Some people in the forums suggested to modify the .profile file in my home directory. I did so, but none of my customizations show up when I open the terminal after. So, I tried to modify other files in my home directory,... (1 Reply)
Discussion started by: Hyunkel
1 Replies

9. Shell Programming and Scripting

Filter by modify date.

I want to filter an "ls -al" command so it only shows me files with modify dates older than two weeks. What is the best way of doing this? (2 Replies)
Discussion started by: millerdc
2 Replies

10. UNIX for Dummies Questions & Answers

modify date of a file

I'm trying to find a way to print the date that a file was modified on. Using ls -l gives me to much information! All i need is the date. There must be a simple way to do this, but i cannot find it anywhere.. can someone give me some advise? (2 Replies)
Discussion started by: tine
2 Replies
Login or Register to Ask a Question