Search for string and return lines above and below


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for string and return lines above and below
# 1  
Old 03-17-2009
Search for string and return lines above and below

Hi,

I have a file with the following contents:

COMPANY ABCD
TRUCKER JOE SMITH
TRUCK 456
PLATE A12345678
TRUCKER BILL JONES
TRUCK 789
PLATE B7894561

I need to create a script or search command that will search for this string '789' in the file. This string is unique and only occurs in the 'TRUCK' record. Once it finds the string, I would like it to return the first occurrence of COMPANY above the string (since there can be many more instances above). And I would also like to return the record immediately after the search, which would be PLATE.

EXAMPLE:

COMPANY ABCD
TRUCK 789
PLATE B7894561

Any help is appreciated.
# 2  
Old 03-17-2009
nawk -f doza.awk myFile

doza.awk:
Code:
/^COMPANY / {comp=$0; next}
$2 == "789" { num=$0; c=1; next}
c&&c-- { print comp ORS num ORS $0 }

# 3  
Old 03-17-2009
Thank you for the fast response. However 'nawk' is not available on my UNIX system. (HP-UX)

Can I use a different command?
# 4  
Old 03-17-2009
'awk' or 'gawk' should do
# 5  
Old 03-17-2009
The 'awk' command worked and returned the intended results!

Thanks again for the fast response!

Chris
# 6  
Old 03-17-2009
Sorry to bother again, but is there a way to pass the awk script the search string?

Instead of '789', can I pass, '456' or another value to the script?
# 7  
Old 03-18-2009
Quote:
Originally Posted by doza22
Sorry to bother again, but is there a way to pass the awk script the search string?

Instead of '789', can I pass, '456' or another value to the script?
nawk -v pat=456 -f doza.awk myFile

Code:
/^COMPANY / {comp=$0; next}
$2 == pat { num=$0; c=1; next}
c&&c-- { print comp ORS num ORS $0 }

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

2. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

3. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

4. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

5. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

6. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

7. UNIX for Dummies Questions & Answers

search for string and return substring

Hi, I have a file with the following contents: I need to create a script or search command that will search for this string 'ENDC' in the file. This string is unique and only occurs in one record. Once it finds the string, I would like it to return positions 101-109 ( this is the date of... (0 Replies)
Discussion started by: Lenora2009
0 Replies

8. Shell Programming and Scripting

Search string and return only the 5th line

I have looked at almost every posting on here regarding, grep, sed, awk, nawk, perl methods to perform this function, but they are just not returning the results I hope to see. Example File: abcdef 123456 ghijkl 7890123 7890123 7890123 7890123 mnopqrs 456789 This is the most... (4 Replies)
Discussion started by: doza22
4 Replies

9. Shell Programming and Scripting

Script to find string and return next 2 lines

I am trying to create a script to search for a string within a file, and if found, return the next two lines. Example file:- msj mh query return this 1 return this 2 mjk mhj query return this 3 return this 4 So the script would identify the string "query" and then return the lines... (10 Replies)
Discussion started by: daveaasmith
10 Replies

10. Shell Programming and Scripting

String search and return value from column

Dear All I had below mention file as my input file. 87980457 Jan 12 2008 2:00AM 1 60 BSC1 81164713 Jan 12 2008 3:00AM 1 60 BSC2 78084521 Jan 12 2008 4:00AM 1 60 BSC3 68385193... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies
Login or Register to Ask a Question