954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to perform airthimatic operation on file contents

hi i m abhi
i have one file containing some info like
LT19-10-13-400922 --TID1-3039 1--RequestREGISTER
LT19-10-13-405432 --TID1-3039 1--Response100
LT19-10-13-410015 --TID1-3039 1--Response401
LT19-10-13-415481 --TID1-3039 2--RequestREGISTER
LT19-10-13-419800 --TID1-3039 2--Response100

where LT19-10-13-400922 is log time ,,i.e. 19 hr. 10 min. 13 seconds 400922 usec. and i want this to convert only in second that is (19*3600+ 10*60 + 13). i want other out put file with these values.


waiting for early respone
Thanks in Advance:)

singhabsk
Newbie Poster
16 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

Try this:

use strict;
use warnings;
open (OUT, ">newfile.txt");
while(<DATA>){
	chomp;
	my ($time,$rest)=split(/ --/,$_,2);
	my ($hours,$mins,$secs,$usec)=split(/\-/,$time);
	$hours=~s/^LT//;
	my $newsec=$hours*3600+$mins*60+$secs;
	print OUT "LT$newsec --$rest\n";
}
close OUT;

__DATA__
LT19-10-13-400922 --TID1-3039 1--RequestREGISTER
LT19-10-13-405432 --TID1-3039 1--Response100
LT19-10-13-410015 --TID1-3039 1--Response401
LT19-10-13-415481 --TID1-3039 2--RequestREGISTER
LT19-10-13-419800 --TID1-3039 2--Response100


The DATA section can be read from another file.

Output (newfile.txt):

LT69013 --TID1-3039 1--RequestREGISTER
LT69013 --TID1-3039 1--Response100
LT69013 --TID1-3039 1--Response401
LT69013 --TID1-3039 2--RequestREGISTER
LT69013 --TID1-3039 2--Response100


I noticed your time values are all the same. I didn't know if you wanted just the seconds, or the LT+seconds or the rest of each line, so I put them all back in.

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

Thanks mitchems for your early response :)
I suppose my time.txt file containing the contents like

19-10-13-400922 TID1-3039 1RequestREGISTER
19-10-13-405432 TID1-3039 1Response100
19-10-13-410015 TID1-3039 1Response401
19-10-13-415481 TID1-3039 2RequestREGISTER
19-10-13-419800 TID1-3039 2Response100
19-10-13-426400 TID2-3039 1RequestREGISTER
.........................................
and i have modified the code as below

#!perl -w

use strict;
use warnings;

open(INPUT, "newfile.txt");
while(){
chomp;
my ($time,$rest)=split(/ --/,$_,2);
my ($hours,$mins,$secs,$usec)=split(/\-/,$time);
$hours=~s/^LT//;
my $newsec=$hours*3600+$mins*60+$secs;
print OUT "LT$newsec --$rest\n";
}

close INPUT;
close OUT;

but its giving following errors

Use of uninitialized value in concatenation (.) or string at check.pl line 14, line 1.
Use of uninitialized value in concatenation (.) or string at check.pl line 14, line 2.
Use of uninitialized value in concatenation (.) or string at check.pl line 14, line 3.
Use of uninitialized value in concatenation (.) or string at check.pl line 14, line 4.
Use of uninitialized value in concatenation (.) or string at check.pl line 14, line 5.
Use of uninitialized value in concatenation (.) or string at check.pl line 14, line 6.
:(
help me out plzzzz
thanks alot

singhabsk
Newbie Poster
16 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

hey mitchems in output file $rest part is not writing in newfile.txt
the problem at line 14 i.e. print OUT "LT$newsec --$rest\n";
:(

singhabsk
Newbie Poster
16 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

The problem is that your original example file had lines of:

LT19-10-13-400922 --TID1-3039 1--RequestREGISTER

And your new one has:

19-10-13-400922 TID1-3039 1 RequestREGISTER

The "--" is missing from the new file. I split on the " --". If it is not there $rest is not initialized, which is giving you both errors. If you wish to fix it, split on something other than " --" (for example split on space).

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

Like this:

use strict;
use warnings;

open(INPUT, "<time.txt");
open (OUT, ">newfile.txt");
while(<INPUT>){
chomp;
my ($time,$rest)=split(/ /,$_,2); #/\t/ if tab delimited
my ($hours,$mins,$secs,$usec)=split(/\-/,$time);
$hours=~s/^LT//;
my $newsec=$hours*3600+$mins*60+$secs;
print OUT "$newsec $rest\n";
}
close INPUT;
close OUT;


It would be helpful if you posted the actual format of the input file. Also, I don't know if your file is space of tab delimited. See above.

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

:)
Thank u very much mitchem
yes i had mistaken............now its solved,,,
hope u will help further ...........Mr. Perl GURU :)

singhabsk
Newbie Poster
16 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

Good morning all,
here i have some probs. in logic if possible just solved it
i have a text file contains following data.....
sec TID ATTRIBUTE
69013 1-3039-1 REGISTER
69013 1-3039-1 100
69013 1-3039-1 401
69013 1-3039-2 REGISTER
69013 1-3039-2 100
69013 2-3039-1 REGISTER
69013 3-3039-1 REGISTER
69013 4-3039-1 REGISTER
69013 1-3039-2 200
69013 2-3039-1 100
69013 2-3039-1 401
..................
..................
69023 193-3039-2 100
69023 193-3039-2401
69023 222-3039-1 REGISTER
69023 177-3039-2 100
69024 177-3039-2401
69024 214-3039-1 100
69024 214-3039-1 401
69024 214-3039-2 REGISTER
69024 223-3039-1 100
69024 223-3039-1 401
69024 223-3039-2 REGISTER
......................................................................................
For first 10 second records
i wanna no. of count of 401 where 401's and REGISTER's TIDs are same.
i wanna no. of count of 200 where 200's and REGISTER's TIDs are same.
i wanna no. of count of different REGISTER's TIDs. (that is no. of different TIDs ending with 1 (1-3039-1 or 2-3039-1) and ending with two (1-3039-2 or 1-3039-2)).

help me out if u can perl guru............
thanks in adv. :)

singhabsk
Newbie Poster
16 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

Start a new thread. This one is solved.

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

k thanks
:)

singhabsk
Newbie Poster
16 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You