Searching string in a multiline file using perl - perl

I'm trying to find a match in a multi-line string using this script.
It works only when there's one row in the destination file.
I would like to know if there's any substitution for $_ in order to search a multi-line text?
#!/usr/bin/perl
my $time=`date +%D_%H:%M`;
chomp($time);
my $last_location=`cat /file.txt`;
chomp($last_location);
open (ERRORLOG, ">>/errors.log") || die "failed to open errorlog file \n$!\n\a";
open (MESSAGES, "</logfile") || die "failed to open alarms file \n$!\n\a";
seek(MESSAGES, 0, 2) || die "Couldn't seek to pos: 0 at end of file $!\n";
$end_position = tell(MESSAGES);
if ($end_position < $last_location) {
$last_location=0;
}
if ($end_position > $last_location) {
seek(MESSAGES, $last_location, 0) || die "Couldn't seek to pos: $last_location $! \n";
$num_of_messages_sent=0;
while (<MESSAGES>) {
chomp;
$line_to_check $_;
if ($line_to_check =~ /some text/ ) {
print ERRORLOG "$time: $line_to_check \n";
if ($num_of_messages_sent < 4) {
do something;
}
if ($num_of_messages_sent == 4) {
do something;
}
#increase counter
$num_of_messages_sent = $num_of_messages_sent + 1;
}
}
$last_location = tell(MESSAGES);
# print "last: $last_location , end: $end_position \n";
`echo $last_location >/file_last_location.txt`;
}
close (ERRORLOG);
close (MESSAGES);

Looks better this way:
while (my $line = <MESSAGES>) {
chomp($line);
print "line : $line\n";
if ($line =~ m!your_regexp_here!i){
print ERRORLOG "$time: $line_to_check \n";
$num_of_messages_sent++;
print "\tMATCH\tline: $line\n";
if ($num_of_messages_sent < 4){
print "Found $num_of_messages_sent matches\n";
}
}
}

Related

Unable to print after while loop in perl

