help for fast way of finding line number for a regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help for fast way of finding line number for a regex
# 1  
Old 09-05-2011
help for fast way of finding line number for a regex

Hello,

I am trying to find out the line numbers where regex match and put them into a file with below command:
Code:
 awk '/'$pat'/ {print NR}' $fileName >> temp.txt

where $pat is the regex

but this command is taking a lot of time to execute with bigger files for size more than 5000000 KBs.

could we make this faster by any alternative command?

Note: I am writing a script to find out a section of a file based on regex match. and the regex can be more than one. for example n lines above regex and n lines down the regex.
# 2  
Old 09-05-2011
try with fgrep ..
Code:
$ fgrep -n "$pat" infile

# 3  
Old 09-05-2011
Code:
 
grep -n $pat $fileName | cut -d: -f1 > temp.txt

# 4  
Old 09-05-2011
Of course it's slow, what you're doing is akin to trying to repairing a watch using a hammer: it's possible, but frustrating. In this case, the regex as you're applying it has to scan each line completely, checking each character on each line, checking for a match.

So the first question is: is it really a regex, or is it a fixed string?
And the second: can the regex be anchored in some way? Eg, start of the line, or the only word on the line, or something else to minimize the search cost?
# 5  
Old 09-05-2011
Hello pludi,

regex is a user input and can be any string and we just need to find out the line numbers where it matches anywhere on a line.

Jayan,

fgrep -n "$pat" infile is not working .... Smilie

---------- Post updated at 04:41 AM ---------- Previous update was at 04:37 AM ----------

may be this clarifies more....

I am trying to write a script which will give n lines above (user input) and n lines below (user input) the matched pattern(user input).
and the pattern may be n number of times in the file. so taking all occurance separatly and asking user for which occurance user want the above and below lines.
# 6  
Old 09-05-2011
Code:
$ fgrep -n "$pat" $fileName | cut -d: -f1 > temp.txt

# 7  
Old 09-05-2011
Quote:
I am trying to write a script which will give n lines above (user input) and n lines below (user input) the matched pattern(user input).
With GNU grep you can use -B (before) and -A (after) options.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding the line number of a particular line in a file

Hi Frnds, I need to find the line number of a particular line in a file and store that line number to a variable. if a file named myfile contains following look at the sun look at the moon look at the star look at the ocean i need to get the line number of the line 'look at the... (3 Replies)
Discussion started by: mvignesh
3 Replies

2. Shell Programming and Scripting

Finding the line with the exact same number

Hello All, What i am doing is , i tail a file from certain chatacter and then cat -n to get the line numbers.I search for a particular string and gets it line number. What i am interested in is the next line immediately after the pattern i search. But grep gives me result for all line... (5 Replies)
Discussion started by: kailash19
5 Replies

3. Shell Programming and Scripting

multiple regex finding in files

Hello folks, I have a text file aa.txt that contains below text (\')|(\-\-) ((\%3D)|(=)) 20%0d% i want to search each line pattern in /opt/1.log and /opt/2.log. Can some one suggest (1 Reply)
Discussion started by: learnbash
1 Replies

4. UNIX for Dummies Questions & Answers

awk - display from line number to regex

Hi. Is there a way in awk to show all lines between a line number and the next line containing a particular regex? We can do these, of course: awk '/regex1/,/regex2/' filename awk 'FNR > X && FNR < Y' filename But can they be combined? Thanks. (3 Replies)
Discussion started by: treesloth
3 Replies

5. Shell Programming and Scripting

Finding line with highest number in a file

Hi All, My file looks some thing like this, File 1: - A 10 B 30 C 5 D 25 E 72 F 23 now my requirement is to find the line with highest number in it, i;e the result should be E 72 Thanks in Advance (1 Reply)
Discussion started by: balu_puttaganti
1 Replies

6. 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

7. Shell Programming and Scripting

finding the line number from a grep ?

Hi there does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file test1 test2 test3 test1 and i needed to get the line numbers for all instances of test1 in that file with the answer being (1,4) Would anybody be able... (7 Replies)
Discussion started by: hcclnoodles
7 Replies

8. Shell Programming and Scripting

finding line number if the line contains

hi i have a file , that contains data like 34343538 3136414D 45583030 30302E54 445816AMEX0000.T 524E2020 20202020 20202020 20202020 RN 20202020 20203030 38303030 30303030 0080000000 30303030 30300D 000000. 20080724-051254.668419 D 473... (1 Reply)
Discussion started by: Satyak
1 Replies

9. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies

10. Shell Programming and Scripting

Finding the line number of matching braces

Hi,I am new to shell scripting and i want to find the line numbers of matching braces. The file contents are as follows File XXX.dat 1 ( CLASS "FRUIT" 2 (TYPE "PERSISTENT") 3 (MESSAGE_TYPE "M") 4 (GET_REQRD "Y") 5 (SET_REQRD "Y") 6 ) 7 ( CLASS... (3 Replies)
Discussion started by: Rajendra_1510
3 Replies
Login or Register to Ask a Question