473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

line number and first occurence of a pattern

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 3931
mehj123
55
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 Expert Mod 2GB
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
thanks a lot Mehjabeen....

works great....
Jan 22 '08 #4
mehj123
55
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
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,...
122
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) {...
4
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...
8
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...
13
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...
7
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 =...
5
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...
8
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
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...
1
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...
0
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...
0
isladogs
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...
0
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,...
0
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$) { } ...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...

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.