BEGIN {
use FindBin;
$scriptsDir = $FindBin::RealBin;
}
sub print_log {
($log, $msg) = ($_[0], $_[1]);
print $log $msg;
}
$opt_rh_gsr = "path_to_file";
open(FO, "$opt_rh_gsr") || die "-F-: Can not open file \n";
while(<FO>) {
if(/vdd_nets/) {
$vdd_net = 1;
$vdd_string = "VDD_NETS \{ \n";
}
if(/gnd_nets/) {
$gnd_net = 1;
}
if(($gnd_net == 1)) {
chomp();
$new_line = $_;
#split_new_line = split(":", $new_line);
}
if(($gnd_net == 1) && /\}/) {
$gnd_net = 0;
$gnd_string .= "\} \n";
exit;
}
if($vdd_net) {
if(/^\s*\S+\s+\S+\s+{/) {
$paren++;
}
if (0 != $paren && /^\s*(\w+)\s*$/) {
$vdd_nets{$1} = $parenvolt;
next;
}
if(/^\s*}\s*$/ || /^\s+$/) {
if (0 == $paren) {
$vdd_net = 0; next;
}
else {
$paren--; next;
}
}
chomp();
if(/\s*\}\s*$/ && ($vdd_net == 1)){
s/\'//g;
$vdd_net = 0;
#_ = split(":");
$vdd_string .= "$_[0] $_[1] \n";
$vdd_string .= "\} \n";
next;
}
if($gnd_net) {
if(/^\s*\}\s+$/ || /^\s+$/) {
$gnd_net = 0;
next;
}
#chomp();
if(/\s*\}\s*$/ && ($gnd_net == 1)){
s/\'//g;
$gnd_net = 0;
}
#_ = split();
$GNDNET = $_[0];
if ($_[0] =~ /^\w+$/) {
$groundnets{$_[0]} = 1;
}
}
}
}
print " done reading \n";
close(FO);
print "closed file \n";
The above is not printing the last 2 print statement (before and after the close of file handle). I tried print STDOUT, that didn't work. I also tried to flush, that didn't work either.
The script is exiting after executing, so it is not stuck in a infinite loop anywhere. I tries using perl5.6 and 5.8, but both of them have the same problem.
To exit a loop, you should use the keyword last instead of exit (which exits the whole program). This if:
if(($gnd_net == 1) && /\}/) {
$gnd_net = 0;
$gnd_string .= "\} \n";
print "exiting loop $gnd_string \n";
exit;
}
Should thus be:
if(($gnd_net == 1) && /\}/) {
$gnd_net = 0;
$gnd_string .= "\} \n";
print "exiting loop $gnd_string \n";
last;
}
(unless you actually wanted to exit the program, in which case the print should rather have been print "exiting program...")
A few tips:
Always add use strict and use warnings at the beginning of your scripts. It will catch many mistakes and save you a lot of time.
Use 3-operand open to open files (ie, open FILEHANDLE,MODE,EXPR instead of open FILEHANDLE,EXPR), and lexical filehandles (ie, $FO instead of FO). Your open should thus have been: open my $FO, '<', $opt_rh_gsr instead of open(FO, "$opt_rh_gsr").
Adding || die "-F-: Can not open file \n" after open is a good idea, but 1) you should do or die instead of || die (in this specific case it doesn't matter, but with or rather than ||, you can omit the parenthesis around open's arguments), and 2) you should add the name of the file you were trying to open (in that case, you'd print die "-F-: Can not open file '$opt_rh_gsr'). 3) add $! to the die to have the error message (die "-F-: Can not open file '$opt_rh_gsr': $!). And 4), as suggested by TLP, don't add a newline at the end of a die string.
sub print_log { ($log, $msg) = ($_[0], $_[1]); ... could have been sub print_log { ($log, $msg) = #_;; it's a bit more idiomatic and concise.
Indent properly your code. It's possible that indentation was lost in the copy-paste, but, if it's not the case, then you should indent better your code. This will save you a lot of time when writing/reading your code, and will save other people even more time when they'll read your code. Most IDEs have indentation features that can help you indent the code.

Search a list file of files for keywords

I have an array of files. Now I need to cat each file and search for a list of keywords which is in file keywords.txt.
my keywords.txt contains below
AES
3DES
MD5
DES
SHA-1
SHA-256
SHA-512
10.*
http://
www.
#john.com
john.com
and I'm expecting output as below
file jack.txt contains AES:5 (5 line number) http://:55
file new.txt contains 3DES:75 http://:105
Okay Here is my Code
use warnings;
use strict;
open STDOUT, '>>', "my_stdout_file.txt";
my $filename = $ARGV[2];
chomp ($filename);
open my $fh, q[<], shift or die $!;
my %keyword = map { chomp; $_ => 1 } <$fh>;
print "$fh\n";
while ( <> ) {
chomp;
my #words = split;
for ( my $i = 0; $i <= $#words; $i++ ) {
if ( $keyword{ $words[ $i ] } ) {
print "Keyword Found for file:$filename\n";
printf qq[$filename Line: %4d\tWord position: %4d\tKeyword: %s\n],
$., $i, $words[ $i ];
}
}
}
But the problem is its considering all the Arguments and trying to open the files for ARGV[2]. Actually i need to Open only ARGV[0] and ARGV[1]. ARGV[2] i kept for writing in output only.
Appreciate responses.
Thanks.
I think maybe there are multipe keywords in one line of those txt files. So below code for your reference:
my #keywords;
open IN, "keywords.txt";
while(<IN>) {
if(/^(.+)$/) {
push(#keywords, $1);
}
}
close(IN);
my #filelist=glob("*.txt");
foreach my $filename(#filelist) {
if(open my $fh, '<', $filename) {
my $ln=1;
while(my $line = <$fh>) {
foreach my $kw(#keywords) {
if($line=~/$kw/) {
print $filename.':'.$ln.':'.$kw."\n";
}
}
$ln++;
}
}
close($fh);
}
Ok but one thing i noticed here is..
the Keywords
http://
oracle.com
it is matching the whole word, instead it should try to match as part of strings.. Only these keywords.. Others should be searched as per the above.

Perl script to convert xls to csv

This script coverts xls to csv ok.
The challenge is that it does not convert blank cell in the xls to blanks in csv file.
Any help is appreciated: UPDATED SCRIPT
#!/usr/bin/perl
use strict;
use Spreadsheet::ParseExcel;
use Text::CSV;
my $sourcename = shift #ARGV or die "invocation: $0 <source file>\n";
my $source_excel = new Spreadsheet::ParseExcel;
my $source_book = $source_excel->Parse($sourcename)
or die "Could not open source Excel file $sourcename: $!";
my $storage_book;
foreach my $source_sheet_number (0 .. $source_book->{SheetCount}-1) {
my $source_sheet = $source_book->{Worksheet}[$source_sheet_number];
print "--------- SHEET:", $source_sheet->{Name}, "\n";
next unless defined $source_sheet->{MaxRow};
next unless $source_sheet->{MinRow} <= $source_sheet->{MaxRow};
next unless defined $source_sheet->{MaxCol};
next unless $source_sheet->{MinCol} <= $source_sheet->{MaxCol};
foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}) {
foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}) {
my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
if ($source_cell && $source_cell->Value) {
#print "( $row_index , $col_index ) =>", $source_cell->Value, "\t;";
print $source_cell->Value, ";";
}
else
{
print ";"
}
}
}
}
sample excel
EFG KDD ABS JME
FGO POP JET
converted as:
EFG;KDD;ABS;JME;
FGO;POP;JET;
but it should be:
EFG;KDD;ABS;JME;
FGO;;POP;JET;
You have to check if the value of the cell is initialized, not the cell it self.
Change:
if ($source_cell) {
#print "( $row_index , $col_index ) =>", $source_cell->Value, "\t;";
print $source_cell->Value, ";";
}
To:
if ($source_cell && $source_cell->Value) {
#print "( $row_index , $col_index ) =>", $source_cell->Value, "\t;";
print $source_cell->Value, ";";
} else {
print ";";
}
should work.
UPDATE:
foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}) {
foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}) {
my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
if ($source_cell && $source_cell->Value) {
print $source_cell->Value.";";
} else {
print ";";
}
}
print "\n";
}
}

