first occurence and line number

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.

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\);

Not the best one , but willl work. This works only if the file is in the specifed format, or else may result in inconsistent results ...take care.

team$ cat myfile :slight_smile: ---Just copied from your example

................... ..
total rejected rows: 40
total rejected rows: 50
total rejected rows: 80
total rejected rows: 90

total discarded rows: 40
................. ..
................. ..
................. ..

team$

team$ cat perl1.pl
#!/usr/bin/perl -w

open (FH,"<myfile") || die ( "Can't Open myfile :$! " );

while (<FH>) {
if ( $_ =~ /total rejected rows: / ) {
print "First Rejected Row Found \n";
my $val = $';
print "$val". "Hope this is the Value \n";
last;
}
}

close(FH);
team$

team$ perl1.pl
First Rejected Row Found
40
Hope this is the Value
team$

Sorry, to get the line number, keep incrementing a variable and get that printed/stores when the match is found.

Hi,

i think below one is easy and can meet your requirements.

cat -n filename | sed -n '/total rejected rows/p' | head -1

if you have GNU grep

# grep -n -m 1 "pattern" file
3:this is line

nl filename | grep "total rejected rows" | head -1

A different try:

awk '/rejected/ {print NR $0; exit;}' filename

thanks guys.... :b:

hi,

thanx for ur great work....

how do i delete the whitespaces in the output...
First Occurance: 100
the space between ":" and "100"

i tried to use this awk command to delete the whitespaces and i got the error.

can u guys suggest some solutions?

awk '{sub(/^[\t]+/, ""); print}'

#!/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>)
                {


if ( $_ =~ /$search/ ) {

#print " First Occurance:$val \n";

my $val = $';

`awk '{sub(/^[\t]+/, ""); print "$val"}'`


print "line number:$.\n";

last;
}
}
}

my $file_n ="test.txt";
my $search_p = "total rejected rows:";
&search_pattern($file_n, $search_p);

Errors:

syntax error at search5.pl line 26, near "print"
Execution of search5.pl aborted due to compilation errors.

If I dont give the awk command,

the output is:

First Occurance: 100

line number:8

thanks,

Mercury.

why are you mixing perl with awk. although you can do call an awk statement from inside Perl, but try not to do that. If you want to use Perl in this case, then do it in Perl. To substitute white space, see here for example.