The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 09:29 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 10:19 AM
Perl onliner to search the last line with an occurence of a pattern ammu Shell Programming and Scripting 4 01-31-2008 01:09 AM
Count the number of occurence of perticular word from file rinku Shell Programming and Scripting 40 08-10-2007 08:33 PM
identifying duplicates line & reporting their line number stresslog UNIX for Dummies Questions & Answers 5 04-24-2006 01:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-21-2008
mercuryshipzz mercuryshipzz is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 16
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.

[Code = Perl]

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


[/code]
  #2 (permalink)  
Old 01-21-2008
Rajesh Kumar Rajesh Kumar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
Not the best way, but it works

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 ---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$
  #3 (permalink)  
Old 01-21-2008
Rajesh Kumar Rajesh Kumar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
To get the line number ...

Sorry, to get the line number, keep incrementing a variable and get that printed/stores when the match is found.
  #4 (permalink)  
Old 01-21-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
a easy one

Hi,

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

Code:
cat -n filename | sed -n '/total rejected rows/p' | head -1
  #5 (permalink)  
Old 01-21-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,539
if you have GNU grep
Code:
# grep -n -m 1 "pattern" file
3:this is line
  #6 (permalink)  
Old 01-22-2008
ThobiasVakayil ThobiasVakayil is offline
Registered User
  
 

Join Date: May 2006
Posts: 64
nl filename | grep "total rejected rows" | head -1
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0