Perl script.file handling issues

I have written a Perl script:
#!/usr/bin/perl
use strict;
use warnings;
my $file_name;
my $ext = ".text";
my $subnetwork2;
my %files_list = ();
opendir my $dir, "." or die "Cannot open directory: $!";
my #files = readdir $dir;
sub create_files() {
my $subnetwork;
open(MYFILE, 'file.txt');
while (<MYFILE>) {
if (/.subnetwork/) {
my #string = split /[:,\s]+/, $_;
$subnetwork = $string[2];
}
if (/.set/ && (defined $subnetwork)) {
my #string = split /[:,\s]+/, $_;
my $file = $subnetwork . $string[1];
open FILE, ">", "$file.text" or die $!;
close(FILE);
}
}
close(MYFILE);
}
sub create_hash() {
foreach (#files) {
if (/.text/) {
open($files_list{$_}, ">>$_") || die("This file will not open!");
}
}
}
sub init() {
open(MYFILE3, 'file.txt');
while (<MYFILE3>) {
if (/.subnetwork/) {
my #string3 = split /[:,\s]+/, $_;
$subnetwork2 = $string3[2];
last;
}
}
close(MYFILE3);
}
sub main_process() {
init;
create_files;
create_hash;
open(MYFILE1, 'file.txt');
while (<MYFILE1>) {
if (/.subnetwork/) {
my #string3 = split /[:,\s]+/, $_;
$subnetwork2 = $string3[2];
}
if (/.set/) {
my #string2 = split /[:,\s]+/, $_;
$file_name = $subnetwork2 . $string2[1] . $ext;
}
if (/.domain/ || /.end/ || ($. < 6)) {
my $domain = $_;
foreach (#files) {
if (/.text/ && /$subnetwork2/) {
prnt { $files_list{$_} } "$domain";
}
}
}
elsif ($. >= 6) {
print { $files_list{$file_name} } "$_";
}
}
close(MYFILE1);
foreach my $val (values %files_list) { close($val); }
closedir $dir;
}
main_process;
This script creates files in the current directory based upon the content of file.txt, and then open those files again.
Then it starts processing file.txt and redirects the lines according to the filename set dynamically.
This setting of the file name is also based upon the data in the file file.txt.
The problem that I am facing here is that the redirection is only to a single file. That means there is some problem with the file handle.
All the files that are expected to be created are created perfectly but the data goes into only one of them.
I doubt if there is a problem with the file handle that I am using while redirecting.
Could anyone please help?
Sample input file is below:
..cnai #Generated on Thu Aug 02 18:33:18 2012 by CNAI R21D06_EC01, user tcssrpi
..capabilities BASIC
.utctime 2012-08-02 13:03:18
.subnetwork ONRM_ROOT_MO:NETSim_BAG
.domain BSC
.set BAG01
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
AMRCSFR3ICM=
AMRCSFR4ICM=
USERDATA=""
.set BAG02
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
..end
The problem that i am facing is during execution:
> process.pl
Use of uninitialized value in ref-to-glob cast at process.pl line 79, <MYFILE1> line 6.
Can't use string ("") as a symbol ref while "strict refs" in use at process.pl line 79, <MYFILE1> line 6.
The problem i can understand is with this line:
print { $files_list{$_} } "$domain";
but i am unable to understand why!!
The output i need is :
> cat NETSim_BAGBAG01.text
.set BAG01
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
AMRCSFR3ICM=
AMRCSFR4ICM=
USERDATA=""
> cat NETSim_BAGBAG02.text
.set BAG02
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
>
Your problem in following lines:
open(PLOT,">>$_") || die("This file will not open!");
$files_list{$_}=*PLOT;
You should replace they with:
open($files_list{$_},">>$_") || die("This file will not open!");
This portion of your code is the key:
open(PLOT,">>$_") || die("This file will not open!");
$files_list{$_}=*PLOT;
The problem is that you are essentially using the filehandle PLOT as a global variable; every single entry in your hash is pointing to this same filehandle. Replace with something like this:
local *PLOT;
open(PLOT,">>$_") || die("This file will not open!");
$files_list{$_}=*PLOT;
You have got youself very entangled with this program. There is no need for the hash table or the multiple subroutines.
Here is a quick refactoring of your code that works with your data and writes files NETSim_BAG.BAG01.text and NETSim_BAG.BAG02.text. I put a dot between the subnet and the set to make the names a little clearer.
use strict;
use warnings;
my $out_fh;
open my $fh, '<', 'file.txt' or die $!;
my ($subnetwork, $set, $file);
while (<$fh>) {
if ( /^\.subnetwork\s+\w+:(\w+)/ ) {
$subnetwork = $1;
}
elsif ( /^\.set\s+(\w+)/ and $subnetwork) {
$set = $1;
$file = "$subnetwork.$set.text";
open $out_fh, '>', $file or die qq(Unable to open "$file" for output: $!);
print $out_fh;
}
elsif ( /^\.\.end/ ) {
undef $subnetwork;
undef $file;
}
if (/^[^.]/ and $file) {
print $out_fh $_;
}
}

