473,748 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

line number and first occurence of a pattern

8 New Member
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5. use warnings;
  6. sub search_pattern
  7. {
  8.  
  9. my $file_name = $_[0];
  10.  
  11. my $search = $_[1];
  12. open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
  13.  
  14.  
  15.  
  16. while (<LOGFILE>) {
  17.     chomp($_);
  18.  
  19.     if (/$search/) {
  20.     awk '{print NF ":" $0}' #Value of the last field
  21.         print "\n$."  # prints the line number
  22.     }
  23. }
  24. }
  25.  
  26. my $file_n ="test.txt";
  27.  
  28. my $search_p = "total rejected rows:";
  29.  
  30. &search_pattern($file_n, $search_p);
  31.  
  32.  
  33.  
Jan 21 '08 #1
4 3956
mehj123
55 New Member
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5. use warnings;
  6. sub search_pattern 
  7. {
  8.  
  9. my $file_name = $_[0];
  10.  
  11. my $search = $_[1];
  12. open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
  13.  
  14.  
  15.  
  16. while (<LOGFILE>) {
  17.     chomp($_);
  18.  
  19.     if (/$search/) {
  20.     awk '{print NF ":" $0}' #Value of the last field
  21.         print "\n$."  # prints the line number
  22.     }
  23. }
  24. }
  25.  
  26. my $file_n ="test.txt";
  27.  
  28. my $search_p = "total rejected rows:";
  29.  
  30. &search_pattern($file_n, $search_p);
  31.  
  32.  
  33.  
Hi,

You should be running the awk command by enclosing it in `(back ticks).. eg
Expand|Select|Wrap|Line Numbers
  1. `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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. sub search_pattern
  7. {
  8.     my $file_name = $_[0];
  9.  
  10.     my $search = $_[1];
  11.  
  12.     open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
  13.  
  14.     while (<LOGFILE>)
  15.     {
  16.         chomp($_);
  17.  
  18.         if (/$search/)
  19.         {
  20.             my($line,$occ) = split(/:/,$_);
  21.             print "Occurence : $occ\n";
  22.             print "Line number : $.\n";
  23.         }
  24.     }
  25. }
  26.  
  27. my $file_n ="test.txt";
  28. my $search_p = "total rejected rows:";
  29. &search_pattern($file_n, $search_p);
  30.  
Hope this helps,

Mehjabeen
Jan 22 '08 #2
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
Jan 22 '08 #3
mercuryshipzz
8 New Member
thanks a lot Mehjabeen....

works great....
Jan 22 '08 #4
mehj123
55 New Member
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
Jan 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

36
8396
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
122
5317
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*/
4
14128
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?
8
10583
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...
13
2464
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?
7
3346
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*"):
5
4490
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...
8
1797
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:
3
3566
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";
0
8995
marktang
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...
0
8832
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,...
0
9381
jinu1996
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...
1
9332
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,...
0
9254
tracyyun
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...
0
6078
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();...
0
4608
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...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.