|
JMinara Beginner
Joined: 05 Jul 2005 Posts: 14
|
Posted: Thu Apr 20, 2006 6:59 am
Determining line number of a string in a text file |
I'm working with a textfile and I'm searching for a string, and I need to know the line number of that string. Basically I need %grep, except instead of returning the value of the line I need it to return the line number.
Is there any way to do that?
Edit: Evidently not. |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4715 Location: Pensacola, FL, USA
|
Posted: Thu Apr 20, 2006 7:19 am |
if you can loop through the file line by line, adding them in turn to a variable array... %ismember would return the numeric position within the array
|
|
_________________ Discord: Shalimarwildcat |
|
|
|
Tarn GURU
Joined: 10 Oct 2000 Posts: 873 Location: USA
|
Posted: Thu Apr 20, 2006 7:20 am Re: Determining line number of a string in a text file |
JMinara wrote: |
I'm working with a textfile and I'm searching for a string, and I need to know the line number of that string. Basically I need %grep, except instead of returning the value of the line I need it to return the line number.
Is there any way to do that?
Edit: Evidently not. |
Why not?
Open a file with #FILE.
You probably have a readme.txt in your zMud directory, so
#FILE 1 "readme.txt"
(opening "readme.txt" as file 1)
Then,
#SHOW %read(1)
will read a single line from file 1.
Instead of showing the line, put it in a loop with a counter, and use the "~=" pattern-checking operator to see if a particular line matches a pattern. If it matches the pattern, the counter can tell you what line you're on.
Alternatively: use the mechanism above to read the file and write another, numbering the lines as it goes. %Then grep could give you the numbered lines matching your pattern from the new file.
-Tarn |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|