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
# 8  
Old 08-28-2008
further clarification

Thanks for the awk command - it works!, but not entirely. Smilie

Consider my input file parts.txt as follows.
1 useless line....
2 useless line....
3 useless line....
4 PROCESSING PART 111
5 useless line....
6 useless line....
7 ERROR processing part!
8 useless line....
9 useless line....
10 PROCESSING PART 222
11 useless line....
12 useless line....
13 useless line....
14 useless line....
15 SUCCESS!
16 PROCESSING PART 333
17 useless line....
18 useless line....
19 SUCCESS!
20 PROCESSING PART 444
21 useless line....
22 useless line....
23 ERROR processing part!
24 PROCESSING PART 555
25 useless line....
26 useless line....
27 SUCCESS!
28 useless line....
29 useless line....
30 useless line....
31 useless line....
I am looking for PART with ERROR in a pair. So my expected output should be as follows.
7 ERROR processing part!
4 PROCESSING PART 111
23 ERROR processing part!
20 PROCESSING PART 444
Running the suggested awk command returned the SUCCESS PARTs also. See below.
awk '/ERROR/ { lastb=$0 } /PROCESSING/ { print; print lastb; lastb="" }' parts.txt
4 PROCESSING PART 111

10 PROCESSING PART 222
7 ERROR processing part!
16 PROCESSING PART 333

20 PROCESSING PART 444

24 PROCESSING PART 555
23 ERROR processing part!
I only need the first occurence of the PART number in the reverse from the line where 'ERROR' is found.

Thanks.
# 9  
Old 08-28-2008
You seem to have mixed up the A and the B on your side. You should collect the PROCESSING and print when you see ERROR, shouldn't you?
# 10  
Old 08-28-2008
Quote:
Originally Posted by era
You seem to have mixed up the A and the B on your side. You should collect the PROCESSING and print when you see ERROR, shouldn't you?
Sorry for my reverse logic. But I was trying to avoid some of the intermediate 'PROCESSING' lines that I dont need to report.
# 11  
Old 08-28-2008
Code:
vnix$ awk '/PROCESSING/ { lastb=$0 } /ERROR/ { print lastb; print; lastb="" }' <<HERE
> 1 useless line....
> 2 useless line....
> 3 useless line....
> 4 PROCESSING PART 111
> 5 useless line....
> 6 useless line....
> 7 ERROR processing part!
> 8 useless line....
> 9 useless line....
> 10 PROCESSING PART 222
> 11 useless line....
> 12 useless line....
> 13 useless line....
> 14 useless line....
> 15 SUCCESS!
> 16 PROCESSING PART 333
> 17 useless line....
> 18 useless line....
> 19 SUCCESS!
> 20 PROCESSING PART 444
> 21 useless line....
> 22 useless line....
> 23 ERROR processing part!
> 24 PROCESSING PART 555
> 25 useless line....
> 26 useless line....
> 27 SUCCESS!
> 28 useless line....
> 29 useless line....
> 30 useless line....
> 31 useless line....
> HERE
4 PROCESSING PART 111
7 ERROR processing part!
20 PROCESSING PART 444
23 ERROR processing part!

Note the minor change I made in the printing order (they would come out with the ERROR before the PROCESSING line in the original script).
# 12  
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.
I adopted the command as follows:
awk '/ERROR/{a=$0}/PROCESSING/ && a {print a;a=""}' parts.txt
and got the following...
7 ERROR processing part!
23 ERROR processing part!
The expected result is ...
4 PROCESSING PART 111
20 PROCESSING PART 444
I dont really need the 'ERROR' lines. I was only trying to show the pairing in my example.
# 13  
Old 08-28-2008
Computer

Quote:
Originally Posted by era
Code:
vnix$ awk '/PROCESSING/ { lastb=$0 } /ERROR/ { print lastb; print; lastb="" }' <<HERE
> 1 useless line....
> 2 useless line....
> 3 useless line....
> 4 PROCESSING PART 111
> 5 useless line....
> 6 useless line....
> 7 ERROR processing part!
> 8 useless line....
> 9 useless line....
> 10 PROCESSING PART 222
> 11 useless line....
> 12 useless line....
> 13 useless line....
> 14 useless line....
> 15 SUCCESS!
> 16 PROCESSING PART 333
> 17 useless line....
> 18 useless line....
> 19 SUCCESS!
> 20 PROCESSING PART 444
> 21 useless line....
> 22 useless line....
> 23 ERROR processing part!
> 24 PROCESSING PART 555
> 25 useless line....
> 26 useless line....
> 27 SUCCESS!
> 28 useless line....
> 29 useless line....
> 30 useless line....
> 31 useless line....
> HERE
4 PROCESSING PART 111
7 ERROR processing part!
20 PROCESSING PART 444
23 ERROR processing part!

Note the minor change I made in the printing order (they would come out with the ERROR before the PROCESSING line in the original script).
Awewsome! That's so cool. Thanks. Smilie
# 14  
Old 08-28-2008
Quote:
Originally Posted by PacificWonder
I am looking for PART with ERROR in a pair. So my expected output should be as follows.
7 ERROR processing part!
4 PROCESSING PART 111
23 ERROR processing part!
20 PROCESSING PART 444
Code:
awk '/PROCESSING/{a=$0} /ERROR/ && a {printf "%s\n%\s\n", $0, a ;a=""}' file

Please learn howto use the code tags, and try to provide the input sample and expected results sample from the beginning.

Regards,
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