first occurence and line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting first occurence and line number
# 1  
Old 01-21-2008
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  
Old 01-21-2008
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 Smilie ---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  
Old 01-21-2008
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  
Old 01-21-2008
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  
Old 01-21-2008
if you have GNU grep
Code:
# grep -n -m 1 "pattern" file
3:this is line

# 6  
Old 01-22-2008
nl filename | grep "total rejected rows" | head -1
# 7  
Old 01-22-2008
A different try:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print occurence number

Hi folks, I have a file with lots of lines in a text file,i need to print the occurence number after sorting based on the first column as shown below, thanks in advance. sam,dallas,20174 sam,houston,20175 sam,atlanta,20176 jack,raleigh,457865 jack,dc,7845 john,sacramento,4567 ... (4 Replies)
Discussion started by: tech_frk
4 Replies

2. Programming

Python Count Number Of Occurence

Hello, I have a programming assignment to count number of occurrences of hours in particular file. Below is the code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" largest = None fh = open(fname) counts = dict() test = list() for line in fh: ... (2 Replies)
Discussion started by: infinitydon
2 Replies

3. UNIX for Dummies Questions & Answers

Add number of occurence

Good morning, I need help to add number of occurence based on column 1 & column 5 file input 81161267334|1|100000|81329998077|20150902 81161267334|1|50000|82236060161|20150902 81161268637|1|25000|81329012229|20150911 81161269307|1|25000|81327019134|20150901... (3 Replies)
Discussion started by: radius
3 Replies

4. Shell Programming and Scripting

Split column into two on first occurence of any number

Input : abc def 1 xyz zzz bca cde 2 yyy xxx Expected output : abc def |1 xyz zzz bca cde |2 yyy xxx I have tried the command below and losing the number. Any help is greatly appreciated 1. sed 's//|/' num.txt Result: abc def | xyz zzz bca cde |... (7 Replies)
Discussion started by: kbsuryadev
7 Replies

5. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

6. Shell Programming and Scripting

Number each occurence using sed

hi, I need to number each occurrence of a pattern within a file using sed. Given object 0000 object 111 object 222 I need following 1.object 0000 2.object 111 3.object 222 (5 Replies)
Discussion started by: xerox
5 Replies

7. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

8. UNIX for Dummies Questions & Answers

number of occurence using grep -c then assigning it to a variable

Hi guys! I need to count the occurence of a certain pattern. For example the pattern is PC. the contents of the file sample.txt: A PC asdfgadfjkl asdfa PC sadfaf fdsPCasdfg if i use grep -c PC sample.txt it will display 3 as the number of occurence how do i save that number to a... (1 Reply)
Discussion started by: khestoi
1 Replies

9. UNIX for Dummies Questions & Answers

grep the number of first occurence

File1.txt ....... ....... OMC LA OMC LK OMC LS ........ ........ Above is the content of File1.txt, i want to get the Number of Occurence to order, lets say if OMC LA = 1, OMC LS=3, and OMC LK=2.. omc_ident="OMC LA" or "OMC LK" or "OMC LS" omc_num=`grep '^OMC' File1.txt| grep... (4 Replies)
Discussion started by: neruppu
4 Replies

10. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies
Login or Register to Ask a Question