Search String and extract few lines under the searched string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search String and extract few lines under the searched string
# 1  
Old 01-08-2014
Code 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 KSZ063 and get the below 6 lines to it.


Ex sample file
Code:
KSZ077-082200-
GRAY-
INCLUDING THE CITIES OF...CIMARRON...MONTEZUMA
900 AM CST WED JAN 8 2014

.REST OF TODAY...CLOUDY. HIGHS AROUND 30. NORTH WINDS 10 TO
15 MPH WITH GUSTS TO AROUND 25 MPH.
.TONIGHT...MOSTLY CLOUDY. LOWS AROUND 14. NORTH WINDS UP TO 5 MPH
SHIFTING TO THE SOUTHWEST AFTER MIDNIGHT.
.THURSDAY...MOSTLY SUNNY. HIGHS IN THE MID 30S. SOUTH WINDS 5 TO
15 MPH.

$$

KSZ063-082200-
FINNEY-
INCLUDING THE CITIES OF...GARDEN CITY...KALVESTA
900 AM CST WED JAN 8 2014

.REST OF TODAY...CLOUDY. HIGHS AROUND 30. NORTH WINDS 5 TO 15 MPH
WITH GUSTS TO AROUND 25 MPH.
.TONIGHT...MOSTLY CLOUDY. LOWS AROUND 14. NORTHEAST WINDS UP TO
5 MPH SHIFTING TO THE SOUTH AFTER MIDNIGHT.
.THURSDAY...MOSTLY SUNNY. HIGHS IN THE UPPER 30S. SOUTH WINDS
5 TO 15 MPH.

$$

# 2  
Old 01-08-2014
Code:
grep -A6 KSZ063 file

# 3  
Old 01-08-2014
It gives me an illegal option. Any ideas of using with awk

Code:
$ grep -A6 KSZ063 *.zone
grep: illegal option -- A
grep: illegal option -- 6
Usage: grep -hblcnsviw pattern file . . .

# 4  
Old 01-08-2014
Code:
awk '/KSZ063/{f=1}c==7{c=0;f=0}f&&++c<=7' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 01-08-2014
It works great !!!! Thanks Yoda.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

2. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

3. 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

4. Shell Programming and Scripting

Append a searched string with another string using sed

Hi, I need to replace and append a string in a text if grep is true. For eg: grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ? How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be, lx123 web222 xyz Want to... (8 Replies)
Discussion started by: vchee
8 Replies

5. 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

6. 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

7. Shell Programming and Scripting

put a string before a searched string

hi all! i have a working that looks like this... file1 MALE JOHN MALE ANJO FEMALE ANNE MALE JAMES FEMALE HONEY FEMALE IZA what i want to do is insert "M" when the first string is "MALE" and insert "F" when the first string is "FEMALE". file1 M MALE ... (10 Replies)
Discussion started by: kingpeejay
10 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

10. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies
Login or Register to Ask a Question