How to print the matched line number using perl grep
How to print a line number of the matched pattern using perl grep.
I am doing the following , I can print the matched line but not the line number. Code:
open $INPUT, '<', $filename |
I guess you have to loop through all lines with a counter, do a grep on each line and if it matches you have the line number and the matching line.
|
Yes setting a line counter and searching line by line is a quick solution.
I wanted to check whether perl grep has any option for line numbers as Unix grep -n. |
perldoc perlfunc
says that it does not have such options |
Not this way.
The problem is that <$INPUT> is treated as an array construction and will read the entire file into a buffer, then the grep will search that buffer - thus no line numbers are available. You can get what you want - but using: Code:
open $INPUT, '<', $filename; |
I dont want to do line by line comparison, thats why I moved to perl grep .... I am looking for a workaround if no option is provided by perl grep.
|
Quote:
If you want a matched pattern you must do line by line comparison. How else can it be done? (The only difference is how do you organize the loop) |
I am looking for solution in this way.... and not the line matching way.
|
Well, you won't find it.
|
Well, Thanks for your valuable time.
I will go with line by line option. This can be one of the shortcomings of PERL compare to shell script. Have a good day ! |
Quote:
my @oh = `grep -n PATTERN FILENAME`; print @oh; So I guess there is no real shortcoming. |
Quote:
|
I am using line by line option now and tried searching pattern from root (/)
I am getting the following error Quote:
PS: Yes, grep with backticks will create additional process. |
All times are GMT -5. The time now is 12:51 PM. |