How to get the lines matched of a file in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the lines matched of a file in perl?
# 1  
Old 07-28-2010
How to get the lines matched of a file in perl?

Hi,

I want to match the time in the file and retrieve those contents of the file.

I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing.

For Example

Code:
$start = "14:23";
$end = "14:30";

I am matching file contents with this time but some times it matches start time or end time or both all the cases i am considering it.

If start time is not matching then if end time is matching it should retrieve both the lines of start time as well as end time.

For example: Here are the file contents:

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

It should retrieve all the 3 lines in this case.and it should add 7+5 =12

Example2:
Code:
14:24:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

In this case End time is matching so it should all the 3 lines and it should add 7+5 =12

Example3:

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

In this case both start time and End time are matching so it should all 3 lines and it should add 7+5 =12

I am not able to perform the above action.

How can i get this in perl?

Regards
# 2  
Old 07-28-2010
Quote:
Originally Posted by vanitham
Hi,

I want to match the time in the file and retrieve those contents of the file.

I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing.

For Example

Code:
$start = "14:23";
$end = "14:30";

I am matching file contents with this time but some times it matches start time or end time or both all the cases i am considering it.

If start time is not matching then if end time is matching it should retrieve both the lines of start time as well as end time.

For example: Here are the file contents:

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

It should retrieve all the 3 lines in this case.and it should add 7+5 =12

Example2:
Code:
14:24:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

In this case End time is matching so it should all the 3 lines and it should add 7+5 =12

Example3:

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

In this case both start time and End time are matching so it should all 3 lines and it should add 7+5 =12

I am not able to perform the above action.

How can i get this in perl?

Regards
(1) Why does the second line match the pattern in all cases ? For example, given these values:

Code:
$start = "14:23";
$end = "14:30";

the second line (in red) -

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

matches neither the start time (14:25:03 != 14:23) nor the end time (14:27:00 != 14:30).


(2) What is the format of the end time in the 2nd line in each case ?

For example, consider this end time in 2nd line (in red):

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

That's not HH:MM:SS format, because of the :16 after 00.

Code:
14:27:00:16
HH:MM:SS:??

Should it not be like so ?

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00,16,7,0,0
14:25:00,14:30:16,5,0,0

tyler_durden
# 3  
Old 07-29-2010
Quote:
Originally Posted by durden_tyler
(1) Why does the second line match the pattern in all cases ? For example, given these values:

Code:
$start = "14:23";
$end = "14:30";

the second line (in red) -

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

matches neither the start time (14:25:03 != 14:23) nor the end time (14:27:00 != 14:30).


(2) What is the format of the end time in the 2nd line in each case ?

For example, consider this end time in 2nd line (in red):

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00:16,7,0,0
14:25:00,14:30:16,5,0,0

That's not HH:MM:SS format, because of the :16 after 00.

Code:
14:27:00:16
HH:MM:SS:??

Should it not be like so ?

Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00,16,7,0,0
14:25:00,14:30:16,5,0,0

tyler_durden
Hi,

If we consider as above:
Code:
14:23:00,14:25:16,0,0,0
14:25:03,14:27:00,16,7,0,0
14:25:00,14:30:16,5,0,0

How can we get the lines and add up the values?

Regards
# 4  
Old 07-29-2010
MySQL solution

Is this the code you are trying ,

Code:
#!/usr/bin/perl 

use strict;
use warnings;

my $start = "14:23";
my $end = "14:30";
my $ind = 0;
my @arr;
my $result = 0;
open FILE,"<file";
while ( <FILE> ) {
    if ( /^$start.+/) {
        $ind = 1 ;
        my $reversed = reverse $_;
        my @temp  = split (/,/,$reversed);
        push  @arr,$temp[2];
    }
    elsif ( $ind == 1 && /^[0-9]{2}:[0-9]{2}:[0-9]{2}:$end.+/ ) {
        my $reversed = reverse $_;
        my @temp  = split (/,/,$reversed);
        push  @arr,$temp[2];
        $ind = 0;
    }
    elsif ( $ind == 1 ) {
        my $reversed = reverse $_;
        my @temp  = split (/,/,$reversed);
        push  @arr,$temp[2];
    }
    else {
        next;
    }
}

foreach ( @arr )
{
    $result += $_;
}

print "Result : $result";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all matched lines from two files

Hello, everyone I have two files like this: File 1: A B C D E FFile 2:A B 1 A C 2 A K 3 B A 4 D E 3 W X 2 A B 2I want to print all lines (file2) that the first two columns are consist of elements from file1. So, what I expected is : A B 1 A B 2 (2 Replies)
Discussion started by: nengcheng
2 Replies

2. UNIX for Dummies Questions & Answers

Extracting the two lines where the first line is matched

Hi, If I have a file of something like @hg19_gold_AL122127.6-131160 GCTTCATCATGCATGGATAGGCTGGCGCCTTTCCTGAGGCCATATGCCGATGGATATG @hg19_gold_AL122127.6-131159 CTTTAATATTTCCGCCACCATCCTGAGTGAATCCCAGCAAGGACAGTCTTTGGGGATT @hg19_gold_AL122127.6-131158... (4 Replies)
Discussion started by: jyu429
4 Replies

3. Shell Programming and Scripting

Delete lines and the first pattern between 2 matched patterns

Hi, i need help to delete all the lines between 2 matched patterns and the first pattern must be deleted too. sample as follows: inputfile.txt >kump_1 ........................... ........................... >start_0124 dgfhghgfh fgfdgfh fdgfdh >kump_2 ............................. (7 Replies)
Discussion started by: redse171
7 Replies

4. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

5. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

6. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

7. Shell Programming and Scripting

removing lines around a matched pattern

I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line. I've googled for this problem, and most people helping are only interested in... (9 Replies)
Discussion started by: tamale
9 Replies

8. Shell Programming and Scripting

Get header and its matched value in perl?

Hi, I have the file contents: Start,End,Req,Resp 12:39,12:40,4,5 The sting to be matched is: Req and Resp. parsefile("Req,Resp"); Here is the code. sub parsefile ($) { $header=shift; (2 Replies)
Discussion started by: vanitham
2 Replies

9. Shell Programming and Scripting

Get all the values matched in perl

HI, I have sentences like this: @str=("An ribonucleic acid (RNA)-binding protein, started its expression in the daughter cells","Elucidation of the mechanism of retinal degeneration of RNA-binding protein","Rna binding protein is an important protein","In the retinal degenerative process... (1 Reply)
Discussion started by: vanitham
1 Replies

10. Shell Programming and Scripting

Sed grep the first matched lines

Hi I have a myfile.txt contains the following: CONTEXT { AAAAA } ... CONTEXT { BBBBB } I want to extract the lines in between CONTEXT { ... }, one by one. Hence I wrote a command like the following, sed -n '/^CONTENT/,/^}/ { w a.txt }' myfile.txt The problem with this... (5 Replies)
Discussion started by: hezjing
5 Replies
Login or Register to Ask a Question