in reply to Re^6: How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
in thread How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column

I have modified the program and now I am able to get the excel data Into a text file to be used as keywords. I want these keywords to be searched from no of files (main_log here) but not able to get the output like I want . My Input Data
8077 8116 D type hexa usim = 1, isim = 1 8077 8116 D type hexa usim = 1, isim = 1 pcscf_v4 : num = 2, protocol_type = 0x21, port_num = 0, addr = 10.56.5 +.85 8088 1223 D temp sim_invalid = 0 8099 1223 XX is_ems_support = 1 #88 8099 1224 XX pdn_act_ind->pcscf_discovery_flag = 1 ind->signaling_flag = 1 some text here plmn = 405872 DefaultStatusBarPlmnPlugin: into updateCarrierLabel ipv6 = 36.5.2.4.25.143.200.141.0.0.0.0.32.20.232.161 get_ipv6_prefix,temp ipv4 = 10.167.185.101 _send_ipv4_notify info.is_ems_support = 1 act_ind->emergency_indication = 1 access_rat_type = 0 transaction_id = 2, rat_type = 0

Variables to read from Excel sheet :

isim = usim = sim_invalid = pcscf_discovery_flag signaling_flag ran_type emergency_ind rat pcscf_v4 pcscf_v6 plmn is_ems_support ipv4 ipv6

#!/usr/bin/env perl use strict; use warnings; my $ex = ''; my $book = ''; my $sheet = ''; my $data = ''; my $my_data = 0; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; # use existing instance if Excel is already running eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; } $book = $ex->Workbooks->Open("E:\\Automation Related\\Perl Scripts - A +LL\\IMS_debugging\\Excel\\UE_NW_Parameters.xlsx"); $sheet = $book->Worksheets(1); $data = $sheet -> Range("A1:B2873") -> {Value}; # Opening the Output file for exporting the Excel data Into Text File open(FILE, ">excel_data.txt"); foreach my $cell_ref (@$data) { foreach my $my_data (@$cell_ref){ print FILE ($my_data); } print FILE "\n"; } close FILE; # Opening excel data File here open( my $kw, '<', 'excel_data.txt') or die $!; my @keywords = <$kw>; chomp(@keywords); # remove newlines at the end of keywords #get list of files in current directory my @files = <main_log*>; # loop over each file to search keywords in foreach my $file (@files) { open(my $fh, '<', $file) or die $!; my @content = <$fh>; close($fh); my $l = 0; my $outfile = 'excel_match'.$file.'.txt'; print "Check the output generated In file $outfile\n"; open my $fh_out, '>', $outfile or die "$!"; foreach my $kw (@keywords) { my $search = quotemeta($kw); # otherwise keyword is used as re +gex, not literally foreach (@content) { # go through every line for this keyword $l++; if (/\Q$search/) { print $fh_out "%s in file %s, line %d:%s'.$/, +$kw, $file, $l, $_"; last; } } } }

My input data to search from: 8077 8116 D type hexa usim = 1, isim = 1 8077 8116 D type hexa usim = 1, isim = 1 pcscf_v4 : num = 2, protocol_type = 0x21, port_num = 0, addr = 10.56.5.85 8088 1223 D temp sim_invalid = 0 8099 1223 XX is_ems_support = 1 #88 8099 1224 XX pdn_act_ind->pcscf_discovery_flag = 1 ind->signaling_flag = 1 some text here plmn = 405872 DefaultStatusBarPlmnPlugin: into updateCarrierLabel ipv6 = 36.5.2.4.25.143.200.141.0.0.0.0.32.20.232.161 get_ipv6_prefix,temp ipv4 = 10.167.185.101 _send_ipv4_notify info.is_ems_support = 1 act_ind->emergency_indication = 1 access_rat_type = 0 transaction_id = 2, rat_type = 0

My output should be like these for above data Into excel format isim = 1 usim = 1 sim_invalid = 0 pcscf_discovery_flag 1 signaling_flag 1 ran_type 0 emergency_ind 1 rat pcscf_v4 10.56.5.85 pcscf_v6 plmn 405872 is_ems_support 1 ipv4 10.167.185.101 ipv6 36.5.2.4.25.143.200.141.0.0.0.0.32.20.232.161

Issue - I am not able to get the corresponding lines searched matching keywords so I can pull out the variables value from these lines and put back into excel corresponding to these keywords e.g. x =5 (x is variable and 5 is the value to be picked from a line containing x) Can you please help ?
  • Comment on Re^7: How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
  • Select or Download Code

Replies are listed 'Best First'.
Re^8: How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
by poj (Abbot) on Feb 23, 2017 at 19:22 UTC

    Using keyword isim = as an example, do you want the value 1 to be inserted in the column next to that keyword or just replace that keyword with isim = 1

    Update: You want output to be

    ran_type	0
    pcscf_v4	10.56.5.85
    
    but (1) there is no ran_type in the input data and (2) on this line
    pcscf_v4 : num = 2, protocol_type = 0x21, port_num = 0, addr = 10.56.5.85
    the match is against keyword addr =

    Please put <c></c> tags around your example data.
    poj
      Using keyword isim = as an example, do you want the value 1 to be inserted in the column next to that keyword => YES Update: You want output to be ran_type 0 pcscf_v4 10.56.5.85 =>YES pcscf_v4 : num = 2, protocol_type = 0x21, port_num = 0, addr = 10.56.5.85 the match is against keyword addr = => actually there Is also a field address as well but I want to capture only addr corresponding pcscf_v4. My Input data
      8077 8116 D type hexa usim = 1, isim = 1 8077 8116 D type hexa usim = 1, isim = 1 pcscf_v4 : num = 2, protocol_type = 0x21, port_num = 0, addr = 10.56.5 +.85 8088 1223 D temp sim_invalid = 0 8099 1223 XX is_ems_support = 1 #88 8099 1224 XX pdn_act_ind->pcscf_discovery_flag = 1 ind->signaling_flag = 1 some text here plmn = 405872 DefaultStatusBarPlmnPlugin: into updateCarrierLabel ipv6 = 36.5.2.4.25.143.200.141.0.0.0.0.32.20.232.161 get_ipv6_prefix,temp ipv4 = 10.167.185.101 _send_ipv4_notify info.is_ems_support = 1 act_ind->emergency_indication = 1 access_rat_type = 0 transaction_id = 2, rat_type = 0 signaling_flag = 1, ran_type = 0 At stop time, parameter address=noping

        I assume this spreadsheet is some kind of template that needs to be filled in otherwise just creating a new one would be much easier. Whilst scanning the spreadsheet for the keywords you need to store the cell address for each if you want to write back the values.

        Try this poj