Script to find string and return next 2 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find string and return next 2 lines
# 1  
Old 09-11-2008
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 "return this 1", "return this 2", "return this 3" and "return this 4".

How best to do this?

Regards, Dave.
# 2  
Old 09-11-2008
Actually, I tried the following:-

awk 'c&&c--;/query/{c=2}' inputfilename

and it worked fine for me!
# 3  
Old 09-11-2008
grep query -A 2 inputFileName | grep -v query
# 4  
Old 09-11-2008
Quote:
Originally Posted by yogi_raj_143
grep query -A 2 inputFileName | grep -v query

The -A option to grep is not standard.

Last edited by cfajohnson; 09-12-2008 at 04:03 AM..
# 5  
Old 09-12-2008
Code:
cat filename | sed -n '/query/{n
p
n
p
}'

# 6  
Old 09-12-2008
Quote:
Originally Posted by summer_cherry
Code:
cat filename | sed -n '/query/{n
p
n
p
}'


cat is an unnecessary external command:

Code:
sed -n '/query/{n;p;n;p;}' filename

# 7  
Old 10-09-2008
I am currently using the following query to print the lines AFTER a pattern:

awk '/Statement Execution Time/{c=10}c&&c--'

Can this be adapted to print the lines after a pattern, up to a second pattern? I want the lines after (and including) "Statement", up to (not including) "Stack Trace". There may not always be the same number of lines between the two.

(Example File)
Statement Execution Time
Select Distinct.......
From.........
Where..........
Order By........
Stack Trace
config.java.feeds.invoices
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Find command doesn't return in shell script.

Hi All, I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file. However, this doesn't work as I believe the find command doesn't exit's... (11 Replies)
Discussion started by: Vipin Batra
11 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. Shell Programming and Scripting

Find a string and then return the next 20 characters in multiple files

Hello all, I have a directory with 2000+ files. I need to look in each file for an invoice number. To identify this, i can search for the string 'BIG' and then retrieve the next 30 characters. I was thinking awk for this, but not sure how to do it. Each file contains one long string and in... (8 Replies)
Discussion started by: jdinero
8 Replies

4. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

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

7. Shell Programming and Scripting

Help to find string and return following characters from list of files

Hi, I'm fairly new to UNIX, but hopefully some-one can help me with this: I am using the following code to find files with the name "example.xml": find . -name "example.xml" -print that would print me a list like the example here: ./dir1/dir2/example.xml... (5 Replies)
Discussion started by: boijie
5 Replies

8. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

9. UNIX for Dummies Questions & Answers

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... (7 Replies)
Discussion started by: doza22
7 Replies

10. Shell Programming and Scripting

find string, then get the next 3 lines in a file

Hi all. HPUX - /bin/sh (posix) I am parsing a 3 field flat file, space deliminted example data.file acct dining mem open 0 50 dep 50 0 close 255 0 acct plus mem open 100 100 dep 50 0 close 323 0 I would like to find strings, then write the next 3 lines out to another file ... (8 Replies)
Discussion started by: lyoncc
8 Replies
Login or Register to Ask a Question