reverse search a text file from a specified line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reverse search a text file from a specified line
# 1  
Old 08-28-2008
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  
Old 08-28-2008
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  
Old 08-28-2008
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 02:49 PM.. Reason: Clarification indicates it's the easy one
# 4  
Old 08-28-2008
"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  
Old 08-28-2008
Code:
awk '/B/ { lastb=$0 } /A/ { print; print lastb; lastb="" }' file

# 6  
Old 08-28-2008
Code:
awk '/B/{a=$0}/A/ && a {print a;a=""}' file

a will be the last occurrence of B.
# 7  
Old 08-28-2008
Quote:
Originally Posted by danmero
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.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

3. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

4. Shell Programming and Scripting

Better and efficient way to reverse search a file for first matched line number.

How to reverse search for a matched string in a file. Get line# of the first matched line. I am getting '2' into 'lineNum' variable. But it feels like I am using too many commands. Is there a better more efficiant way to do this on Unix? abc.log aaaaaaaaaaaaa bbbbbbbbbbbbb... (11 Replies)
Discussion started by: kchinnam
11 Replies

5. UNIX for Dummies Questions & Answers

search all file for particular text and make changes to line 3

Hi All, I am sitting on HPUX. I want to change the exit into #exit, which appears into 3red line of code in shell scripting, wondering how shell script to be called up to perform action. I have following code in all files. Now, I need to find the text exit and replace into #exit. #!/sbin/sh... (10 Replies)
Discussion started by: alok.behria
10 Replies

6. Shell Programming and Scripting

Conditional reverse of certain file line order

Hi I am brand new to programming, I dont know how to go about this task, or what language is best for this...If there is an easy solution in different languages, I would love to see them. I want to learn about the steps to take on this, so Please put in comments where code is used. I believe in... (9 Replies)
Discussion started by: perlrookie
9 Replies

7. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

8. Shell Programming and Scripting

read line in reverse order from file

dear all i want to read 5th no of line from last line of file. kindly suggest me possible ways. rgds jaydeep (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies

9. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question