![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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] |
|
||||
|
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$ |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|