Comparing filehandle with glob returned by IO::Select::can_read()

I am trying to manage three filehandles via IO::Select in perl. I have 1 input handle, 1 input/output handle, and 1 output handle. Im having a little trouble determining which filehandle is which when processing Select's return arrays of can_read() and can_write();
Example below
Any advice on how to actually compare these two filehandles? I've tried scalars, i've tried references, no references, etc. I can't think of why this isn't working.
#!/usr/bin/perl
use strict;
use warnings;
use IO::Select;
open(INPUT, "/dev/fd/3") or die "Unable to open input! $!";
my $stdin_buf;
# Main loop
while (1)
{
foreach my $read_fh ($select->can_read(10)) # This DOES return INPUT as being readable
{
if ($read_fh == \*INPUT) # THIS fails.
{
read($read_fh, $stdin_buf, 512);
}
}
}
I got it working. It was a combination of using references and eq (which i had tried prior to fixing references);
working code:
#!/usr/bin/perl
use strict;
use warnings;
use IO::Select;
##############################################
# Asterisk to stream pipe thingie-ma-jig-bob #
# Written by Sean Powell - 10-5-10 #
##############################################
my $extension = $ARGV[0];
if (!$extension || $extension !~ /^\d+$/)
{
print "USAGE: Please provide a decimal extension as the first parameter";
exit(1);
}
my $ffmpeg = "/usr/bin/ffmpeg -f s16le -ar 8000 -ac 1 -i - -ab 64k -f mp3 -";
my $ezstream = "/usr/local/bin/ezstream -c /etc/asterisk/ICES/" . $extension . ".xml";
my $stdin_buf;
my $ffmpeg_buf;
my $last_activity = 0;
open(INPUT, "/dev/fd/3") or die "Unable to open input! $!";
open(FFMPEG, "|$ffmpeg") or die "Unable to fork off ffmpeg! $!";
open(EZSTREAM, "|$ezstream") or die "Unable to fork off ezstream! $!";
open(DEBUG, ">>/root/debug.log") or die "Unable to open debug log! $!";
my ($input_fh, $ffmpeg_fh, $ezstream_fh) = (*INPUT, *FFMPEG, *EZSTREAM);
my $select = new IO::Select(*INPUT);
$select->add(*FFMPEG);
$select->add(*EZSTREAM);
# Main loop
while (1)
{
foreach my $read_fh ($select->can_read(10))
{
print DEBUG "Filehandle can read: $read_fh - $input_fh - $ffmpeg_fh - $ezstream_fh\n";
if ($read_fh eq $input_fh)
{
my $read = read($read_fh, $stdin_buf, 512);
print DEBUG "Read off $read bytes from INPUT\n";
$last_activity = time();
}
if ($read_fh eq $ffmpeg_fh)
{
my $read = read($read_fh, $ffmpeg_buf, 512);
print DEBUG "Read off $read bytes from FFMPEG\n";
$last_activity = time();
}
}
foreach my $write_fh ($select->can_write(10))
{
if ($write_fh eq $ffmpeg_fh && length($stdin_buf) > 0)
{
my $size = length($stdin_buf);
my $wrote = syswrite($write_fh, $stdin_buf, $size);
while ($wrote < $size)
{
$wrote += syswrite($write_fh, $stdin_buf, $size - $wrote, $wrote);
}
print DEBUG "Wrote $wrote bytes to FFMPEG\n";
$last_activity = time();
$stdin_buf = undef;
}
if ($write_fh eq $ezstream_fh && length($ffmpeg_buf) > 0)
{
my $size = length($ffmpeg_buf);
my $wrote = syswrite($write_fh, $ffmpeg_buf, $size);
while ($wrote < $size)
{
$wrote += syswrite($write_fh, $ffmpeg_buf, $size - $wrote, $wrote);
}
$ffmpeg_buf = undef;
print DEBUG "Wrote $wrote bytes to EZSTREAM\n";
$last_activity = time();
}
}
last if (time() - $last_activity > 30);
}
close(INPUT);
close(EZSTREAM);
close(FFMPEG);
Use eq instead of == for this kind of comparison.
if ($read_fh eq *INPUT) { ... }