By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,253 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,253 IT Pros & Developers. It's quick & easy.

line number and first occurence of a pattern

P: 8
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
Share this Question
Share on Google+
4 Replies


mehj123
P: 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
Expert Mod 2.5K+
P: 3,503
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

P: 8
thanks a lot Mehjabeen....

works great....
Jan 22 '08 #4

mehj123
P: 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

Post your reply

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