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 3956
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
numberwhun 3,509
Recognized Expert Moderator Specialist
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, but a prime number
tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range
so it would have to fairly efficient
Dag
|
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) {
/* array differs from value, do something*/
|
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 I just cant seem to think of
the right loop that would continue searching after finding this first
occurence. Could someone please help me out here?
|
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 multiple lines. I can detect it can as error pattern using information from the first line but cannot print out the remaining lines.
Is there a way to regexp multiple lines and store all lines in a string or array.
For eg an error pattern would...
|
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 logic would help me accomplish this?
| |
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 = zipfile.ZipFile(zipname, "r")
for filename in z.namelist():
count = len(z.read(filename).split('\n'))
if fnmatch.fnmatch(filename, "*AUDIT*"):
|
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 line number that is being obtained by
stackFrame.GetFileLineNumber() represents AnotherForm.LogMessage
function's line number. I am trying to write the line number of the
statement which is causing an exception and not the LogMessage
function's line...
|
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 the number of occurences in the second file.
#!/usr/bin/perl -w
$datafile = $ARGV;
print "$datafile\n";
open (LIST1, $datafile) || die "File not found\n";
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |