The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2008
PacificWonder PacificWonder is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 10
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 files
Please let me know if you have any suggestions on how I could go about this.
Thanks.
  #2 (permalink)  
Old 08-28-2008
PacificWonder PacificWonder is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 10
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.
  #3 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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
  #4 (permalink)  
Old 08-28-2008
PacificWonder PacificWonder is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 10
"Or does "first up" in steps 3 and 5 mean the latest B before the current A?"

Yes, I only need to get the latest pattern B preceeding the current pattern A.

Thanks.
  #5 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Code:
awk '/B/ { lastb=$0 } /A/ { print; print lastb; lastb="" }' file
  #6 (permalink)  
Old 08-28-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
Code:
awk '/B/{a=$0}/A/ && a {print a;a=""}' file
a will be the last occurrence of B.
  #7 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Quote:
Originally Posted by danmero View Post
Code:
awk '/B/{a=$0}/A/ && a {print a;a=""}' file
a will be the last occurrence of B.
Intuitive (-;

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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0