![]() |
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 |
| read line in reverse order from file | jaydeep_sadaria | Shell Programming and Scripting | 2 | 08-13-2008 03:53 AM |
| how can search a String in one text file and replace the whole line in another file | kkraja | UNIX for Dummies Questions & Answers | 6 | 08-06-2008 07:23 AM |
| search and replace a specific text in text file? | santosham | UNIX for Dummies Questions & Answers | 4 | 06-25-2008 05:53 PM |
| Perl: Search for string on line then search and replace text | Crypto | Shell Programming and Scripting | 4 | 01-04-2008 10:24 AM |
| Search for text and print the next line | alijassim | Shell Programming and Scripting | 2 | 08-17-2004 06:08 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
reverse search a text file from a specified line
Hello All,
I have a following task that I need to accomplish through a script or program and I am looking for some help as I have exhausted my ideas. 1. given: a text file with thousands of lines 2. find: pattern A in file and get line number ( grep -n works) 3. find: the first occurence of pattern B up from line number obtained in step2 4. find: pattern A again but after the line number previously found 5. find: the first occurence of pattern B up from the new line number in step4 6. repeat the process until the file is completely processed. Possible solutions? a) will vi work programmatically with forward search '/', go to line 'nG' and reverse searching '?' ? b) I think C program will work with file pointer, seek and other facilities. But I have no C compiler. c) will java work - RandomAccessFile - seek? d) how about awk? not sure if it has reverse searching capability. e) how about a combination of grep -n file, head -n, tac |grep this may be a possibility but could be tedious with a few intermediary filesPlease let me know if you have any suggestions on how I could go about this. Thanks. |
|
||||
|
I need to clarify step 3 and 5.
suppose I find pattern A on line 25 of the input text file, now I need to find the first occurence of pattern B from lines 25 through 1 of the file - in reverse. Say pattern B occurs on line 20 and line 15 and line 10. I need line 20, but not lines 15 & 10. Thanks. |
|
||||
|
If I understand the logic correctly, wouldn't it suffice to remember the B hits and rattle off one each time you see an A? This is basically a two-liner in Perl, I guess it could be done in awk as well but the lack of ordered arrays is a bit of a drag. (Or does "first up" in steps 3 and 5 mean the latest B before the current A? Then it's even easier, and doesn't require an array.)
Last edited by era; 08-28-2008 at 01:49 PM.. Reason: Clarification indicates it's the easy one |
|
||||
|
Quote:
Actually the && a is a useful addition, I meant to remember to not forget it, but failed. But you're not printing the A lines at all, did I misunderstand that part of the requirements? (Also actually not clear whether the matches should be printed or somehow collected, say, just collect the line numbers for later processing? Easy enough to do, NR is the current line number in awk.) |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|