Hi,
My objective is to get the line number of the first occurance of the search pattern.
my test.txt contains:
..... ..................
total rows....
................... ..
total rejected rows: 40
total rejected rows: 50
total rejected rows: 80
total rejected rows: 90
total discarded rows: 40
................. ..
................. ..
................. ..
i want to get the line number of first occurance of "total rejected rows:" and the value next to it that is 40 in this case.
i beleive the awk command in this program returns the last value of the line ie., 40 in this case.
And $. returns the corresponding line number.
these are the errors i got while executing this program:
Errors:
String found where operator expected at search2.pl line 19, near "awk '{print NF ":" $0}'"
(Do you need to predeclare awk?)
syntax error at search2.pl line 19, near "awk '{print NF ":" $0}'"
Execution of search2.pl aborted due to compilation errors.
can somebody help me with this?
thanks,
Mercury. -
-
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
sub search_pattern
-
{
-
-
my $file_name = $_[0];
-
-
my $search = $_[1];
-
open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
-
-
-
-
while (<LOGFILE>) {
-
chomp($_);
-
-
if (/$search/) {
-
awk '{print NF ":" $0}' #Value of the last field
-
print "\n$." # prints the line number
-
}
-
}
-
}
-
-
my $file_n ="test.txt";
-
-
my $search_p = "total rejected rows:";
-
-
&search_pattern($file_n, $search_p);
-
-
-
4 3931
Hi,
My objective is to get the line number of the first occurance of the search pattern.
my test.txt contains:
..... ..................
total rows....
................... ..
total rejected rows: 40
total rejected rows: 50
total rejected rows: 80
total rejected rows: 90
total discarded rows: 40
................. ..
................. ..
................. ..
i want to get the line number of first occurance of "total rejected rows:" and the value next to it that is 40 in this case.
i beleive the awk command in this program returns the last value of the line ie., 40 in this case.
And $. returns the corresponding line number.
these are the errors i got while executing this program:
Errors:
String found where operator expected at search2.pl line 19, near "awk '{print NF ":" $0}'"
(Do you need to predeclare awk?)
syntax error at search2.pl line 19, near "awk '{print NF ":" $0}'"
Execution of search2.pl aborted due to compilation errors.
can somebody help me with this?
thanks,
Mercury. -
-
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
sub search_pattern
-
{
-
-
my $file_name = $_[0];
-
-
my $search = $_[1];
-
open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
-
-
-
-
while (<LOGFILE>) {
-
chomp($_);
-
-
if (/$search/) {
-
awk '{print NF ":" $0}' #Value of the last field
-
print "\n$." # prints the line number
-
}
-
}
-
}
-
-
my $file_n ="test.txt";
-
-
my $search_p = "total rejected rows:";
-
-
&search_pattern($file_n, $search_p);
-
-
-
Hi,
You should be running the awk command by enclosing it in `(back ticks).. eg - `awk \'{print NF ":" $0}\'`
But i guess there is some syntax error in the statement.. You might get more details at this link
On the other hand, if you are not soo keen on using awk, this code also provides the same solution - #!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
-
sub search_pattern
-
{
-
my $file_name = $_[0];
-
-
my $search = $_[1];
-
-
open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
-
-
while (<LOGFILE>)
-
{
-
chomp($_);
-
-
if (/$search/)
-
{
-
my($line,$occ) = split(/:/,$_);
-
print "Occurence : $occ\n";
-
print "Line number : $.\n";
-
}
-
}
-
}
-
-
my $file_n ="test.txt";
-
my $search_p = "total rejected rows:";
-
&search_pattern($file_n, $search_p);
-
Hope this helps,
Mehjabeen
First, Mercury,
Why does this sound like a homework question? But, since you provided code, at least you aren't looking for the answers without doing some work.
While I see that mehj123 has provided a Perl solution, I have not looked at it to see if it works.
mehj123,
Please know that while I understand you are just providing alternatives, this is the Perl forum and not the AWK forum. The user was looking for a Perl solution to their issue.
Regards,
Jeff
thanks a lot Mehjabeen....
works great....
mehj123,
Please know that while I understand you are just providing alternatives, this is the Perl forum and not the AWK forum. The user was looking for a Perl solution to their issue.
Regards,
Jeff
Hi Jeff,
Will keep it in mind in future.. But I thought since Mercury had provided the code and all that was needed was a split function, I gave the solution.
Anyways, Thanks for the feedback :)
Thanks
Mehjabeen
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dag |
last post by:
Is there a python module that includes functions for working with prime
numbers? I mainly need A function that returns the Nth prime number and
that returns how many prime numbers are less than N,...
|
by: Einar |
last post by:
Hi,
I wonder if there is a nice bit twiddling hack to compare a large
number of variables?
If you first store them in an array, you can do:
for (i = 0; i < n; i++) {
if (array != value) {...
|
by: sibingpeter |
last post by:
Hi there,
Im trying to find the right way to code the loop to count the number of
occurences of a given substring in a string. Im able to find the first
occurence using the strstr function, but...
|
by: Natti |
last post by:
Hello,
I have a perl program which parses an input file for specific error patterns and generates an output file with all these lines.
One of the error patterns that I am looking at spans across...
|
by: programming |
last post by:
how do i delete from a text file 1 of the following lines:
jon|scott
adam|smith <--delete
paul|clark
say i would like to delete the middle line of this txt, in member.txt
what php code or...
|
by: mike.aldrich |
last post by:
Hi folks,
I am trying to read the first occurence of non-whitespace in a file,
within a zipfile. Here is my code:
zipnames = glob.glob("<search_dir>*")
for zipname in zipnames:
z =...
|
by: IdleBrain |
last post by:
I am trying to log the Application name, Method name, line number and
column number whenever an exception is generated in an C# 2005
application using the following code.
Problem is that the...
|
by: mpatharkar |
last post by:
Hi all,
I have one input file "pick no.txt" from which i have to find perticuler pattern and print it in to output file "Numbers.txt".
The input file is
1."pick no.txt"
Number:
|
by: lilly07 |
last post by:
I am trying open a text file, and find the occurence of first column string (ie) $v in a separate file which contains only one column.
my code is as follows but somehow it doesn't seem to count...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |