Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I trying substring line from file where is a keywords - open newest file in folder but now I using file as a source data: kontroly20220513_154743.txt

I need find keywords: Konec_Kontroly, Konec, Volat, Stav, Info and print this data to logfile with time: kontroly.log

Now its not working, log file is empty. Can you help me please?

What I have tried:

use strict;
use warnings;
my $vstup = "C:/Users/cen65842/Documents/Kontroly/testkontroly/kontroly20220513_154743.txt";
open( my $default_fh, "<", $vstup ) or die $!;

my $log = "C:/Users/cen65842/Documents/Kontroly/testkontroly/kontroly.log";

my $Konec_Kontroly = substr($default_fh,index($default_fh,"Konec_Kontroly")+12,index($default_fh,"Konec_Kontroly",index($default_fh,"Konec Kontroly")+2)-index($default_fh,"Konec Kontroly")-13);
my $Konec = substr($default_fh,index($default_fh,"Konec")+12,index($default_fh,"Konec",index($default_fh,"Konec")+2)-index($default_fh,"Konec")-13);
my $Volat = substr($default_fh,index($default_fh,"Volat")+12,index($default_fh,"Volat",index($default_fh,"Volat")+2)-index($default_fh,"Volat")-13);
my $Stav = substr($default_fh,index($default_fh,"Stav")+12,index($default_fh,"Stav",index($default_fh,"Stav")+2)-index($default_fh,"Stav")-13);
my $Info = substr($default_fh,index($default_fh,"Info")+11,index($default_fh,"Info",index($default_fh,"Info")+1)-index($default_fh,"Info")-12);

<pre>#ziskani casu - mesic je od 0 a rok je pocet roku od 1900
my ($sec,$min,$hour, $mday,$mon,$year, $wday,$yday,$isdst) = localtime(time);

#sestaveni jsounu pro poslani
my $xjson='{ "GENCELEK" : "SAS_CALLC" , "Konec_Kontroly" : "' . $String . '" , "Datum" : "' . $Konec . '" , "Výsledek kontroly" : "' . $Volat . '" , "Stav" : "' . $Stav . '" , "Info" : "' . $Info . '" }'; 

#zapisovani do logu
open(DATA, ">>", $log);
printf DATA "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;;
print DATA " " . $xjson . " ";
Posted
Comments
Richard MacCutchan 16-May-22 6:51am    
Try displaying some of the variables at the terminal as the script runs, to see that it is working correctly.
Member 15251555 16-May-22 7:48am    
final result in a log file is empty, only: 2022-05-16 13:46:38 { "GENCELEK" : "Konec_Kontroly" , "Konec_Kontroly" : "" , "Datum" : "" , "Výsledek kontroly" : "" , "Stav" : "" , "Info" : "" }

but missing substring values.
Richard MacCutchan 16-May-22 8:44am    
So it looks like all your substring commands are not returning any values. You need to test each command separately. So instead of reading from the input file, set a variable with the actual string that should be read in and execute the substring command to see what results you get.
Richard MacCutchan 16-May-22 9:23am    
Having run some perl tests it would appear that the substring command is using the value of $default_fh, which is a perl internal variable, rather than reading from the file.
Member 15251555 17-May-22 2:00am    
when I use $vstup as a string, substring working but not working substring from open document and search this keywords.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900